Compiling 32-bit Python on amd64

If you ever feel the need to compile a 32-bit version of Python on a amd64 bit machine, this is how you do it on a Debian/Ubuntu system.

Firstly you need the correct compiler stuff, this means you need gcc-multilib and 32-bit development libraries of at least libc. On Debian/Ubuntu installing the gcc-multilib package will pull in most if not all of the required dependencies.

Next is invoking the configure script of Python. Sadly Python is one of those autoconf using projects who advertise the use of environment variables like CFLAGS in the --help output of ./configure but don't actually respect them, this is all too common for autoconf-but-no-automake using projects. So the correct way to start building is using the OPT environment variable instead of CFLAGS:

OPT=-m32 LDFLAGS=-m32 ./configure --prefix=/opt/pym32

You may want to finetune the OPT value, since this is where normally -g -O3 etc appears in so you've just got rid of those. I'm not quite convinced of this design but anyway.

Now you can watch the compilation, depending on your machine you may have time to make a cup of tea or so. Near the end you'll see something like this:

Failed to build these modules:
_hashlib           _sqlite3           _ssl
_tkinter           bz2                gdbm

This is pretty much exactly the same as when normally compiling python: go find the packages that provide the development libraries needed for these modules. Only this time you need to look for the 32-bit variety of these. On Debian/Ubuntu look out for packages named like lib32foo-dev. After installing all applicable ones I could find this is what I got it down too in the end (only using system packages):

Failed to build these modules:
_hashlib           _sqlite3           _ssl
_tkinter           gdbm

Just in case you aren't quite happy with your achievement so far you could now try compiling an extension module against your 32-bit python:

LDFLAGS=-m32 /opt/pym32/bin/python build

Now wasn't this useful? Silly binary-only libraries...