How to install Python 3.x from source on Linux

This guide shows how to install Python 3.x from source on RHEL / CentOS 8 Linux.
At this time, I installed Python 3.9.

Procedures

1. Install required packages

(1) Install the packages required for compile.

dnf -y install gcc make

(2) Install the packages required for installation of Python and pip.

dnf -y install zlib-devel openssl-devel tk-devel libffi-devel

2. Download source of Python 3.x

Download the Python 3.9 source code.
Note: At this time, I downloaded Python-3.9.10.tar.xz
https://www.python.org/downloads/source/

3. Install Python 3.x

(1) Decompress a downloaded file.

chmod 755 Python-3.9.10.tar.xz
tar Jxvf Python-3.9.10.tar.xz

(2) Change current directory to the decompressed one.

cd ./Python3.9.10

(3) Create makefile.

./configure --prefix=/usr/local/python3910 --with-ensurepip

(4) Run make.

make

If it shows like the following, finished successfuly.

Python build finished successfully!

(5) Run installation.

make install

4. Make Python3 command run Python 3.x

(1) Create symbolic link from Python3.9 to Python3.

unlink /usr/bin/python3
ln -s /usr/local/python3910/bin/python3.9 /usr/bin/python3

(2) Similarly, create symbolic link from pip3.9 to pip3.

unlink /usr/bin/pip3
ln -s /usr/local/python3910/bin/pip3.9 /usr/bin/pip3

5. Comfirmation

(1) To confirm the version of Python 3, run the following command.

python3 --version

If it shows like the following, installed successfully.

Python 3.9.10

(2) Similarly, confirm the version of pip3.

pip3 --version

If it shows like the following, installed successfully.

pip 21.2.4 from /usr/local/python3910/lib/python3.9/site-packages/pip (python 3.9)

That’s about it.