CentOS 6 ships with Python 2.6.6 and several critical system utilities, for example
yum, will break if the default Python interpreter is upgraded. The trick is to install new versions of Python in
/usr/local so that they can live side-by-side with the system version.
Execute all the commands below as root either by logging in as root or by using
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.
yum groupinstall“Development tools”
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
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. In Python 3.4 the Unicode support has been completely rewritten and strings are automatically stored using the most efficient encoding possible.
You should probably compile Python as a shared library. If you compile Python as a shared library you must also tell it how to find the library. Our option:
- Compile the path into the executable by adding this to the end of 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.
Download, compile and install Python
Here are the commands to download, compile and install Python.
# Python 3.4.1:
tar xf Python-3.4.1.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.4. The system version of Python 2.6.6 will continue to be available as
Setuptools + pip
Setuptools has replaced Distribute as the official package manager used for installing packages from the Python Package Index. Setuptools and pip are installed with Python 3.4.1. It builds on top of Setuptools and provides a few extra functions that are useful when you manage your packages.
# With pip installed you can now do things like this:
The packages will end up in
X.Y is the Python version).
Since you are using Python 3.4 you don’t need to install 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
# Use the built-in pyvenv program in Python 3.4 to create a sandbox called my34project:
# Activate the my34project sandbox and check the version of the default Python interpreter in it:
# This will show Python 3.4.1
When you use pyvenv to create a sandbox you must install setuptools and pip inside the sandbox. You can reuse the ez_setup.py file you downloaded earlier and just run it after you activate your new sandbox.
Most Linux distributions come with wget.
Download ez_setup.py and run it using the target Python version. The script will download the appropriate version and install it for you:
> wget https://bootstrap.pypa.io/ez_setup.py -O - | python
Note that you will may need to invoke the command with superuser privileges to install to the system Python:
> wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
Alternatively, Setuptools may be installed to a user-local path:
> wget https://bootstrap.pypa.io/ez_setup.py -O - | python - --user
If your system has curl installed, follow the wget instructions but replace wget with curl and -O with -o. For example:
> curl https://bootstrap.pypa.io/ez_setup.py -o - | python
For more advanced installation options, such as installing to custom locations or prefixes, download and extract the source tarball from Setuptools on PyPIand run setup.py with any supported distutils and Setuptools options. For example:
setuptools-x.x$ python setup.py install --prefix=/opt/setuptools
Use --help to get a full options list, but we recommend consulting the EasyInstall manual for detailed instructions, especially the section on custom installation locations.