39

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).

3
  • 2
    You might want to try using the PIL in an application, there might just be a problem with selftest.py
    – Aaron Hill
    Jun 26, 2012 at 23:28
  • 1
    In 18.04 it looks like you just do sudo apt-get install python-pil or sudo apt-get install python3-pil. Aug 31, 2018 at 12:27
  • ^ Definitely, sudo apt-get install python-pil just worked for me on raspbian.
    – alecxe
    Dec 13, 2018 at 18:21

7 Answers 7

31

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
3
  • 1
    Works for me on 13.04 with Pillow, without symlinking. Oct 29, 2013 at 5:23
  • 2
    worked for me too (14.04 LTS 64-bit, unmodified from above): sudo apt-get build-dep python-imaging sudo apt-get install libjpeg62 libjpeg62-dev
    – Peter Teoh
    Jan 11, 2015 at 10:39
  • I just needed "apt-get build-dep python-imaging" Apr 9, 2017 at 10:54
27

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/

5
  • This did not work for me on Ubuntu 12.04 64bit.
    – Gus E
    May 22, 2013 at 20:03
  • 1
    if it fits your needs, you could try 'pillow'. It is a drop in replacement for PIL.
    – Hernantz
    May 23, 2013 at 15:05
  • 8
    Whenever possible, use pillow. It's essentially PIL but actually maintained. Oct 9, 2013 at 19:39
  • Package zlib1g-dev is not available, but is referred to by another package. This may mean that the package is missing, has been obsoleted, or is only available from another source E: Unable to locate package libjpeg E: Unable to locate package libjpeg-dev E: Unable to locate package libfreetype6-dev E: Package 'zlib1g-dev' has no installation candidate
    – axolotl
    May 22, 2016 at 7:35
  • The python-imaging package in Ubuntu 12.04 LTS is linked to the JPEG library, and the changelog mentions multiarch breaking that prior to July 2011. Maybe this selftest.py had been broken because of that, too, but that didn't necessarily affect anything else. Jan 4, 2017 at 12:16
14

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

3
  • 6
    Try pip install Pillow instead of pip install pill (the latter does not exist)
    – aclark
    Feb 7, 2015 at 21:41
  • @Lynob this is the same as PIL? and is suppoerted by TkInker?
    – 3kstc
    Jun 15, 2016 at 4:41
  • @3kstc yes it is
    – Lynob
    Jun 15, 2016 at 5:55
4

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
4

sudo apt-get install python-imaging now installs Pillow not PIL. As for selftest.py, you may need to check out the source to run it. And of course, you need to install the dependencies if you want PIL to support them e.g.

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

To run a version of PIL newer than is included in Ubuntu::

pip install Pillow
2

This solution work for me on Ubuntu 14.04 as libjpeg was not available in the repository.

What did end up working for me

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

I needed to pip install Pillow in a virtualenv on 14.04 (not using system packages).

To build pillow from PyPI inside a virtualenv (Python 2.7):

$ sudo apt-get build-dep pillow
$ virtualenv venv
$ source venv/bin/activate
(venv)$ pip install pillow

... then you can import PIL.

You must log in to answer this question.

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