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

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.6 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

Python 3.6 can be installed on RHEL 8 / CentOS 8 by running the command below on your terminal.

sudo dnf install python3

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 python3-<packagename>

The same applies to all other Python 3 Libraries.

To use Python 3, just type

python3

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 -y install python2

Confirm:

$ which python2
/usr/bin/python2

To use Python 2.7, type the command:

python2

Set Default Python Version

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:

Set Python 3 as default:

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

Set Python 2 as default:

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

Running python -V should show default Python version configured

$ python -V
Python 2.7.15

Use Python 2 via python:

$ python
Python 2.7.15 (default, Oct 16 2018, 15:28:01) 
[GCC 8.2.1 20180905 (Red Hat 8.2.1-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

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

$ sudo alternatives --auto python