Monday, September 5, 2011

How to Compile Python from Source

Here we are going compile python from source. I assume you have a clean installation of Debian testing. Here are few packages required for compilation.
apt-get -y install build-essential zlib1g-dev libbz2-dev \
    libncurses5-dev libreadline-gplv2-dev libsqlite3-dev \
    libssl-dev libgdbm-dev
Once above installation is complete, download python source code from here: Suppose you choose to download python 2.5.2.
cd /usr/local/src
tar xjf Python-2.5.2.tar.bz2
cd Python-2.5.2
Since most of libraries in Debian moved from /usr/lib to /usr/lib/i386-linux-gnu we need create symbolic links in old location so the build scripts can find them all. This is far easier than specify a valid library location for each case. Here are links:
ln -s /usr/lib/i386-linux-gnu/ \
ln -s /usr/lib/i386-linux-gnu/ \
ln -s /usr/lib/i386-linux-gnu/  \
ln -s /usr/lib/i386-linux-gnu/  \
ln -s /usr/lib/i386-linux-gnu/  \
ln -s /usr/lib/i386-linux-gnu/  \
ln -s /usr/lib/i386-linux-gnu/  \
ln -s /usr/lib/i386-linux-gnu/  \


Before we start compile we need to configure it first. You can run it with all defaults (this will install python to /usr/local/).
Or you can specify some other location:
./configure --prefix=/usr/local
The configuration process take few seconds. Next issue make command to actually compile it (this may take few minutes). The -s option prints warning only and -j 2 utilizes 2 CPU cores during the compilation).
make -s -j 2
You can optionally test it before installing with:
make test
or run specific tests of your interest:
./python Lib/test/


make install
Python executable should be located at /usr/local/bin/python2.5.

Extra Packages

While python is perfectly working at this moment you might need install some extra packages (e.g. virtualenv) with easy_install.
wget -O - -q | python2.5
easy_install-2.5 virtualenv
This way you can install as many python versions as you like.


While working with some third party package (e.g. django) you got the following error:
ImportError: ...undefined symbol: PyUnicodeUCS2_Replace
There reason is described here. You have to re-configure the python:
./configure --enable-unicode=ucs4
and build/install it again.

1 comment :

  1. AttributeError: 'NoneType' object has no attribute 'rfind'. This issue in Debian can be fixed by adding '/usr/lib/i386-linux-gnu/' to lib_dirs list in