How to install Python3 on CentOS

Question: I am trying to install a Python app that requires python3 on CentOS system. However, my CentOS only has python 2.X installed, but not python 3.X. How can I install python3 on CentOS?

As of the latest CentOS 7, the default Python version still remains python 2.7, and python3 is not available in base repositories. If you need to use python3 as part of Python application dependency, there are several ways to install python3 on CentOS.

Method One: Build and Install Python3 from the Source

You can always build python3 from its source manually. Since you can choose the version of python3 to install, this is the surest way to meet Python dependency requirement.

Here is how you can build and install python3 from the source.

First, install minimum necessary tools:

$ sudo yum install yum-utils

Then using yum-builddep, set up a necessary build environment for python3 and install missing dependencies. The following command will automatically take care of that.

$ sudo yum-builddep python

Now download the latest python3 (e.g., python 3.5) from https://www.python.org/ftp/python/

$ curl -O https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz

Finally, build and install python3 as follows. The default installation directory is /usr/local. If you want to change this to some other directory, pass "--prefix=/alternative/path" parameter to configure before running make.

$ tar xf Python-3.5.0.tgz
$ cd Python-3.5.0
$ ./configure
$ make
$ sudo make install

This will install python3, pip3, setuptools as well as python3 libraries on your CentOS system.

$ python3 --version
Python 3.5.0

If you want to use python3 as your default Python interpreter, you can define the following alias in your .bashrc.

alias python='/usr/local/bin/python3.5'

Method Two: Install Python3 from EPEL Repository

The latest EPEL 7 repository offers python3 (python 3.4 to be exact). Thus if you are using CentOS 7 or later, you can easily install python3 by enabling EPEL repository as follows.

$ sudo yum install epel-release

Then install python 3.4 and its libraries using yum:

$ sudo yum install python34

Note that this will not install matching pip. To install pip and setuptools, you need to install them separately as follows.

$ curl -O https://bootstrap.pypa.io/get-pip.py
$ sudo /usr/bin/python3.4 get-pip.py

Method Three: Install Python3 from Software Collections (SCL)

Another way to install python3 is via enabling Software Collections (SCL) repository. The SCL repository is available for CentOS 6.5 or later, and the latest SCL offers python 3.3. Once you enable the SCL repository, go ahead and install python3 as follows.

$ sudo yum install python33

To use python3 from the SCL, you need to enable python3 on a per-command basis as follows.

$ scl enable python33 <command>

You can also invoke a bash shell with python3 enabled as the default Python interpreter:

$ scl enable python33 bash
Download this article as ad-free PDF (made possible by your kind donation): 
Download PDF

Subscribe to Ask Xmodulo

Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.


Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

12 thoughts on “How to install Python3 on CentOS

  1. For Method #1 -- one suggestion: if you plan on having multiple copies of python running, try this sequence instead:

    ./configure --prefix=/usr/local --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
    make
    sudo make altinstall

    The "altinstall" is the key flag, and will write python to /usr/local/bin/python3.5 rather than overwriting "python3"

  2. Rather than modifying your .bashrc (which might be a problem if used on a shared $HOME dir for example) consider using the "alternatives" command on the system (available for Debian and Redhat derivatives). You can set the default version used for all users systemwide while still allowing other versions to be run as needed by any user.

  3. The "environment-modules" package also allows each user to load a module which will properly set env vars to use various versions of any app/program.
    Centos 7 package:
    environment-modules.x86_64 : Provides dynamic modification of a user's environment

    I believe it is also available as a different package name in Debian derivatives; try something like "apt-cache search modules" to locate it in the repos.
    Sorry, no easy way to find it on deb at the moment.

  4. HI! thank you for the help! As for me, in Method One, alias in bashrc worked only like "alias python='/usr/local/bin/python3.5'" (additional slash before "usr"). May be it will help somebody. Thank you!

Leave a comment

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