How to Install Python on CentOS 8

Reading Time: 3 minutes
python logo

In this tutorial, we will consider how to enable both Python 2 and Python 3 for use on CentOS 8. In earlier distributions of CentOS, an unversioned Python command was available by default. 

When the CentOS installation was complete, it was possible to drop into a Python shell by simply running the “python” command in a terminal.

Paradoxically, CentOS 8 does not have an unversioned Python command by default.  This begs the question, why? RedHat states that this choice is by design “to avoid locking users into a specific version of Python.” Currently, RedHat 8 utilizes Python 3.6 implicitly by default, although Python 2.7 is additionally provided to maintain existing software.

Previously, the unversioned Python command in CentOS distributions, while convenient, posed specific issues. The unversioned Python would typically point to a Python 2 interpreter, but because Python 2 is now at EOL (end of life), this becomes problematic for several reasons. Simply re-pointing the command to Python 3 may seem like an uncomplicated solution, but this would be problematic on many levels due to possible confusion with versioning. Instead of continuing to point the “python” command to a default version of Python because of familiarity, or pointing it to Python 3 to keep up with the times, the choice was made to no longer include a default “python” command.

This is not to say that Python is no longer available by default on CentOS. Both Yum and the new DNF package managers specifically rely on Python. The Python version they now depend on is called the “Platform-Python.” The “Platform-Python” version, for all intents and purposes, should not be employed as an interactive means to utilize Python. 

Prerequisites

  • As always, we will be working as the root user in the terminal on a fresh installation of a CentOS 8 server. 
  • Common knowledge of the command-line interface in Linux

Install Python 2

Step 1: Update the Environment

It is always a good idea to start things off by ensuring that all of our system packages are up to date before installing new software. We are going to take advantage of the new package management software DNF for this purpose.

[root@centos8 ~]# dnf update -y

Step 2: Install Python 2

Now that the environment has been updated, let’s go ahead and use DNF to install Python 2. Thankfully, both Python 2 and 3 are included in the CentOS 8 base package repositories, so installing is straight-forward.

oot@centos8 ~]# dnf install python2 -y

Step 3: Verify the Python 2 Install

To verify Python 2 is installed, we can run a simple “python2” command with the version flag.

[root@centos8 ~]# python2 -V
Python 2.7.16

Step 4: Running Python 2

Subsequently, in order to access the Python 2 shell, we can run the following command.

[root@centos8 ~]# python2
Python 2.7.16 (default, Nov 17 2019, 00:07:27)
[GCC 8.3.1 20190507 (Red Hat 8.3.1-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

That’s it! Python 2 is now installed! It should be noted, the Python package installer PIP is also installed by default when Python 2 is installed, so you should be able to start working with Python packages right away.

Install Python 3

Step 1: Update the Environment

Once again, to maintain best practices, let’s go ahead and ensure our system packages are all up to date.

[root@centos8 ~]# dnf update -y

Step 2: Install Python 3

We are now ready to install Python 3.

oot@centos8 ~]# dnf install python3 -y

Step 3: Verify the Python 3 Install

We can verify the installation and version of Python 3 the same way we did with Python 2.

[root@centos8 ~]# python3 -V
Python 3.7.5rc1

Step 4: Running Python 3

Next, we can enter into a Python 3 shell environment by running the following command.

[root@centos8 ~]# python3
Python 3.6.8 (default, Nov 21 2019, 19:31:34)
[GCC 8.3.1 20190507 (Red Hat 8.3.1-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

As with the Python 2 installation, pip3 is likewise included when you install Python 3. There you have it! You’re ready to start working with Python on your CentOS 8 server.

Series Navigation<< Previous ArticleNext Article >>
Refer a friend and get a $50 hosting credit!