How to install the latest version of Python on CentOS

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

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 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  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 .

Shared library

You should compile Python as a shared library. All modern 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 (LDFLAGS="-Wl,-rpath /usr/local/lib").

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 either by temporarily logging in as or by using sudo.

, compile and install Python

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

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.

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.

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

Changelog

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.
Print Friendly, PDF & Email

Comments

comments

Bài viết liên quan

Để lại lời nhắn