Python is one of the most popular programming languages in the world. With its simple and easy to learn syntax, Python is a popular choice for beginners and experienced developers.

Unlike other Linux distributions, Python is not installed by default on CentOS 8.

As you already know, there are two Python versions that are being actively developed. While Python 2 is well-supported and active, Python 3 is considered to be the present and future of the language.

By default RHEL/CentOS 8 doesn’t have an unversioned system-wide python command to avoid locking the users to a specific version of Python. Instead, it gives the user a choice to install, configure, and run a specific Python version. The system tools such as yum use an internal Python binary and libraries.

This guide will walk you through installing Python 3 and Python 2 on CentOS 8.

Installing Python 3 on CentOS 8

To install Python 3 on CentOS 8 run the following command as root or sudo user in your terminal:

sudo dnf install python3

To verify the installation, check the Python version by typing:

python3 --version

At the time of writing this article, the latest version of Python 3 available in the CentOS repositories is “3.6.x”:

Python 3.6.8

The command also installs pip.

To run Python, you need to explicitly type python3 and to run pip type pip3.

You should always prefer to install distribution provided python modules using yum or dnf because they are supported and tested to work properly on CentOS 8. Use pip inside a virtual environment only. Python Virtual Environments allows you to install Python modules in an isolated location for a specific project, rather than being installed globally. This way, you do not have to worry about affecting other Python projects.

The names of the Python 3 modules packages are prefixed with “python3”. For example, to install the paramiko module, you would run:

sudo dnf install python3-paramiko

Installing Python 2 on CentOS 8

The Python 2 packages are also included in the default CentOS 8 repositories.

To install Python 2, enter the following command:

sudo dnf install python2

Verify the installation by typing:

python2 --version

The output should look something like this:

Python 2.7.15

To execute Python 2, type python2, and to run pip type pip2.

Set Default Python Version (Unversioned Python Command)

If you have applications that expect to find the python command in the system’s path, you’ll need to create the unversioned python command and set the default version.

To set Python 3 as the system-wide unversioned python command, use the alternatives utility:

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

For Python 2, type:

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

The alternatives command creates a symlink python that points to the specified python version.

Type python --version in your terminal, and you should see the default Python version.

To change the default version, use one of the commands above. If you want to remove the unversioned python command, type:

sudo alternatives --auto python

Conclusion

In CentOS 8, Python is not installed by default.

To install Python 3, type dnf install python3 and to install Python 2, type dnf install python2.

If you have any questions or feedback, feel free to comment below.