Install Python on CentOS 8

January 20, 2020

Introduction

Today, there are two active versions of Python. Although many companies are still using Python 2 for legacy reasons, Python 3 is slowly (but surely) taking over. This was confirmed by the news that Python 2 has no official support as of January 2020.

Since the programming language does not come preinstalled on the new CentOS 8, you can install the version of Python that suits your needs best. Another option would be installing both versions and switching between the two.

This article will show you how to install Python 3 and its predecessor Python 2 on CentOS 8.

tutorial on how to install Python on CentOS 8

Prerequisites

  • A CentOS 8 Linux OS
  • Access to the root user or a user account with sudo privileges
  • Access to a terminal window/command line (CtrlAltF2)

Update Local Repository (Optional)

You can install Python 3 by downloading the package from the local repository with the DNF package manager. Prior to installing, we recommend updating the repository.

Open a terminal window and update the repository with the command:

dnf update 

Install Python 3 on CentOS 8

With the repository up-to-date, type in the following command to download and install Python 3 on your system:

dnf install python3
command to install Python 3 on CentOS 8

If you aren’t logged in as the root user, make sure to start the command with the sudo prefix: sudo dnf install python3.

Verify whether Python 3 has been installed successfully:

python --version
check Python 3 version

The output should confirm you have successfully installed Python3.

Install Python 2 on CentOS 8

You can install Python 2 from the CentOS repository with a single command:

dnf install python2
installing Python 2 on CentOS 8

Type y and hit Enter to confirm you want to install.

output confirming Python 2 has been installed on CentOS 8

Verify the installation by prompting the system to show the active Python version with the command:

python2 --version
verify Python version

Run Python on CentOS

There is no default python command on CentOS 8.

To run Python 3, you need to use the command:

python3

To run Python 2, type:

python2

Set Default Version of Python

If you have more than one version of Python installed on your CentOS 8, you may need to set the default version of Python. Configuring the default version helps applications and programs that require a python command to navigate to the appropriate location.

Set Python 3 or Python 2 as the system-wide python command.

To assign Python 3 as the default version, use the command:

alternatives --set python /usr/bin/python3

You can also set Python 2 as the unversioned python command:

alternatives --set python /usr/bin/python2

You can also remove the unversioned command with:

alternatives --auto

Uninstall Python on CentOS 8

Use DNF to uninstall any Python version.

To remove Python 3, run the command:

dnf remove python3

The command uninstalls Python 3 and removes related dependencies. Confirm you want to remove the listed packages by typing y and hit Enter.

remove Python 3 from CentOS 8

If you want to remove Python 2, use:

dnf remove python2

Again, verify you want to remove the package with y and Enter.

remove Python 2 from CentOS 8

Conclusion

This step-by-step tutorial shows you how simple it is to install Python 3 and Python 2 on CentOS 8. It also includes instructions for setting the default Python version, as well as steps for removing Python from the system. Next, you can install Pip, a package manager for the Python.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
How to Install Pip on CentOS 8
August 3, 2020

Pip is a standard package manager for Python which you can use to install additional packages that are not...
Read more
How to Use Comments in Python
November 25, 2019

The ability to use comments while writing code is an important skill valued among developers. These comments...
Read more
How to Check Python Version in Linux, Mac, & Windows
December 15, 2023

Python is a popular programming language with different versions organized by release date. Certain...
Read more
How to Get the Current Date and Time in Python
March 12, 2024

The article shows you how to create a basic Python script that displays the current date and time. Find out..
Read more