How do I install python imaging library (PIL)?

How do I install the python imaging library (PIL) on Ubuntu?

I tried doing

sudo apt-get install python-imaging

but now when i run python selftest.py (a script I got from somewhere on the http://pythonware.com/products/pil/ website) I get (among other warning messages):

...
*** JPEG support not installed
*** ZLIB (PNG/ZIP) support not installed
...
*** 1 tests of 57 failed.

Have I somehow messed up the PIL -- how do I fix that?

Is maybe the PIL just fine, but that "selftest.py" is not really the right program for checking to see if PIL is installed properly -- how else can I tell if PIL is installed properly or not?

(I'm using Ubuntu 12.04 LTS "Precise Pangolin").

(What I'm ultimately trying to do is to add a 2D barcode generator to some python code, and all the 2D barcode generators I could find that were written in python all seemed to use the PIL).


The above solution did not work for me on Ubuntu 12.10 as libjpeg was not available in the repository.

What did end up working for me was:

sudo apt-get build-dep python-imaging
sudo apt-get install libjpeg62 libjpeg62-dev

If you get the error "You must put some 'source' URIs in your sources.list" then make sure that your /etc/apt/sources.list has deb-src entries which match your deb entries.

Then you must symlink the files from their actual location on your server to the location where PIL expects them.

32-bit version

sudo ln -s /usr/lib/i386-linux-gnu/libz.so /usr/lib/libz.so
sudo ln -s /usr/lib/i386-linux-gnu/libjpeg.so /usr/lib/libjpeg.so
sudo ln -s /usr/lib/i386-linux-gnu/libfreetype.so /usr/lib/libfreetype.so

64-bit version

sudo ln -s /usr/lib/x86_64-linux-gnu/libz.so /usr/lib/libz.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib/libjpeg.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libfreetype.so /usr/lib/libfreetype.so

Finally, pip install PIL

Success!

enter image description here


Update Sep 2014

Pillow is a more modern fork of PIL.

#jpeg support
sudo apt-get install libjpeg-dev
#tiff support
sudo apt-get install libtiff-dev
#freetype support
sudo apt-get install libfreetype6-dev
#openjpeg200support (needed to compile from source)
wget http://downloads.sourceforge.net/project/openjpeg.mirror/2.0.1/openjpeg-2.0.1.tar.gz
tar xzvf openjpeg-2.0.1.tar.gz
cd openjpeg-2.0.1/
sudo apt-get install cmake
cmake .
sudo make install
#install pillow
pip install pillow

Something similar happened to me, I solved this way

sudo apt-get install libjpeg libjpeg-dev libfreetype6 libfreetype6-dev zlib1g-dev

And try there installing via pip install PIL.

More on what pip is can be found here. In short is a convenient (and becoming a standard) way of installing python libraries.

if it continues to fail, it can be due to PIL searching those libraries in a different path.

It turns out that the APT installations put the libraries under /usr/lib/x86_64-linux-gnu and PIL will search for them in /usr/lib/. So you have to create symlinks for PIL to see them.

Try to see if libjpeg and libz libs exist in /usr/lib/x86_64-linux-gnu and make a symlink this way

sudo ln -s /lib/x86_64-linux-gnu/libz.so.1 /lib/
sudo ln -s /usr/lib/x86_64-linux-gnu/libfreetype.so.6 /usr/lib/
sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so.62 /usr/lib/

Source: http://jj.isgeek.net/2011/09/install-pil-with-jpeg-support-on-ubuntu-oneiric-64bits/


I just want to add, that pip install pil no longer works, at least on my machine, you have to do

pip install PIL --allow-external PIL --allow-unverified PIL


The above answers create links for x64 libraries in x86 locations. Instead I would download the PIL source then add these lines in setup.py:

    add_directory(library_dirs, "/usr/lib/x86_64-linux-gnu")
    add_directory(library_dirs, "/lib/x86_64-linux-gnu")

after these lines (~line 211)

    # standard locations
    add_directory(library_dirs, "/usr/local/lib")
    add_directory(include_dirs, "/usr/local/include")

    add_directory(library_dirs, "/usr/lib")
    add_directory(include_dirs, "/usr/include")

reinstall PIL. If you're using pip:

pip uninstall PIL

Then from the source directory run:

python setup.py install