Friday, September 18, 2009

Install Python 2.6.2 on CentOS 5.3

Normally I don't use CentOS but rather Ubuntu since I like the shiny new stuff. Unfortunately for me, though, my work gave me some new processing machines with the provision I use CentOS. CentOS is great in that it's rock-solid stable. It sucks because to get that stability the software versions are rarely updated except for back-ports of security fixes. This isn't much of an issue unless you need some features existing in newer versions. What sucked is my current projects take full advantage of the multiprocessing library in python 2.6. This was a bit of a pain to figure out.

For example, CentOS 5.3 has python 2.4 which is hella old. It works in general, but if your project requires the use of something like the multiprocessing library (available in python 2.6) then you're hosed. Furthermore, just upgrading CentOS to 2.6 will significantly break the system. Bunches of things are tightly bound to version 2.4, really important stuff like yum for instance.

The way around this is to install python 2.6 along side 2.4 and adjust your bash environment variables to look there. Once that's established you can take advantage of all sorts of tasty things like multiprocessing and virtualenv. Since it was a nuisance to figure out, I'm gonna go ahead and document the steps here. Much of this is swiped from Villa Road with some alterations determined from my own experience and some other sites I've already forgotten.

Step 1: login as root
ssh root@yourbox.com
cd


Step 2: Install the extra packages for enterprise linux repository. It adds some previously unavailable packages and also provides some updates to existing repo packages
rpm -ivh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm


Step 3: Install development tools, ssl and zlib. These development tools will allow us to build python properly as well as aide setuptools when using easy_install later in this tutorial.
yum groupinstall 'Development Tools'
yum install openssl-devel* zlib*.x86_64


Step 4: Download sqlite, python and setuptools
wget http://www.sqlite.org/sqlite-amalgamation-3.6.18.tar.gz
wget http://www.python.org/ftp/python/2.6.2/Python-2.6.2.tgz
wget http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c9-py2.6.egg


Step 5: Build and Install SQLite
cd
tar zxvf sqlite-amalgamation-3.6.18.tar.gz
cd sqlite-3.6.18/
./configure
make
make install


Step 6: Build and install Python 2.6.2. Now, there are some important things to discuss here. First and foremost we have given the option –prefix=/opt/python2.6. This option installs the python binaries and the python library in /opt/python2.6 (it will make the dir for us) rather than in /usr/local/ which would, as we stated above, replace the standard python interpreter and inherently be bad juju. The /opt directory in redhat based distributions is a directory provides a home for larger, mostly custom built, binaries and applications.

Also, we made sure that the interpreter is going to make use of multiple threads by adding the –with-threads option. The –enable-shared option just allows python to be embedded into other apps.
cd
tar xfz Python-2.6.2.tgz
cd Python-2.6.2
./configure --prefix=/opt/python2.6 --with-threads --enable-shared
make
make install


Step 7: Now we need to make sure all our users can use the new python. To do this, we will need to add a couple of aliases and an addition to the $PATH to each users .bash_profile. This file is kept in the home directory of each user (eg: /home/rwilson/.bash_profile)
su - root
cd
nano .bash_profile
# add the following lines to the bottom of the file
alias python='/opt/python2.6/bin/python'
alias python2.6='/opt/python2.6/bin/python'
PATH=$PATH:/opt/python2.6/bin
# 'ctrl + o' to save the file and 'ctrl+x' to close the file

# now do the same for every other user, like this:
nano /home/rwilson/.bash_profile
alias python='/opt/python2.6/bin/python'
alias python2.6='/opt/python2.6/bin/python'
PATH=$PATH:/opt/python2.6/bin


Step 8: Now we need to update BASH so that it knows about the new shared libraries that we have put on the system. Create a symlink to them and then reload the cache of the shared libraries

su -
cd
cat >> /etc/ld.so.conf.d/opt-python2.6.conf
/opt/python2.6/lib #hit 'enter' and then 'ctrl+d'
ldconfig


Step 9: Now that bash is aware of our new libraries and such, let's go ahead and make /opt/python2.6 writable by everyone. Normally this may be a no no, but it was the only way I could make a clean break from the system library when it came time to install setuptools.

chmod -R a+w /opt/bin/python2.6


Step 10: At this point I chose to close out my terminal and bring it back up, then log back in as a non-root user to be certain my .bash_profile was loaded correctly. There is most certainly another way to do it, but I'm lazy and know this works fine. Then I checked my python version (which should be 2.6.2) and went from there

#do this after logging out, closing the terminal, bringing the terminal back up, and logging back in as non-root user
which python
# should say:
# alias python='/opt/python2.6/bin/python'
# /opt/python2.6/bin/python

python -V
# should say:
#Python 2.6.2


Step 11: OK now we're all good to go with install setuptools to our new python

cd
wget http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c9-py2.6.egg
sh setuptools-0.6c9-py2.6.egg


Step 12: And finally we make a symlink for our shared library

cd /opt/python2.6/lib/python2.6/config
ln -s ../../libpython2.6.so .


Now you can use easy_install for grabbing things like numpy for your shiny new Python 2.6.2 =)

The next article will be about getting virtualenv setup and running. That one should be fairly universal for most distributions. Then later we'll be looking at building BLAS, ATLAS, FFTW and friends from source so they take advantage of our specific hardware. =)

No comments:

Post a Comment