(guest@joequery.me)~ $ |

[SOLUTION] Ubuntu Python 2.6 No module named zlib

The issue

Certain important Python libraries require the zlib module. By default, Python2.6 does not include zlib as part of the standard library without some extra work.

If you're on a relatively new Ubuntu version but still need to maintain backwards compatability with a Python2.6 program, shared library directory changes in Ubuntu can lead to conflicts.

Prerequisites

Ensure the following packages are installed

$ sudo apt-get install zlib1g zlib1g-dev

Download the latest source of Python2.6 (which is Python 2.6.9 at the time of this article)

~$ cd /tmp
/tmp$ wget "https://www.python.org/ftp/python/2.6.9/Python-2.6.9.tgz"
/tmp$ tar -xf Python-2.6.9.tgz

Solution

This post on UbuntuForums outlines the issues with Python2.6 compatibility with newer Ubuntu versions.

It is Ubuntu's "fault". They changed where shared libraries are placed in
order to support multiple architectures better (eg both x86 and x86_amd64 on the
same machine) in a way that diverges from how it has always been done in the
past.
...
...
Upstream Python is being patched, but that doesn't really help as they are only
doing it with current versions of Python

So we need to locate the libz shared library file and move it to a place where Python can locate it.

Locating the libz file

First, cd to the /lib directory

$ cd /lib

Now we will find the exact libz shared library file via the find command

/lib$ find . -name "libz.so.1"
./i386-linux-gnu/libz.so.1

My libz.so.1 file was located at ./i386-linux-gnu/libz.so.1. Your libz file may be in a different directory.

We will now create a symlink to libz.so.1 file that was returned by the find command above.

/lib$ sudo ln -s i386-linux/gnu/libz.so.1 libz.so

Recompile

Now head back over to the Python-2.6.9 directory we untarred earlier.

/lib$ cd /tmp/Python-2.6.9

Now we compile from source

/tmp/Python-2.6.9$ ./configure
/tmp/Python-2.6.9$ make && sudo make altinstall

Verification

/tmp/Python-2.6.9$ python2.6
Python 2.6.9 (unknown, Sep  6 2015, 20:22:36)
[GCC 4.8.2] on linux3
Type "help", "copyright", "credits" or "license" for more information.
>>> import zlib
>>>

Success! \o\ /o/ \o\ /o/ \o\ /o/ \o\ /o/

Tagged as python, ubuntu

Date published - September 06, 2015