How to install Python 2.7.6 on CentOS 6.3

How to install Python 2.7.6 on CentOS 6.3

(6.2 and 6.4 okay too, probably others)

CentOS 6.2 ships with Python 2.6.6 and depends on that specific version. Be careful not to replace it or bad things will happen. If you need access to a newer version of Python you must compile it yourself and install it side-by-side with the system version.

Here are the steps necessary to install Python 2.7.6. Execute all the commands below as root. Either log in as root temporarily or use sudo.

Install development tools

In order to compile Python you must first install the development tools:

yum groupinstall "Development tools"

You also need a few extra libs installed before compiling Python or else you will run into problems later when trying to install various packages:

yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel

Download, compile and install Python

The –no-check-certificate is optional

cd /opt
wget --no-check-certificate https://www.python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz
tar xf Python-2.7.6.tar.xz
cd Python-2.7.6
./configure --prefix=/usr/local
make && make altinstall

It is important to use altinstall instead of install, otherwise, you will end up with two different versions of Python in the filesystem both named python.

(Depending on your version of wget, you may need to add the –no-check-certificate option to the wget command line.)

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

Check with:

root@lg1:/opt/Python-2.7.6 ] ls -ltr /usr/bin/python*

lrwxrwxrwx 1 root root    6 Nov 16  2002 /usr/bin/python2 -> python
-rwxr-xr-x 1 root root 1418 Jul 10  2013 /usr/bin/python2.6-config
-rwxr-xr-x 2 root root 4864 Jul 10  2013 /usr/bin/python2.6
-rwxr-xr-x 2 root root 4864 Jul 10  2013 /usr/bin/python
lrwxrwxrwx 1 root root   16 Oct 24 15:39 /usr/bin/python-config -> python2.6-config

root@lg1:/opt/Python-2.7.6 ] ls -ltr /usr/local/bin/python*
-rwxr-xr-x 1 root root 6214533 Mar 19 22:46 /usr/local/bin/python2.7
-rwxr-xr-x 1 root root    1674 Mar 19 22:46 /usr/local/bin/python2.7-config

If things don't look right, you might need to create a symbolic link in /usr/local/bin

cd /usr/local/bin
ls -ltr python*

WARNING: don't do this before checking the $PATH for root. if it has /usr/local/bin before /usr/bin, it will see python2.7 first i.e.

root@openstack h2o]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

If you add this link, do a "which python" for the user and for root. If root is pointing to /usr/local/bin/python, remove the link you just added, and figure out something else.

ln -s /usr/local/bin/python2.7 /usr/local/bin/python

final check:

sudo sh
root@lg1:~ ] which python
/usr/bin/python
root@lg1:/home/0xdiag ] python
Python 2.6.6 (r266:84292, Jul 10 2013, 22:48:45) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
root@lg1:~ ] exit

sudo - user
user@lg1:~ ] which python
/usr/local/bin/python
user@lg1:~ ] python
Python 2.7.6 (default, Mar 19 2014, 22:45:29) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.