This tutorial will show you how to install the latest version of Python 2 and Python 3 on CentOS by compiling from source. The examples below are for Python 2.7.14 and Python 3.6.3, but the procedure is the same for any modern version of Python.

If you are using CentOS 6 you can use this tutorial to install both Python 2.7.x and Python 3.6.x. For CentOS 7 only the Python 3.6.x instructions below are applicable. Warning! Do not install Python 2.7.14 on CentOS 7 using these instructions. Your system will end up having two different python2.7 binaries, each with its own package directory. This will likely cause difficult-to-diagnose problems.

This tutorial should work for all versions of CentOS 6 and CentOS 7, and it will probably work on the corresponding RHEL distributions as well. I have verified it on CentOS 6.9 64 bit and CentOS 7 (1611) 64 bit.

This tutorial is meant for people that are comfortable with compiling and installing applications from source.

What is the problem?

CentOS ships with Python as a critical part of the base system. Because it is a critical part it is not getting updated, other than to plug security vulnerabilities. The lack of updates means that CentOS 6 users are stuck with Python 2.6.6 released in August 2010, and CentOS 7 users are stuck with Python 2.7.5 released in May 2013.

Solving the problem

Utilities such as yum will break if the default Python interpreter is upgraded or replaced. The trick is to install new versions of Python in /usr/local (or some other non-standard location) so that they can live side-by-side with the system version.

Things to consider

Before you compile and install Python there are a few things you should know and/or consider:

Unicode

Python has a long and complicated history when it comes to Unicode support. Unless you have very specific reasons you should configure Python 2.7 to enable UTF-32 support. This increases memory usage but improves compatibility. In Python 3.3+ the Unicode support has been completely rewritten and strings are automatically stored using the most efficient encoding possible.

You enable UTF-32 in Python 2.7 by passing --enable-unicode=ucs4 to the configure command.

Shared library

You should compile Python as a shared library by passing --enable-shared to the configure command. All modern Linux distros ship with Python compiled as a shared library. It reduces memory usage if more than one Python process is running, and there are third-party tools that might not work properly without it. To make sure the executable can find its shared library you also need to pass some additional flags to the configure command ( LDFLAGS="-Wl,-rpath /usr/local/lib" ).

If you do not have sudo or root access you will probably not be able to compile Python as a shared library. If someone knows how to solve this please leave a comment below and I will update this text with instructions.

Use “make altinstall” to prevent problems

It is critical that you use make altinstall when you install your custom version of Python. If you use the normal make install you will end up with two different versions of Python in the filesystem both named python . This can lead to problems that are very hard to diagnose.

Preparations – install prerequisites

In order to compile Python you must first install the development tools and a few extra libs. The extra libs are not strictly needed to compile Python but without them your new Python interpreter will be quite useless.

Execute all the commands below as root either by temporarily logging in as root or by using sudo .

# Start by making sure your system is up-to-date: yum update # Compilers and related tools: yum groupinstall -y "development tools" # Libraries needed during compilation to enable all features of Python: yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel expat-devel # If you are on a clean "minimal" install of CentOS you also need the wget tool: yum install -y wget 1 2 3 4 5 6 7 8 # Start by making sure your system is up-to-date: yum update # Compilers and related tools: yum groupinstall - y "development tools" # Libraries needed during compilation to enable all features of Python: yum install - y zlib - devel bzip2 - devel openssl - devel ncurses - devel sqlite - devel readline - devel tk - devel gdbm - devel db4 - devel libpcap - devel xz - devel expat - devel # If you are on a clean "minimal" install of CentOS you also need the wget tool: yum install - y wget

Download, compile and install Python

Here are the commands to download, compile and install Python.

# Python 2.7.14: wget http://python.org/ftp/python/2.7.14/Python-2.7.14.tar.xz tar xf Python-2.7.14.tar.xz cd Python-2.7.14 ./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib" make && make altinstall # Python 3.6.3: wget http://python.org/ftp/python/3.6.3/Python-3.6.3.tar.xz tar xf Python-3.6.3.tar.xz cd Python-3.6.3 ./configure --prefix=/usr/local --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib" make && make altinstall 1 2 3 4 5 6 7 8 9 10 11 12 13 # Python 2.7.14: wget http : / / python .org / ftp / python / 2.7.14 / Python - 2.7.14.tar.xz tar xf Python - 2.7.14.tar.xz cd Python - 2.7.14 . / configure -- prefix = / usr / local -- enable - unicode = ucs4 -- enable - shared LDFLAGS = "-Wl,-rpath /usr/local/lib" make && make altinstall # Python 3.6.3: wget http : / / python .org / ftp / python / 3.6.3 / Python - 3.6.3.tar.xz tar xf Python - 3.6.3.tar.xz cd Python - 3.6.3 . / configure -- prefix = / usr / local -- enable - shared LDFLAGS = "-Wl,-rpath /usr/local/lib" make && make altinstall

After running the commands above your newly installed Python interpreter will be available as /usr/local/bin/python2.7 or /usr/local/bin/python3.6 . The system version of Python 2.6.6 will continue to be available as /usr/bin/python , /usr/bin/python2 and /usr/bin/python2.6 .

You might also want to strip symbols from the shared library to reduce the memory footprint.

# Strip the Python 2.7 binary: strip /usr/local/lib/libpython2.7.so.1.0 # Strip the Python 3.6 binary: strip /usr/local/lib/libpython3.6m.so.1.0 1 2 3 4 # Strip the Python 2.7 binary: strip / usr / local / lib / libpython2 . 7.so.1.0 # Strip the Python 3.6 binary: strip / usr / local / lib / libpython3 . 6m.so.1.0

Install/upgrade pip, setuptools and wheel

Each Python interpreter on your system needs its own install of pip, setuptools and wheel. The easiest way to install or upgrade these packages is by using the get-pip.py script.

# First get the script: wget https://bootstrap.pypa.io/get-pip.py # Then execute it using Python 2.7 and/or Python 3.6: python2.7 get-pip.py python3.6 get-pip.py # With pip installed you can now do things like this: pip2.7 install [packagename] pip2.7 install --upgrade [packagename] pip2.7 uninstall [packagename] 1 2 3 4 5 6 7 8 9 10 11 # First get the script: wget https : / / bootstrap .pypa .io / get - pip .py # Then execute it using Python 2.7 and/or Python 3.6: python2 . 7 get - pip .py python3 . 6 get - pip .py # With pip installed you can now do things like this: pip2 . 7 install [ packagename ] pip2 . 7 install -- upgrade [ packagename ] pip2 . 7 uninstall [ packagename ]

The packages will end up in /usr/local/lib/pythonX.Y/site-packages/ (where X.Y is the Python version).

What’s next?

If you are using Python 2.7 I strongly recommend that you install virtualenv and learn how to use it. Virtualenv makes it possible to create isolated Python environments. If you are using Python 3.3+ then you don’t need virtualenv because that functionality is already built in.

Each isolated Python environment (also called sandbox) can have its own Python version and packages. This is very useful when you work on multiple projects or on different versions of the same project.

Create your first isolated Python environment

# Install virtualenv for Python 2.7 and create a sandbox called my27project: pip2.7 install virtualenv virtualenv my27project # Use the built-in functionality in Python 3.6 to create a sandbox called my36project: python3.6 -m venv my36project # Check the system Python interpreter version: python --version # This will show Python 2.6.6 # Activate the my27project sandbox: source my27project/bin/activate # Check the Python version in the sandbox (it should be Python 2.7.14): python --version # Deactivate the sandbox: deactivate # Activate the my36project sandbox: source my36project/bin/activate # Check the Python version in the sandbox (it should be Python 3.6.3): python --version # Deactivate the sandbox: deactivate 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 # Install virtualenv for Python 2.7 and create a sandbox called my27project: pip2 . 7 install virtualenv virtualenv my27project # Use the built-in functionality in Python 3.6 to create a sandbox called my36project: python3 . 6 - m venv my36project # Check the system Python interpreter version: python -- version # This will show Python 2.6.6 # Activate the my27project sandbox: source my27project / bin / activate # Check the Python version in the sandbox (it should be Python 2.7.14): python -- version # Deactivate the sandbox: deactivate # Activate the my36project sandbox: source my36project / bin / activate # Check the Python version in the sandbox (it should be Python 3.6.3): python -- version # Deactivate the sandbox: deactivate

Changelog

2017-11-22 Updated the shared library paragraph with some extra information. 2017-10-08 Examples updated with Python 2.7.14 and 3.6.3. 2017-07-20 Examples updated with Python 3.6.2. 2017-05-24 Examples updated with Python 3.6.1. 2017-02-08 Add warning about not installing Python 2.7 on CentOS 7 (since it already has 2.7 in the base system). 2017-02-07 Examples updated with Python 2.7.13 and Python 3.6.0.

Mention that this also works for installing Python 3.6 on CentOS 7.

Added expat-devel to the list of prerequisites.

Removed ldconfig instructions.

Added instructions for stripping the shared libraries.

Changed the instructions for pip/setuptools to use the get-pip.py script. 2014-03-15 Examples updated with Python 3.3.5. 2014-02-16 The Python versions used in the examples have been updated to 2.7.6 and 3.3.4.

The list of library prerequisites has been extended so that more features are compiled into Python.

New parameters for compiling Python with a shared library and for enabling Unicode UTF-32 support in Python 2.7 and Python 3.2 have been added.

Instructions for installing and using setuptools, pip, virtualenv and pyvenv have been added/updated.

How to install the latest version of Python on CentOS