Install Python 3 / Python 2.7 on CentOS 8|RHEL 8

Posted on 147 views

This guide will show you how to Install Python 3 or Python 2.7 on CentOS 8 / RHEL 8 Linux. RHEL / CentOS 8 has been built with development agility and production stability in mind. The default version of Python in RHEL/CentOS 8 is Python 3.6. But Python 2 remains available in RHEL 8.

If for any reason Python 3 is missing in your Red Hat Enterprise Linux 8 installation, you’ll need to install it manually.

Install Python 3 on CentOS 8 / RHEL 8

List AppStream modules available for Python on CentOS 8 / RHEL 8

$ sudo dnf module list | grep -i python
libselinux-python    2.8             common                                   Python 2 bindings for libselinux
python27             2.7 [d]         common [d]                               Python programming language, version 2.7
python36             3.6 [d][e]      build, common [d]                        Python programming language, version 3.6
python38             3.8 [d]         build, common [d]                        Python programming language, version 3.8
python39             3.9 [d][e]      build, common [d]                        Python programming language, version 3.9

We can choose the module to install from the output. I’ll install Python3.9 on my system.

sudo dnf install python3.9

A similar command can be used to install Python 3.8

sudo dnf install python3.8

Python 3 add-on packages generally have the python3 prefix in their names. For example, the dns module can be installed using:

sudo dnf install python3X-

#Example
$ sudo dnf install python39-PyMySQL

The same applies to all other Python 3 Libraries.

To use Python 3, just type

$ python3.9
Python 3.9.6 (default, Nov  9 2021, 13:31:27)
[GCC 8.5.0 20210514 (Red Hat 8.5.0-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

Install Python 2.7 on RHEL 8 / CentOS 8

For some guys with existing software not ready to run on Python 3, RHEL/CentOS 8 got you covered. It contains the Python 2 stack.

Install Python 2.7 on CentOS 8 / RHEL 8 in parallel with Python 3 using the command:

sudo dnf module install python27

Confirm:

$ which python2
/usr/bin/python2

$ which python2.7
/usr/bin/python2.7

To use Python 2.7, type the command:

$ python2
Python 2.7.18 (default, Dec 11 2021, 23:19:43)
[GCC 8.5.0 20210514 (Red Hat 8.5.0-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

Setting Default Python Version using alternatives

You should have noted that to use Python 3, the command is python3andpython2 for Python 2. What if your applications are configured to refer to python which is not available system-wide.

$ python
bash: python: command not found...

You can use the alternativesmechanism to enable the unversioned python command system-wide, and set it to a specific version:

Setting Python 3 as default

Set Python 3 as default:

# Default Python 3
sudo alternatives --set python /usr/bin/python3

# Default Python 3.9
sudo alternatives --set python /usr/bin/python3.9
sudo alternatives --set python3 /usr/bin/python3.9

Validate your settings

$ python -V
Python 3.9.6

$ python3 -V
Python 3.9.6

Setting Python 2 as default

Setting Python 2 as default:

sudo alternatives --set python /usr/bin/python2

Running python -V should show default Python version configured

$ python -V
Python 2.7.18

Use Python 2 via python:

$ python
Python 2.7.18 (default, Dec 11 2021, 23:19:43)
[GCC 8.5.0 20210514 (Red Hat 8.5.0-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

Unsetting default Python version

To reset this configuration and remove the unversioned python command, run:

sudo alternatives --auto python

That how easy it can be to Install Python3 | Python 2 on CentOS 8 / RHEL8. Enjoy using Python for your Development Projects in RHEL 8.

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.