How to Upgrade Python on CentOS

3.7/5 - (7 votes)

If you’re running CentOS, you’re probably a few versions behind on Python.  Currently, the version packaged for CentOS 5 and 6 is 2.6.  Contrary to what the title of this post implies, you actually cannot safely upgrade Python on any Redhat distribution.  If you’re feeling brave, try this to see why:

yum remove python

Warning, do not pass a ‘-y’ to the above command.  All you’re doing here is viewing output.  Hit CTRL+C once it prompts to continue, and no changes will be made to your system.

If you ran the command above, you’ll see all the packages that depend on Python, most all of which will break (including Yum itself) if you alter the system-installed package or attempt to upgrade.  Therefore, if you need a newer version of Python, the only safe way to do this is to install it alongside the system version.

This example is for installing Python 2.7, but you can easily do similar steps for version 3.3, etc.  There are a couple ways to do this:

RPM

This is the best way to install additional versions of Python since their updates will be maintained automatically. First, install the EPEL repo:

https://ius.io/GettingStarted/

Then simply:

yum install python34*

The exact version may change depending on what version of CentOS you are running. The additional binaries will be in /usr/bin, for example:

/usr/bin/python34

From Source

Just download your preferred version from python.org.

cd /usr/src

wget http://python.org/ftp/python/2.7.6/Python-2.7.6.tgz

tar -xvzf Python-2.7.6.tgz

cd Python-2.7.6

./configure –prefix=/usr/local

make

make install

And that’s all that’s to it!  Now any scripts that require the alternate version should have the following shebang:

#!/usr/local/bin/python2.7

If you need to use pip to install modules for your alternate version, make sure to use the correct one:

/usr/local/bin/pip2.7

 

8 Comments

  1. Kenny Reply

    First off excellent article.

    One minor issue:

    ./configure –prefix=/usr/local

    looks like it is a single hyphen, when it should be a double hyphen! #fontproblems

    lol, just thought I would point that out in case any newbies had some trouble.

  2. Anonymous Reply

    I got distracted when i first came to your website, but then went back to it. Somehow i closed over the most important part of your wording. I ran the “yum remove python” and now 70+ low traffic websites are offline. I’ve never made a mistake this bad in my 17 years a system admin. How can i be so stupid?

    I’m having a bad day. Good news is, this gives me a reason to upgrade to CentOS 6.x rather than being stuck on on CentOS 5.8.

  3. J Davis Reply

    In your post you wrote:

    [code] ./configure –prefix=/usr/local [/code]

    THat’s a hypehn not a minus sign and it should be a double minus sign.

    [code] ./configure --prefix=/usr/local [/code]

  4. Pingback: Cassandra Basics | Site Title

  5. urano Reply

    Great information. If I need to run a website with Phyton, what I need to do? I want have scripts in any directory inside public_html

    I will create a ticket for this question inside your consultation system.

Leave a Reply

Your email address will not be published. Required fields are marked *

Log in