I'm trying to compile a 32 bit version of python on a 64 bit ubuntu system with the following configure command:

CC="gcc -m32" LDFLAGS="-L/lib32 -L/usr/lib32 \ -Lpwd/lib32 -Wl,-rpath,/lib32 \
    -Wl,-rpath,/usr/lib32" \ ./configure --prefix=/opt/pym32

then make, make install. No errors, but it should be something wrong because a "readelf -h python" tells me that python was build as a ELF64 instead.

  • 1
    you are "readelf'ing" the correct python - are you picking up the default 64bit python ? i.e. which python
    – fossfreedom
    Sep 5, 2011 at 10:51
  • yep :( i used an absolutepath to readelf: readelf -h /opt/pym32/bin/python
    – Gabriele B
    Sep 5, 2011 at 11:32
  • Is using chroot an option? Sep 5, 2011 at 16:52
  • yes, actually it is...i was just wondering for a one-line solution
    – Gabriele B
    Sep 6, 2011 at 10:39
  • 1
    Here is chroot instructions for chroot'ed Python: opensourcehacker.com/2010/08/03/… Sep 10, 2011 at 18:14

3 Answers 3


Use "--build" and "--host".

./configure --help
System types:
  --build=BUILD     configure for building on BUILD [guessed]
  --host=HOST       cross-compile to build programs to run on HOST [BUILD]

You need to use

./configure --build=x86_64-pc-linux-gnu --host=i386-pc-linux-gnu

to compile for 32-bit Linux in a 64-bit Linux system. You still need to add the other ./configure options.

  • 1
    tnx a lot! it works perfectly :)
    – Gabriele B
    Nov 18, 2011 at 8:13
  • "configure: error: readelf for the host is required for cross builds". Using "locate '*-readelf'" gives only "/usr/bin/x86_64-linux-gnu-readelf". I guess I need to find a package to install that includes i686-linux-gnu-readelf" ? Jan 8, 2016 at 2:30
  • This is how I bypassed the readelf error: sudo apt-get install binutils-multiarch, sudo ln -s /usr/bin/readelf /usr/bin/i386-linux-gnu-readelf And then ./configure --build=x86_64-linux-gnu --host=i386-linux-gnu ...
    – Alex Che
    Jul 25, 2019 at 21:09

This is how I managed to build and side-install 32-bit Python 3.5.2 on Ubuntu 16, having 64-bit version already installed by apt:

# Some prerequisites:
sudo apt install libffi-dev:i386
sudo apt install libssl1.0.0:i386
sudo apt install libssl-dev:i386
sudo apt-get install libncurses-dev:i386 libreadline-dev:i386 # for command history to work in Python terminal
sudo apt-get install binutils-multiarch
sudo ln -s /usr/bin/readelf /usr/bin/i386-linux-gnu-readelf
# Going to be installed to /opt/python-3.5.2-i386
ac_cv_file__dev_ptc=no ac_cv_file__dev_ptmx=yes \
LDFLAGS="-m32 -L/lib32 -L/usr/lib32 -L/lib/i386-linux-gnu -L/usr/lib/i386-linux-gnu -Wl,-rpath,/lib32 -Wl,-rpath,/usr/lib32 -Wl,-rpath,/lib/i386-linux-gnu -Wl,-rpath,/usr/lib/i386-linux-gnu" \
./configure --build=x86_64-linux-gnu --host=i386-linux-gnu \
--enable-ipv6 --prefix=/opt/python-3.5.2-i386 --with-system-ffi
sudo make install
# The following may be needed to build some additional extensions, which are statically 
# built-in in existing python3 intallation on host and won't be built otherwise
sudo PATH=/opt/python-3.5.2-i386/bin:$PATH make
sudo PATH=/opt/python-3.5.2-i386/bin:$PATH make install

After that:

~$ python3 -V
Python 3.5.2
~$ python3 -c 'import struct; print(struct.calcsize("P") * 8)'
~$ /opt/python-3.5.2-i386/bin/python3 -V
Python 3.5.2
~$ /opt/python-3.5.2-i386/bin/python3 -c 'import struct; print(struct.calcsize("P") * 8)'

If you want the compile to be debuged, you should post (in a pastebin) your verbose output from gcc. Otherwise, it's impossible to know.

The suggested solution of using chroot may help you and Mikko kindly offers a link to guide users in how to use chroot for 32bit python on a 64bit machine:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .