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.13 and Python 3.6.2, 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.13 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:
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.
You should compile Python as a shared library. All modern Linux distros ship with Python compiled as a shared library, and there are third-party tools such as mod_wsgi and Blender that won’t work without it. To make sure the executable can find its shared library you need to pass some additional flags to the configure command (
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.
# Start by making sure your system is up-to-date:
# 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.13:
tar xf Python–2.7.13.tar.xz
./configure —prefix=/usr/local —enable–unicode=ucs4 —enable–shared LDFLAGS=“-Wl,-rpath /usr/local/lib”
make && make altinstall
# Python 3.6.2:
tar xf Python–3.6.2.tar.xz
./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/python3.6. The system version of Python 2.6.6 will continue to be available as
You might also want to strip symbols from the shared library to reduce the memory footprint.
# Strip the Python 2.7 binary:
# Strip the Python 3.6 binary:
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
# First get the script:
# Then execute it using Python 2.7 and/or Python 3.6:
# 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
X.Y is the Python version).
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
# 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:
# This will show Python 2.6.6
# Activate the my27project sandbox:
# Check the Python version in the sandbox (it should be Python 2.7.13):
# Deactivate the sandbox:
# Activate the my36project sandbox:
# Check the Python version in the sandbox (it should be Python 3.6.2):
# Deactivate the sandbox:
- Examples updated with Python 3.6.2.
- Examples updated with Python 3.6.1.
- Add warning about not installing Python 2.7 on CentOS 7 (since it already has 2.7 in the base system).
- 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.
- Examples updated with Python 3.3.5.
- 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.