This guide will walk you through how to install the most recent version of Python 3 on CentOS 7 and Ubuntu 18.04 or Ubuntu 20.04.

How to install Python 3 on CentOS 7

Step 1: Update the package manager

Before you begin, refresh the package listings. This helps ensure that yum is up to date.

Launch a terminal window, and enter:

sudo yum update

Allow the process to complete.

Step 2: Install the SCL utility

The SCL, or Software Collections, is an open-source project. Its goal is to let you install and run multiple versions of the same software on the same system. SCL is an excellent workaround for CentOS 7, where we don’t want to interfere with the system version of Python.

Install SCL with:

sudo yum install centos-release-scl


Note: For more information on SCL, or to browse their software collection, please visit the developers’ website. Also, note that the latest version of Python available through the SCL project is 3.6. It can take time for newer software releases to populate through third-party repositories.


Step 3: Install Python 3.6

To install Python 3.6, enter the following into a terminal window:

sudo yum install rh-python36

Note: If you prefer, you can install previous releases as well. SCL maintains Python 3.6, 3.5, 3.4, 3.3, and 2.7. Please see the Software Collections page for more information.


Step 4: Verify Version

In a terminal, enter the following to verify and check your version of Python:

python --version

The system should respond by displaying

The output shows that the installation hasn’t changed the base Python that CentOS relies on.

To verify the installation of Python 3.6, first launch Python in an SCL shell:

scl enable rh-python36 bash

This command places you in a shell where you can use your newer Python version.

Enter the following:

python --version

It should now display:

If you need to exit the shell, type exit and hit enter, and drop to a standard command prompt.

Installing Python 3 From Source Code

If you need a version of Python that is not supported through SCL, you can download a copy of the source code. This method requires additional software, plus extra steps to compile and install the Python package.

1. Start by installing the gcc, open-ssl-devel, and bzip2-devel packages:

sudo yum install gcc openssl-devel bzip2-devel

2. Browse to a directory to download the files: cd usr/src

3. Download the Python version you want to install:

sudo wget https://www.python.org/ftp/python/3.9.2/Python-3.9.2.tgz

4. Extract the package:

sudo tar xzf Python-3.9.2.tgz

5. Compile the source code into an installation package:

cd Python-3.9.2
./configure
./configure --enable-optimizations
make altinstall

The make command builds the installer package. The altinstall command instructs your system to create a second installation of this version of Python. Without it, the system would replace the default version of Python.

6. Verify the software version

 

How to 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


Note: 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

The output should confirm you have successfully installed Python3.

How to install Python 3 on Ubuntu 18.04 or Ubuntu 20.04

Option 1: Install Python 3 Using apt (Easier)

This process uses the apt package manager to install Python. There are fewer steps, but it’s dependent on third-party hosting software updates. You may not see new releases as quickly on a third-party repository.

Most factory versions of Ubuntu 18.04 or Ubuntu 20.04 come with Python pre-installed. Check your version of Python by entering the following:

python --version

or

python3 --version

If the revision level is lower than 3.9.x, or if Python is not installed, continue to the next step.

Step 1: Update and Refresh Repository Lists
sudo apt update
Step 2: Install Supporting Software

The software-properties-common package gives you better control over your package manager by letting you add PPA (Personal Package Archive) repositories. Install the supporting software with the command:

sudo apt install software-properties-common
Step 3: Add Deadsnakes PPA

Deadsnakes is a PPA with newer releases than the default Ubuntu repositories. Add the PPA by entering the following:

sudo add-apt-repository ppa:deadsnakes/ppa

The system will prompt you to press enter to continue. Do so, and allow it to finish. Refresh the package lists again:

sudo apt update
Step 4: Install Python 3

Now you can start the installation of Python 3.9 with the command:

sudo apt install python3.9

Allow the process to complete and verify the Python version was installed successfully:

python3.9 --version

Option 2: Install Python 3.9 From Source Code (Latest Version)

Use this process to download and compile the source code from the developer. It’s a bit more complicated, but the trade-off is accessing a newer release of Python.

Step 1: Update Local Repositories

To update local repositories, use the command:

sudo apt update
Step 2: Install Supporting Software

Compiling a package from source code requires additional software.

Enter the following to install the required packages for Python:

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget

Step 3: Download the Latest Version of Python Source Code

To download the newest release of Python Source Code, navigate to the /tmp directory and use the wget command:

cd /tmp
wget https://www.python.org/ftp/python/3.9.2/Python-3.9.2.tgz


Note: The source code is different from the software found on the main download page. At the time this article was written, Python 3.9.2 was the latest version available.


Step 4: Extract Compressed Files

Next, you need to extract the tgz file you downloaded, with the command:

tar -xf Python-3.9.2.tgz
Step 5: Test System and Optimize Python

Before you install the software, make sure you test the system and optimize Python.

The ./configure command evaluates and prepares Python to install on your system. Using the –optimization option speeds code execution by 10-20%.

Enter the following:

cd Python-3.9.2
./configure --optimization
Step 6: Install a Second Instance of Python (recommended)

To create a second installation of Python 3.9.2, in addition to your current Python installation, enter the following:

sudo make altinstall

It is recommended that you use the altinstall method. Your Ubuntu system may have software packages dependent on Python 2.x.

(Option) Overwrite Default Python Installation

To install Python 3.9.2 over the top of your existing Python, enter the following:

sudo make install

Allow the process to complete.

Step 7: Verify Python Version
python3 --version