CentOS is a popular Linux distribution because it’s free to use without the need for a license and because the feature-freeze practices applied during the release cycle guarantee long-term stability.
The price for stability is that the operating system ships with certain libraries that are quite out of date. Such is the case for Python, which is found in CentOS distributions at versions 2.6 or even lower. If developers need a higher version of Python, they need to compile and install it themselves.
However, it’s important to leave the already installed Python intact, too. The operating system requires it for internal use, such as for the yum package manager. The most common thing that can go wrong is that a developer, in trying to install the new Python version, manages to corrupt the system (just Google for “installing Python 2.7 on CentOS broke my yum”).
For that reason, it makes sense to use an existing tool that manages Python installations without tampering with the system Python installed at
/usr/bin/python, and installs the new version side-by-side with the old one. A relatively new but already widely-used tool for managing Python installations is Anaconda by Continuum.io, which makes building Python quite easy.
– An updated server running CentOS 7.1 or 6.7. If you don’t have a CentOS server, or would like to test it out, you can spin up an Atlantic.Net server in seconds.
-wget and bzip2 installed. You can install them with
yum install wget bzip2
Installing Python on CentOS 7.1 or 6.7
First we need to choose a directory where the new Python binaries will be installed. In this tutorial, we’ll be using
/usr/local/miniconda. We can install Python 2.7 using the Miniconda bash installer and installing to the above directory.
Download the install script with the following command:
Run the install script with the following command: (you may or may not need sudo depending on the file access settings of the target directory)
sh Miniconda-latest-Linux-x86_64.sh -b -p /usr/local/miniconda
These commands will install the latest version of Miniconda for 64-bit versions of Linux. For other specific versions, check with the Miniconda repository.
Add Python 2.7 to the PATH
If it is desirable for us to launch the newly installed Python version when typing
python in the shell, then we need to add an export line to the
~/.bashrc file of all the users that have a need for this behavior (for instance, the user our web server runs as).
After editing the
.bashrc file, run the following to make the changes take effect immediately.
Users that don’t have this in their profiles will call system Python instead. Yum will always call system Python because the path
/usr/bin/python is hardcoded in its source code. So, unless we a try to create a symlink to this path or replace the binary, the package manager will keep working fine.
If we don’t want to override the
python command, we can also create aliases in
.bashrc for ‘python2.7’ and ‘python2.6’ (if your
.bashrc points to
.bash_aliases, you can add the aliases there instead). This method will require us to explicitly say which version we want to use every time.
If our server houses a django application, chances are we also need to install pip for managing our packages. The goal is to have pip install the new packages for Python2.7 installed in
/usr/local/miniconda and not for the system Python. It is possible to do this with one line (assuming we added python 2.7 in the path in the previous section).
conda install pip
To verify that it worked:
Congratulations on installing Python 2.7 on CentOS, and we thank you for following along this how-to! Please check back here for more updates, and fire up a cloud server from Atlantic.Net