building Python from source with zlib support

I am using CentOS 6.6 and was recieving zlib errors. None of the other answers proposed here worked for me (including the fix for CentOS 6.3 of uncommenting a line in Modules/Setup). I have fixed it using the following commands.

yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

Then configuring and installing python as follows:

./configure --prefix=/usr/local LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall

I can now import zlib in /usr/local/bin/python2.7 with no problems.

These instructions are slightly modified from an article found here.


The solution is to install the Ubuntu package dpkg-dev.

sudo apt-get install dpkg-dev

The reason is explained here.

In short, recent versions of Ubuntu don't store libz.so in the standard /usr/lib location, but rather in a platform specific location. For example, on my system is is in /usr/lib/x86_64-linux-gnu. This prevents Python's build system from finding it.

The dpkg-dev package installs the dpkg-architecture executable, which enables Python to find the necessary libraries.

The original question was about Python 3.2.3. I also downloaded Python 2.7.3 and confirmed that the same problem exists, and this solution is applicable to it as well.


I had a similar problem on CentOS 6.3 and python 3.2.3

I solved it by:

Edit /Modules/Setup and uncomment the line:

zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz

change to directory /Modules/zlib:

./configure
make
sudo make install

then compiled my python3.2 source.

and was then able to test import zlib and it all worked fine :)