
Let's start by listing the current version of Python that is installed.
~]# python --version
Python 2.7.5
Some Linux systems have both version 2 and version 3 of Python installed, so let's also check for Python 3.
~]# python3 --version
Python 3.6.8
Almost always, the Python binaries and symbolic links (more on this in a moment) are located in the /usr/bin directory.
]# ll /usr/bin/ | grep -i python
lrwxrwxrwx. 1 root root 7 Jul 9 02:24 python -> python2
lrwxrwxrwx. 1 root root 9 Jul 9 02:24 python2 -> python2.7
-rwxr-xr-x. 1 root root 7144 May 30 02:39 python2.7
-rwxr-xr-x. 1 root root 1835 May 30 02:39 python2.7-config
lrwxrwxrwx. 1 root root 16 Jul 9 02:24 python2-config -> python2.7-config
lrwxrwxrwx. 1 root root 9 Jul 9 02:24 python3 -> python3.6
-rwxr-xr-x. 2 root root 11336 May 30 07:42 python3.6
lrwxrwxrwx. 1 root root 17 Jul 9 02:24 python3.6-config -> python3.6m-config
-rwxr-xr-x. 2 root root 11336 May 30 07:42 python3.6m
-rwxr-xr-x. 1 root root 173 May 30 07:42 python3.6m-config
-rwxr-xr-x. 1 root root 3403 May 30 07:24 python3.6m-x86_64-config
lrwxrwxrwx. 1 root root 16 Jul 9 02:24 python3-config -> python3.6-config
lrwxrwxrwx. 1 root root 14 Jul 9 02:24 python-config -> python2-config
Likewise, on a Debian distribution (Ubuntu, Mint), apt list can be used to determine which Python packages are installed. On a Red Hat distribution (CentOS, Fedora, Red Hat), dnf list or yum list can be used.
~]# dnf list installed | grep -i python
python.x86_64 2.7.5-92.el7_9 @rhel-7-server-rpms
You can try using apt-get install or dnf install or yum install to install Python but this will almost always have no impact as most all Linux systems already have Python 3 installed.
dnf install python3
You can try something like this which should try to install Python version 3.9 but often this also fails since the Linux repo usually doesn't include the specific version of Python you want to install.
yum install python39
Often, to install a specific version of Python, you are going to have to download the Python installation tarball from https://www.python.org/downloads/. For example, if you want to install Python version 3.9.6 this should download a file named Python-3.9.6.tgz.
Or better yet, you can use wget to download the .tgz file.
wget https://www.python.org/ftp/python/3.9.6/Python-3.9.6.tgz
Place the .tgz file on the Linux system that you want to install Python on. Sometimes, you may be able to use pip for the install.
pip install Python-3.9.6.tgz
If pip fails, you can extract the tar archive.
tar -zxpf Python-3.9.6.tgz --directory /tmp
You can now remove the tar archive.
rm Python-3.9.6.tgz
Move into the extracted directory.
cd /tmp/Python-3.9.6
On a Red Hat distribution (CentOS, Fedora, Red Hat), yum list installed or dnf list installed can be used to list the installed OpenSSL packages.
- Python 3.9 can be configured with openssl version 1.0.x
- Python 3.10 and above must be configured with openssl version 1.1.x (e.g. openssl11)
]# yum list installed | grep -i openssl
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
openssl.x86_64 1:1.0.2k-26.el7_9 @rhel-7-server-rpms
openssl-devel.x86_64 1:1.0.2k-26.el7_9 @rhel-7-server-rpms
openssl-libs.x86_64 1:1.0.2k-26.el7_9 @rhel-7-server-rpms
On a Red Hat distribution (CentOS, Fedora, Red Hat), use yum install or dnf install to install the required packages.
dnf install openssl openssl-devel gcc bzip2-devel libffi-devel zlib-devel
Likewise, yum install or dnf install can be used to install the openssl version 1.1.x packages if you are installing Python version 3.10 or above.
dnf install openssl11 openssl11-devel gcc bzip2-devel libffi-devel zlib-devel
On a Debian distribution (Ubuntu, Mint), ensure the following packages are installed as they are required for Python 3.9 and above.
apt-get install gcc build-essential libncursesw5-dev libreadline5-dev libssl-dev libgdm-dev libbz2-dev libc6-dev libsqlite3-dev tk-dev
And then run the configure script. The --prefix option is used to specify the directory where Python will be installed. This command will not create /usr/local/bin or install Python in the /usr/local/bin directory. Instead, when the make altlinstall command is issued, Python will be installed in the /usr/local/bin directory.
./configure --prefix=/usr/local/bin/python<version>
You may also want to include the --enable-optimizations flag. This will increase the time that it takes to configure but should, as the flag suggests, optimize Python.
./configure --prefix=/usr/local/bin/python<version> --enable-optimizations
And then run the make command. You want this command to return Python build finished successfully!.
make
If Could not import runpy module is returned, you can try re-running ./configure without the --enable-optimizations flag and then make clean and make.
./configure
sudo make clean
sudo make
And then make altinstall. It's important to use altinstall here and not "make install" so that the new Python install does not replace a current Python installation.
sudo make altinstall
It's fairly common to have both Python 2 and Python 3 installed. In this scenario, it's also fairly common for /usr/bin/python to be symbolically linked to /usr/bin/python2.7.
~]# ls -l | grep /usr/bin/python
lrwxrwxrwx. 1 root root 7 Mar 7 23:31 /usr/bin/python -> python2
~]# ls -l | grep /usr/bin/python2
lrwxrwxrwx. 1 root root 9 Mar 7 23:31 /usr/bin/python2 -> python2.7
In other words, the "python" command points to Python version 2.7.
~]# python --version
Python 2.7.5
Whereas the python3 command points to Python version 3.x.
~]# python3 --version
Python 3.6.8
Since we installed Python in the /usr/local/bin directory, let's create a symbolic link from /usr/bin/python<major version>.<minor version> to /usr/local/bin/python<version>/bin/python<major version>.<minor version>.
ln -s /usr/bin/python3.9 /usr/local/bin/python3.9.6/bin/python3.9
Now the /usr/bin directory should contain a symbolic link from /usr/bin/python3.9 to /usr/local/bin/python3.9.6/bin/python3.9.
]# ll /usr/bin/ | grep -i python3.9
lrwxrwxrwx. 1 root root 40 Oct 31 07:14 python3.9 -> /usr/local/bin/python3.9.6/bin/python3.9
And the /usr/bin/python3.9 command can be used to see that Python 3.9.6 is indeed being used.
]# /usr/bin/python3.9 --version
Python 3.9.6
If you want the "python" command to invoke "python3", refer to this article.
Did you find this article helpful?
If so, consider buying me a coffee over at