blog

Compiling Python with Readline support on Ubuntu Linux

| categories: python, compile, readline, cmmi, ubuntu

Compiling Python on Ubuntu is often necessary for development purposes and for specific versions. Here I'll show you in a few easy steps how to install your own Python instance into a folder in your home directory.

There are various advantages to compiling your own Python version, instead of using the system's installed version.

Portability is one of them. By having a python instance in my home folder, I can easily install different versions of Ubuntu and just copy over the python files, thereby immediately and easily setting up my previous python environment.

Additionally, Zope versions prior to 2.12 require Python 2.4, which is not available on newer versions of Ubuntu.

Unfortunately, Readline support (i.e provides nifty things like the ability to see your command history via the up arrow) is not enabled by default when compiling your own custom python in Ubuntu. To be able to compile python and enable readline support, we

need to install some additional libraries.

jc@ubuntu:~$ sudo aptitude install build-essential libreadline5-dev

Now it's time to download and extract the version of Python you would like to install.

jc@ubuntu:~$ mkdir -p ~/src/python
jc@ubuntu:~$ cd ~/src/python/
jc@ubuntu:~/src/python$ wget http://www.python.org/ftp/python/2.4.4/Python-2.4.4.tar.bz2
jc@ubuntu:~/src/python$ tar xjf Python-2.4.4.tar.bz2
jc@ubuntu:~/src/python$ cd Python-2.4.4/

We need to enable readline support by modifying the Setup.dist file. The following one-liner will do the trick:

jc@ubuntu:~/src/python/Python-2.4.4$ sed 's/#readline/readline/' Modules/Setup.dist > TMPFILE && mv TMPFILE Modules/Setup.dist

Next comes cmmi (configure, make, make install). I like to install Python in my home directory and would recommend it to anyone else. Hence the '--prefix' argument.

jc@ubuntu:~/src/python/Python-2.4.4$ ./configure --prefix=/home/jc/python/python-2.4.4/ && make && make install

Lastly, I make a sym-link to the new binary to make sure it's in my execution PATH:

jc@ubuntu-12:59:~/$ ln -s /home/jc/python/python2.4/bin/python2.4 ~/bin/

You should now be able to call the python interpreter from the command line:

jc@ubuntu-13:01:~/bin$ python2.4
Python 2.4.6 (#2, Aug 19 2009, 17:13:56)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

If you receive a "command not found" error, make sure that your ~/bin directory is in your PATH by editing your .bashrc (note the leading '.') file. For example, in my .bashrc file, I have the following PATH declaration:

PATH=$PATH:/home/jc/bin

That's it! Best of luck with your new python install.