[Linux]: How to upgrade Python from 2.4 to 2.7 or higher from Source

Updated on September 2, 2017

Question: By default CentOS 6 comes with a Python version 2.4. For SQLMAP application I need to upgrade Python to version 2.6 and above. Even though I tried to upgrade it using yum, it still upgraded to a minor version of 2.4 and not 2.6 or later. Please help me how to upgrade it manually? Will it not conflict with the existing one?

Answer: It should be noted that before upgrading, you need to update several development kit like openssl-devel, sqlite-devel etc., So just do yum -y upgrade python would update the dependency softwares too.

Download Python:

You can also download the latest version of python here and follow the same procedure as below:

#wget -c https://www.python.org/ftp/python/2.7.6/Python-2.7.6.tgz --no-check-certificate

Unpack:

#tar -xvzf Python-2.7.6.tgz
#cd Python-2.7.6

Compile:

#./configure

Install Python:

#make && make install

Backup the old python executable:

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

Note: Though the above procedure has updated the python, but this has broken yum. You can test it out using the below command:

For example, if you try to install a package via yum command, say “yum install ntp” then you’ll may get an error as shown below:

#yum install ntp
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:

No module named yum

Please install a package which provides this module, or
verify that the module is installed correctly.

It's possible that the above module doesn't match the
current version of Python, which is:
2.7.6 (default, Dec  2 2015, 12:03:19)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-55)]

If you cannot solve this problem yourself, please go to
the yum faq at:
http://wiki.linux.duke.edu/YumFaq

This is because yum is dependent on Python2.4, but we made the default python to point to python2.7

The solution is to edit the 1st line of the file: /usr/bin/yum as to below:

#/usr/bin/python2.4

or Run the below command:

#sed -i 's/python/python2.4/g' /usr/bin/yum

That’s it, yum should too work as well.

Was this article helpful?

Related Articles

Leave a Comment