How To Install PIL On Ubuntu

picLet’s say you want to play around with images in Python. To do that, we need a Python package that can handle all the image manipulation. Python Imaging Library (PIL) is one of most popular libraries that is used to process the image data. Actually, people use Pillow now, which is a modern repackaged version of PIL. It has a lot of nice functionalities and it works well. Let’s see how you can install PIL on 64-bit Ubuntu 12.04.  

If you don’t have pip, install it using:

$ sudo apt-get install python-pip

With pip installed, install the required development packages:

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

Once you install these packages, we have to symlink the three image libraries into /usr/lib. Wait, now what is “symlink”? A symlink, which is short for symbolic link, is a special type of file that contains a reference to another file or directory. The reference is in the form of an absolute or relative path and it affects path-name resolution. To do that, type in the following commands on the terminal:

$ sudo ln -s /usr/lib/`uname -i`-linux-gnu/libfreetype.so /usr/lib/
$ sudo ln -s /usr/lib/`uname -i`-linux-gnu/libjpeg.so /usr/lib/
$ sudo ln -s /usr/lib/`uname -i`-linux-gnu/libz.so /usr/lib/

Now we are ready to install PIL. Type the following:

$ sudo pip install pil

To install Pillow (recommended), type the following:

$ sudo pip install Pillow

PIL (or Pillow) should now install with support for JPEGs, PNGs and FreeType, as indicated by the compilation output:

--------------------------------------------------------------------
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version 1.1.7
platform linux2 2.7.3 (default, Apr 19 2014, 18:10:34)
 [GCC 4.6.3]
--------------------------------------------------------------------
*** TKINTER support not available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
--- FREETYPE2 support available
*** LITTLECMS support not available
--------------------------------------------------------------------

I see some errors. What do I do?

Here is the solution to some of the common problems you might face.

Missing image libraries: If the image libraries are not installed and available in /usr/lib, you’ll see something like this:

--------------------------------------------------------------------
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version 1.1.7
platform linux2 2.7.3 (default, Apr 19 2014, 18:10:34)
 [GCC 4.6.3]
--------------------------------------------------------------------
*** TKINTER support not available
*** JPEG support not available
*** ZLIB (PNG/ZIP) support not available
*** FREETYPE2 support not available
*** LITTLECMS support not available
--------------------------------------------------------------------

To add a missing option, make sure you have the required library, and set the corresponding ROOT variable in the setup.py script.

Missing python headers: Make sure you have python-dev. Without it, you’ll see something that ends with the following:

...
running build_ext
building '_imaging' extension
creating build/temp.linux-x86_64-2.7
creating build/temp.linux-x86_64-2.7/libImaging

gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -IlibImaging -I/usr/include -I/usr/local/include -I/usr/include/python2.7 -c _imaging.c -o build/temp.linux-x86_64-2.7/_imaging.o

_imaging.c:75:20: fatal error: Python.h: No such file or directory

compilation terminated.

error: command 'gcc' failed with exit status 1

————————————————————————————————-

2 thoughts on “How To Install PIL On Ubuntu

  1. Hello,

    Thanks a lot for your article, that was very helpfull for a python newbie like me, because PIP and Pillow are painfull to be correctly installed on a Linux system – both Python 2.x and 3.X- (and libfreetype a bit too … 🙂 )

    BTW, are you sure your command lines are complete ? I mean the simlinks : in your command line, both symlinks are linked to /usr/lib, and I wonder how the mentionned libraries wil be found at runtime, since their names are not complete ? (maybe I misunderstood something … )

    FYI, on my machine I did :
    sudo ln -s /usr/lib/`uname -i`-linux-gnu/libfreetype.so /usr/lib/libfreetype.so
    sudo ln -s /usr/lib/`uname -i`-linux-gnu/libjpeg.so /usr/lib/libjpeg.so
    sudo ln -s /usr/lib/`uname -i`-linux-gnu/libz.so /usr/lib/libz.so

  2. The directory ‘/home/kunal/.cache/pip/http’ or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo’s -H flag.
    The directory ‘/home/kunal/.cache/pip’ or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo’s -H flag.
    Collecting pil
    Could not find a version that satisfies the requirement pil (from versions: )
    No matching distribution found for pil
    ***************************************************************************************************
    SHOWING THIS ERROR

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s