How to Install Python 3.x on CentOS 7

Introduction

In this tutorial we’ll be learning how to install Python 3.6 and Python 3.7 on a server running Centos 7.

Python is a general purpose object oriented programming language that’s been growing in popularity at an incredible rate.

Not only has it been popular for it’s use in a wide range of areas, but it is also one of the easiest languages to learn. It’s a great programming language for beginners and experienced developers. It’s versatile and fast to learn and implement.

Some of the most popular applications you’ve heard of are written in Python – like Google, DropBox, YouTube, Quora, Instagram, Pinterest, Spotify, Reddit, BitTorrent, to name a few.

On top of all that, it’s also seen an explosive growth with the increasing rise of machine learning and AI.

Overview

CentOS 7 already comes with a pre-installed version of Python, unfortunately it’s much older, so we’ll have to install the latest version ourselves.

In this tutorial we’ll install Python 3 using two methods. The reason for this is that the latest Python version is not always available in Yum repositories, and versions from repositories are pre-compiled, so we have less control over them.

In the first method we’ll install the latest Python version available from a repository, and in the second method we’ll install the latest Python version by compiling it ourselves.

Get a High RAM VPS at Entry-level Pricing

Starting with 2GB RAM at $6.99/month

Take your pick from our KVM VPS that offer a generous amount of RAM at an affordable price. We've got 5 plans for you to choose from, our cheapest featuring 2GB RAM at $6.99/mo.

[Main Features] - SSD Drives - KVM Virtualization - 1Gbps Shared Uplink - Location: Chicago, IL

Pick one of our KVM plans

Method #1 – Installing Python 3.6 on Centos 7 from a Repository

This is the easier method to install Python on your machine.

For this, we’ll use Software Collections (SCL). SCL is a community project that allows us to install multiple versions of software on the same system, without affecting default packages. By using SCL we have access to newer versions of packages that aren’t available in the core repositories.

CentOS 7 comes with Python 2.7.5 built-in, which is critical to CentOS. By installing different versions of Python alongside the default v2.7.5, we won’t affect system tools such as `yum`, so they’ll continue to work properly.

Step 1 — Install SCL Release File

To enable SCL we’ll have to install the CentOS SCL release file. To do this run:

$ sudo yum install centos-release-scl

Step 2 — Install Python 3.6 from the Repository

Now that we have access to the SCL repository, we can install any version of Python they have available. You can easily search their website to see their pre-compiled Python versions. Currently, the available versions of Python 3 are:

  • Python 3.6
  • Python 3.5
  • Python 3.4
  • Python 3.3

In this tutorial, we’ll install the latest one, which is Python 3.6. To install it on our CentOS 7 machine, run the following command:

$ sudo yum install rh-python36

Now you should have Python 3.6 installed.

Step 3 — Using Python 3.6 from SCL

If you check what Python version you have, you should see the following:

python --version
Python 2.7.5

What you’re seeing is the version that comes with CentOS 7, that’s the default in your current shell.

You can use the new Python 3.6 version that you just installed, by running the following command:

$ scl enable rh-python36 bash

What we’ve just done is to use the Software Collection `scl` tool to launch a new shall instance in which the default version of Python we use is 3.6.

The command `scl` calls the script `/opt/rh/rh-python36/enable` which changes the shell environment variables.

Let’s check our version again:

python --version
Python 3.6.3

Do keep in mind that Python 3.6 is the default Python version only in this shell session. If you exit the session, and open a new one in the terminal, then the default version will be v2.7.5.

Method #2 – Compiling Python 3 from Source Code

This method of installing Python is the more difficult approach, and it will take a bit longer. An advantage to this is that you’ve got more control over it.

Please keep in mind that Yum will not know that you’ve installed a software if you manually compiled the source code. That means that no updates will be available if you’ve manually installed it.

Step 1 — Install Prerequisites

Before installing Python, we’ll need to install the necessary tools and libraries to compile and install programs on your system. To do install these prerequisites, run the following comand:

$ yum install gcc openssl-devel bzip2-devel libffi-devel

Step 2 — Download Python 3.x

Now we’ll download the latest version of Python 3 available on the official site – the latest at the time of writing is Python 3.7.1.

You may be following this tutorial at a later time than time of writing. To find the latest version visit Python.org > Downloads > Source code, click on the latest release and scroll down to Files. What we’ll need to download on our machine is the Gzipped source tarball.

We’ll download Python in `/usr/src`:

cd /usr/src
wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tgz

Next, we’ll extract the package:

$ tar xzf Python-3.7.1.tgz

Step 3 — Install Python3.x

With the source downloaded, we’ll now compile the Python 3.7.1 source code using `altinstall`. We use `altinstall` to install the new version of Python without replacing the default Python 2.7.5 that is used by CentOS 7.

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

This may take quite a while, depending on your machine.

Step 4 — Using Python 3.x

Once the installation is finished, check your Python version. To do this, we’ll use the command `python3.7` instead of the usual `python` command:

python3.7 --version
Python 3.7.1

Conclusion

We hope one of these methods of installing Python 3.x on CentOS 7 come in handy. If you’ve encountered any issues then don’t hesitate to leave a comment or contact us and we’ll get right back to you ASAP.

If you’re on the search for a good deal on a CentOS 7 server to run your Python projects then be sure to check out our Linux KVM Plans. We offer affordable VPS starting at 2GB RAM for $5.99/mo.

Cheers!

Get a High RAM VPS at Entry-level Pricing

Starting with 2GB RAM at $6.99/month

Take your pick from our KVM VPS that offer a generous amount of RAM at an affordable price. We've got 5 plans for you to choose from, our cheapest featuring 2GB RAM at $6.99/mo.

[Main Features] - SSD Drives - KVM Virtualization - 1Gbps Shared Uplink - Location: Chicago, IL

Pick one of our KVM plans

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments