How to install the latest Python 3.x in CentOS 6

Centos is a Red Hat Enterprise (RHEL) clone and comes free. It is an industry standard and a sophisticated operating system.

It is used by 90% of web hosting providers, because it supports a leading edge server control panel called cPanel/WHM.

cPanel allows users to manage everything through the control panel without entering the terminal.

We already know that RHEL has long-term support, and does not offer the latest version of packages due to stability.

If you want to install the latest version of the package, which is not available in the default repository, you must install the source package manually.

This is risky, because releasing a new version does not upgrade manually installed packages to the latest version, and you have to manually install them again in the latest version.

What would be the solution and the recommended method to install the latest version of the python package in this case?

Yes, you can do this by adding the required third-party repository to the system.

Enterprise Linux has many additional third-party repositories, but only a few repositories are recommended for use by the CentOS community, which do not replace the base packages.

They are usually well maintained and provide a significant number of additional packages for CentOS.

In this tutorial, we will teach you how to install the latest Python 3 package on CentOS 6 system.

Method-1 : Installing Python 3 in CentOS 6 with Software Collections(SCL) Repository

The SCL repository is now maintained by a CentOS SIG, which rebuilds the Red Hat Software Collections and also provides some additional packages of their own.

It contains newer versions of various programs that can be installed alongside existing older packages and invoked by using the scl command.

Run the following yum command to install Software Collections Repository (SCL) on CentOS:

# yum install centos-release-sc

Run the following command to check the Python 3 version available in the scl repository:

# yum --disablerepo="*" --enablerepo="centos-sclo-rh" list *python3

Run the below command to install the latest available Python 3 package from SCL:

# yum install rh-python36

Run the below command to check the Python 3 version installed using the SCL repository:

# yum info rh-python36

Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
 * base: centos.mirrors.estointernet.in
 * centos-sclo-rh: centos.mirrors.estointernet.in
 * centos-sclo-sclo: centos.mirrors.estointernet.in
 * epel: repos.del.extreme-ix.org
 * extras: centos.mirrors.estointernet.in
 * updates: centos.mirrors.estointernet.in
Installed Packages
Name        : rh-python36
Arch        : x86_64
Version     : 2.0
Release     : 1.el6
Size        : 0.0  
Repo        : installed
From repo   : centos-sclo-rh
Summary     : Package that installs rh-python36
License     : GPLv2+
Description : This is the main package for rh-python36 Software Collection.

Run the below special SCL command to enable the installed package version at the shell:

# scl enable rh-python35 bash

Run the below command to check the installed python3 version:

# python --version
Python 3.6.9

Run the following command to get a list of SCL packages that have been installed on the system:

# scl -l
rh-python36

Note: You can’t use this version of Python after you get out of this bash. If you have any doubts about this, run the Python version command again.

Method-2 : How to install Python3 in CentOS 6 using EPEL Repository

EPEL stands for Extra Packages for Enterprise Linux maintained by Fedora Special Interest Group.

They create, maintain, and manage a high quality set of additional packages for Enterprise Linux, including, but not limited to, Red Hat Enterprise Linux (RHEL), CentOS, Scientific Linux (SL), and Oracle Linux (OL).

EPEL Repository packages are usually based on their Fedora counterparts, and will never conflict with or replace packages in the base Enterprise Linux distributions.

EPEL package is included in the CentOS Extras repository and enabled by default, so we can install this by running the below command:

# yum install epel-release

Run the following command to check the available Python 3 version in the EPEL repository:

# yum --disablerepo="*" --enablerepo="epel" list python3*

Run the below command to install the latest available python 3 package from the EPEL repository:

# yum --disablerepo="*" --enablerepo="epel" install python34

By default it does not install the applicable pip and setuptools, and you must install them from the URL below with the curl command :

# curl -O https://bootstrap.pypa.io/get-pip.py
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1603k  100 1603k    0     0  2633k      0 --:--:-- --:--:-- --:--:-- 4816k

# /usr/bin/python3.4 get-pip.py
Collecting pip
  Using cached https://files.pythonhosted.org/packages/0f/74/ecd13431bcc456ed390b44c8a6e917c1820365cbebcb6a8974d1cd045ab4/pip-10.0.1-py2.py3-none-any.whl
Collecting setuptools
  Downloading https://files.pythonhosted.org/packages/8c/10/79282747f9169f21c053c562a0baa21815a8c7879be97abd930dbcf862e8/setuptools-39.1.0-py2.py3-none-any.whl (566kB)
    100% |████████████████████████████████| 573kB 4.0MB/s
Collecting wheel
  Downloading https://files.pythonhosted.org/packages/1b/d2/22cde5ea9af055f81814f9f2545f5ed8a053eb749c08d186b369959189a8/wheel-0.31.0-py2.py3-none-any.whl (41kB)
    100% |████████████████████████████████| 51kB 8.0MB/s
Installing collected packages: pip, setuptools, wheel
Successfully installed pip-10.0.1 setuptools-39.1.0 wheel-0.31.0

Run the below command to check the installed Python 3 version using EPEL repository:

# yum --enablerepo=epel info python34

Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
 * base: centos.mirrors.estointernet.in
 * centos-sclo-rh: centos.mirrors.estointernet.in
 * centos-sclo-sclo: centos.mirrors.estointernet.in
 * epel: repos.del.extreme-ix.org
 * extras: centos.mirrors.estointernet.in
 * updates: centos.mirrors.estointernet.in
Installed Packages
Name        : python34
Arch        : x86_64
Version     : 3.4.10
Release     : 4.el6
Size        : 31 k
Repo        : installed
From repo   : epel
Summary     : Version 3 of the Python programming language aka Python 3000
URL         : http://www.python.org/
License     : Python
Description : Python 3 is a new version of the language that is incompatible with the 2.x
            : line of releases. The language is mostly the same, but many details, especially
            : how built-in objects like dictionaries and strings work, have changed
            : considerably, and a lot of deprecated features have finally been removed.

Run the below command to check installed python3 version:

# python3 --version
Python 3.4.10

Note: This isn’t the default Python version on your system. To make it the default Python version, you need to run the update-alternatives command as described at the bottom of this page.

Method-3 : How to install Python3 in CentOS 6 using IUS Community Repository

IUS Community is a CentOS Community Approved third-party RPM repository which contains latest upstream versions of PHP, Python, MySQL, etc.., packages for Enterprise Linux (RHEL & CentOS) 5, 6 & 7.

IUS Community Repository have dependency with EPEL Repository so we have to install EPEL repository prior to IUS repository installation. Follow the below steps to install & enable EPEL & IUS Community Repository to RPM systems and install the packages.

EPEL package is included in the CentOS Extras repository and enabled by default, so we can install this by running the below command:

# yum install epel-release

Download IUS Community Repository Shell script:

# curl 'https://setup.ius.io/' -o setup-ius.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1914  100  1914    0     0   6563      0 --:--:-- --:--:-- --:--:--  133k

Install/Enable IUS Community Repository:

# sh setup-ius.sh

Run the following command to check the available Python 3 version in the IUS repository:

# yum --disablerepo="*" --enablerepo="ius" list python3*

Run the below command to install the latest available Python 3 package from IUS repository:

# yum --enablerepo=ius install python36u

Check the available python 3 version:

# yum --enablerepo=ius info python36u

Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
 * base: centos.mirrors.estointernet.in
 * centos-sclo-rh: centos.mirrors.estointernet.in
 * centos-sclo-sclo: centos.mirrors.estointernet.in
 * epel: repos.del.extreme-ix.org
 * extras: centos.mirrors.estointernet.in
 * updates: centos.mirrors.estointernet.in
Available Packages
Name        : python36u
Arch        : x86_64
Version     : 3.6.8
Release     : 1.el6.ius
Size        : 57 k
Repo        : ius
Summary     : Interpreter of the Python programming language
URL         : https://www.python.org/
License     : Python
Description : Python is an accessible, high-level, dynamically typed, interpreted programming
            : language, designed with an emphasis on code readability.
            : It includes an extensive standard library, and has a vast ecosystem of
            : third-party libraries.
            : 
            : The python36u package provides the "python3.6" executable: the reference
            : interpreter for the Python language, version 3.
            : The majority of its standard library is provided in the python36u-libs package,
            : which should be installed automatically along with python36u.
            : The remaining parts of the Python standard library are broken out into the
            : python36u-tkinter and python36u-test packages, which may need to be installed
            : separately.
            : 
            : Documentation for Python is provided in the python36u-docs package.
            : 
            : Packages containing additional libraries for Python are generally named with
            : the "python36u-" prefix.

Run the below command to check installed python3 version:

# python36 --version
Python 3.6.8

How to change the default Python version on Linux

When you install a new Python version using the above methods, the new version of Python will not become your default Python version, until you perform the steps below.

Use the following command to find the absolute path of Python binary packages:

# which python
/usr/bin/python

Now, you can see the list of installed Python versions by running the ls command as follows. This clearly shows which Python version is linked to which one:

# ls -lh /usr/bin/python*

-rwxr-xr-x. 2 root root 8.9K Aug 18  2016 /usr/bin/python
lrwxrwxrwx. 1 root root    6 Jun 30  2018 /usr/bin/python2 -> python
-rwxr-xr-x. 2 root root 8.9K Aug 18  2016 /usr/bin/python2.6
lrwxrwxrwx. 1 root root    9 Dec 19 13:20 /usr/bin/python3 -> python3.4
-rwxr-xr-x. 2 root root 6.0K Oct  5 01:11 /usr/bin/python3.4
-rwxr-xr-x. 2 root root 6.0K Oct  5 01:11 /usr/bin/python3.4m
lrwxrwxrwx. 1 root root   18 Dec 19 13:33 /usr/bin/python36 -> /usr/bin/python3.6
-rwxr-xr-x. 2 root root 6.7K Aug 10 12:24 /usr/bin/python3.6
-rwxr-xr-x. 2 root root 6.7K Aug 10 12:24 /usr/bin/python3.6m

To get the default Python version, run the following command:

# python --version
Python 2.6.6

How to change default Python version for a specific user on Linux

To change a python version for each user, follow the below procedure.

Create an alias for specific version of python in the user’s “.bashrc” file:

echo "alias python='/usr/bin/python3.4'" >> /home/u1/.bashrc

Run the following command to make the above changes work:

# source /home/u1/.bashrc

Now, check your default python version.

$ python --version
Python 3.4.10

How to change the default Python version system-wide on Linux

Alternatives creates, removes, maintains and displays information about the symbolic links comprising the alternatives system.

To do so, run the command below as root user. The following commands include the Python version below in the conversion table:

# update-alternatives --install /usr/bin/python python /usr/bin/python3.4 1
# update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2

Now, check your default python version:

$ python --version
Python 3.6.9

You can list all python alternatives by running the below command:

# update-alternatives --display python
python - status is manual.
 link currently points to /usr/bin/python3.6
/usr/bin/python3.4 - priority 1
/usr/bin/python3.6 - priority 1
Current `best' version is /usr/bin/python3.4.

Also, you can switch to the other Python version whenever you need by running the command below:

# update-alternatives --config python

There are 2 programs which provide 'python'.

  Selection    Command
-----------------------------------------------
   1           /usr/bin/python3.4
*+ 2           /usr/bin/python3.6

Enter to keep the current selection[+], or type selection number: 

Conclusion

In this tutorial you’ve learned how to:

  • Check which version of Python 3, if any, is installed on your system
  • Install the latest version of Python 3 on Linux

If you found this article helpful, please do share with your friends and spread the knowledge. Please feel free to comment below if you have any queries/concerns. We will get back to you as soon as we can. Happy learning!

About Prakash Subramanian

Prakash Subramanian is a Linux lover and has 3.5+ years of experience in linux server administration with major Linux distribution such as (RHEL, CentOS, Ubuntu). He is currently working as a Senior L2 Linux Server administrator.

View all posts by Prakash Subramanian

2 Comments on “How to install the latest Python 3.x in CentOS 6”

Leave a Reply

Your email address will not be published. Required fields are marked *