Configure and compile Python with Zlib 25

I just spent an hour trying to figure this out, AGAIN! To compile Zlib support into your Python interpreter, do this:

1. Install the Zlib-dev package for your distribution. (you probably have zlib already, but not the dev package)
On Ubuntu it’s called ‘zlib1g-dev’, on others it might just be: zlib-dev or zlib-devel. Do a search on your distro package lists to find it.
You should end up with a ‘zlib.h’ file in your system. In mine, Ubuntu, I have /usr/include/zlib.h

2. Get your python sources to compile and run the configure script like so:
./configure –with-zlib=/usr/include

See there that I included the ‘/usr/include’ directory to find zlib.h in? After the make && make install it worked great!

Keep in mind your mileage may vary, but this might help I hope. More importantly, now I can remember how I did this! :)


MINOR UPDATE (3-29-2013):

Its been almost 8 years since I wrote this, and people still don’t have zlib in python by default. I noticed several comments below about some distros requiring a change in python/modules/setup to add the zlib module.  Below is the text from comment below by “kernc” (about 2 years ago):

./Python2.5/Modules/Setup and uncomment the line starting with:
zlib zlibmodule.c …

I don’t seem to have this in Ubuntu, so not sure which distro’s requires this.


  • Originally posted by Nico:
    It worked !!!! if you were a girl I would kiss you

    thnx mate

    • sriram

      Thanks for the posting Nico!! Helped greatly..

  • many thanx

    just wasted half a day on this myself – another pointer for those having trouble re-installing, make sure to unset your PYHTONHOME and PYHTONPATH env vars … son of a …

  • Thanks! This was driving me nuts :-/

  • Leo

    Thanks!! I did a clean install of Python 2.6.5 and was getting

    ImportError: No module named zlib

    but then I followed your instructions, reinstalled python, and now it works.

  • Kernc

    nothing worked for me until I manually edited ./Python2.5/Modules/Setup and uncommented the line starting with:
    zlib zlibmodule.c …

    quite sad. :S

    • jc

      That was the only thing that worked for me too

  • Matthew Rapaport

    I tried the above, also un-commenting the zlib line in Modules/Setup, but while the configure ran, I end up with the message  “unrecognized options: –with-zlib” So still no luck getting this to run. Strange that the Python that appears to ship with this box (2.4.2) already has zlib built in (I can “import gzip” while this later downloaded version (2.7.2) does not, or at least it doesn’t work when installed…

    This is not my forte.

    • Anonymous

      Yeah, maybe on python 2.7 its done differently. But on my reply to another comment, it occurred to me that the whole deal with 64bit systems may cause some problems. Like, if you have 32bit python, you’ll need 32bit libs intsalled for zlib too, right? Just a thought.

      Do you need your own compiled version? Or can you just install your distro’s package of it? (which *is* easier, IMHO)

  • Anonymous

    whooaaa, man!  I almost threw my computer out the window on this!!

    I’m on CentOs, so I installed zlib-dev this way:
    yum install zlib-devel.x86_64

    then, I un-commented #zlib in the Setup file of Python

    then, I did: (note that I had the same error as Matthew with the with-zlib thing, so I just ommited it, to see what would happen)
    make install

    from within the Python folder (classic installation)

    then I tried to import zlib, and it worked!

    such a complicated process, I can’t believe this…


    • Anonymous

      Well I am glad you got it to work! I posted this years ago. I kept having to deal with this for a few years and decided to post a “how-to” to myself. Funny thing is, I haven’t had to deal with it since! LOL! I just use the packages, and haven’t had a problem. But I suppose depends on software you are using and versions you need, plus these 64bit systems and libs complicate things a little.

    • josecho

      Thank you very much, you’re a great man. I was paralyzed but now my work continuesThanks

  • Mult

    In point 2, do you mean to run python with
    ./configure –with-zlib=/usr/include 
    Im just trying to get this right

  • Thanks – worked for me. Pretty annoying how this isn’t shown as an option in ./configure –help. 

  • Anonymous

    thank you very much , saved me lots of time

  • Shweta Ramdas

    I’ve been looking for a solution to this on OpenSuse for ages, and this worked like a song. Thank you so much!

  • thankyou thankyou thankyou
    I am on red hat set up by central services, so they insist on not using yum, but rug instead. I need to install MySQLdb which needs setuptools which needs zlib. The sticking block has been zlib, which was installed, but python 2.6 would not find. All kinds of links later, and I got the ./configure option here.

    Seeing as zlib is essential, you would think this would be a default. Sheesh!

    • gregthegeek

      Wow, glad I could help, but I had been wondering why I am still getting traffic to this topic/page in 2012, so many years after I wrote the post. I can’t believe there are distro’s without this added still by default. Well, you answered my question at least partially, so I know where people are still running into this issue. Thanks! And glad it helped!

  • Simon

    Thanks for the tip, this was driving me nuts with Python 3.3 for the past hour or so. God I love the internet!

  • kayal

    Really helpful information..! Thanks a lot!!

  • nevermind

    for the record, this page was still useful to me today :) (fix in Modules/Setup) Thanks!

    • gregthegeek

      Awesome! I think I’ll put up a note in my original post about that Modules/Setup part and point them to this section of the comments. Seems to be a big hang up point for people still. Just out of curiosity, what OS / Dist are you running that requires this? CentOS?

  • David Huttleston Jr

    Wow, your MINOR UPDATE just saved my bacon. Cheers!

  • Nate

    Hey, thanks for the help. My solution was slightly different only because I was trying to install python on a remote computer which I cannot access as a root user AND has no access to the internet. Here is my slightly modified solution:

    1. Extract python (ie: tar -xzf Python-3.6.4.tgz)
    2. Make a directory to house the local python (ie: mkdir localpy)
    3. Navigate into the Modules/zlib folder in the extracted python directory (ie: cd ./Python-3.6.4/Modules/zlib)
    4. Configure zlib locally (ie: ./configure –prefix=’/usr/h/testing/localpy’ )
    5. Make and install zlib (make install)
    6. Naviage back to the extracted python folder (ie: cd ../../ )
    7. Configure python locally and point to zlib (ie: ./configure –prefix=’/usr/h/testing/localpy’ –with-zlib=’/usr/h/testing/localpy’)
    8. Make python (ie: make)
    9. Install python (ie: make install)

    This may not work for everyone, but it worked for me today.

    • Greg Fischer

      Nice! Thanks for sharing! I can’t believe this is still an issue for people and is still helpful on this site, after 13 years! Sounds like you had extra challenges to deal with too. :)