Saturday, April 2, 2011

Installing Multiple Versions of Python on Linux

The Google Appengine SDK requires at least version 2.5 of python, or else it spits the dummy. To compound matters, Google has announced that version 1.4.3 of the SDK is the last to support python 2.5, and all newer versions will require python 2.6. [Update: and now you need python 2.7 for multi-threaded apps.]

So how do you install later versions of python without impacting the system default version - which is required for all sorts of vital system services?


The trick is to use the prefix and altinstall options when building the new version of python. Make sure you have libsqlite3-dev and libssl-dev installed or Sqlite3 and SSL support won't be compiled in.

cd ~/src
wget http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tar.bz2
tar -jxvf Python-2.7.2.tar.bz2
rm Python-2.7.2.tar.bz2
cd Python-2.7.2/
./configure --prefix=/opt/python2.7
make
sudo make altinstall
sudo ln -s /opt/python2.7/bin/python2.7 /usr/bin/python2.7


You (and the system) will continue to use the old version by calling python, and you can use the new version by calling python2.7.

So to install sqlite3 support for python2.7 for instance:
cd ~/src
wget http://pysqlite.googlecode.com/files/pysqlite-2.6.3.tar.gz
tar -xzf pysqlite-2.6.3.tar.gz
rm pysqlite-2.6.3.tar.gz
cd pysqlite-2.6.3/
sudo python2.7 setup.py build_static install

6 comments:

  1. Exactly what I was looking for, easy and simple, cheers!

    ReplyDelete
  2. Exactly what I was looking for(2)

    thx a lot

    ReplyDelete
  3. Greg, thanks for guide. How would you uninstall it correctly? If doesn't affect to the system it's OK just wipe out link and python2.7 folder, right?

    ReplyDelete
  4. Removing the source directory doesn't get rid of that version of Python - the cleanest way to do that is using the make tool to uninstall.

    Go back to the source directory (~/src/Python-2.7.2/ in the example above) and enter "sudo make uninstall". Then you can remove the source directory, and that version of Python is now history.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Thanks a lot!
    Only one remark: for sqlite3 in Python2.7.5 my system (Debian 6) required installing libsqlite3-dev.

    ReplyDelete