Introduction

Python is a popular, stable, and well-performing programming language. It is used in many different Linux applications, including CentOS 7.

CentOS 7 uses Python 2.7.5, but as Python has updated to 3.7.2. , this can create challenges. If the default version of Python is updated, it can break base system components like the yum package manager.

This guide will walk you through how to install the most recent version of Python 3 on CentOS 7. We offer two options for installation on CentOS, via the SCL utility or from the source code.

tutorial on python installation on centOS

Note: Even though the latest version of Python is 3.7.2, this guide has you installing an earlier version for simplicity. For an advanced guide to installing Python 3.7.2 from source code, skip ahead to section 3.

Prerequisites

  • Access to a user account with sudo privileges
  • A CentOS 7 system, with the extras repository enabled
  • The yum package manager, included by default
  • A terminal window/command line (Ctrl-Alt-F2)

Preparing CentOS

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 (required for Option 1 installation)

The SCL or Software Collections project is an open-source project. Its goal is to let you install and run multiple versions of the same software on the same system. This 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 3rd-party repositories.

Option 1: Installing Python 3 With SCL

Step 1: 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 browse collections page for more information.

Step 2: Verify Version

1. In a terminal, enter the following: python ––version

The system should respond by displaying

Python 2.7.5

This tells you that the installation hasn’t changed the base Python that CentOS relies on.

2. To verify the installation of Python 3.6, first launch Python in an SCL shell: scl enable rh-python36 bash

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

3. Enter the following: python ––version

It should now display:

Python 3.6.x

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

Step 3: Install Development Tools

Developing in Python requires another software package, called Development Tools.

Install this package by entering: sudo yum groupinstall “Development Tools”

Allow the process to complete.

Step 4: Create a Virtual Environment

1. Start by creating a directory to store your new project:

mkdir ~/python_project

cd ~/python_project

You can rename the directory anything you’d like.

2. If you haven’t already, launch the SCL Python shell: scl enable rh-python36 bash

3. Create a new Python virtual environment: python –m venv python_project_venv

4. Activate the virtual environment: source python_project_venv/bin/activate

The command prompt should change to the name of the project you just created. You’re now working in Python 3.6.

Option 2: Installing Python 3 From Source Code

If you need a version of Python that’s not supported through SCL, you can download a copy of the source code. This 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.7.2/Python-3.7.2.tgz

4. Extract the package: sudo tar xzf Python-3.7.2.tgz

5. Compile the source code into an installation package:

cd Python-3.7.2.tgz

./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: python3.7 –v

The system should display:

Python 3.7.2

Conclusion

This guide gives you two different ways on how to install the latest version of Python3,  3.7.2 on CentOS 7.

It’s important to create a second installation, so you don’t accidentally break the functionality of other tools that rely on the older Python 2.7.5.

For more information on Python and versions, please see the Python developers’ page.