Ubuntu 9.10: Install PIL in Virtualenv
I started with a –no-site-packages virtualenv, so as not to use (or more importantly depend) on any of the global site-packages. Ok, cool.
$ virtualenv --no-site-packages myEnv
First, I needed to install the python developer tools. (Use apt-get or aptitude, whatever floats your boat)
$ sudo aptitude install python-dev
Then, I needed to install libjpeg and libjpeg-dev. I’m not sure why, but I needed libjpeg simply doesn’t exist, so I needed to install libjpeg62. I can’t pretend that I know the difference (or if there is one). In fact, I may have gotten away with installing libjpeg62 and libjpeg-dev (rather than both “62? versions… libjpeg62 and libjpeg62-dev), but only further testing will tell.
Why? If you install PIL without this library, you’ll get those wonderful “decoder jpeg not available” messages in Python. Or worse yet, if you’re trying to use it in a Django, you may get some errors (specifically the “Upload a valid image. The file you uploaded was either not an image or a corrupted image” warning), or you may not get any until you open the shell. Either way, you can test with the method listed below. If you get the “decoder jpeg not available” message, your install didn’t work.
Check version installed of libjpeg:
lsof | grep libjpeg
The zlib package handles PNGs.
$ sudo aptitude install libjpeg62 libjpeg62-dev $ sudo aptitude install zlib1g-dev $ sudo aptitude install libfreetype6 libfreetype6-dev
Alright, now we seem to be done with the prerequisites. Start your virtualenv (of course, myEnv in the example is the name of your virtualenv).
$ source myEnv/bin/activate
Download PIL and install. This will make sure to install PIL within your virtualenv’s site-packages.
(myEnv)$ wget http://effbot.org/downloads/Imaging-1.1.7.tar.gz (myEnv)$ tar zxvf Imaging-1.1.7.tar.gz (myEnv)$ cd Imaging-1.1.7 (myEnv)$ python setup.py install
If you run into further problems (the “decoder jpeg not available” message again), you may have to resort to the long directions to get PIL and libjpeg to play happily together, but I hope not.
Now that you have everything installed, test it. Open up a python shell from within your virtualenv.
Now try the following (with an image in your home directory) to see if everything is running smoothly.
>>> from PIL import Image >>> i = Image.open('/home/username/someJpeg.jpg') >>> i.save('/home/username/someOtherJpeg.jpg')
If all of that works, you should now be ready to work.
Note: I would love to install the jpeg, freetype and zlib packages locally as well, but that was a step beyond what I was willing to mess with. Maybe for a future set of instructions.
In Ubuntu 11, it seems that you need libjpeg8-dev, rather than libjpeg62. I have seen cases where multiple of PIL version are installed, under pyshared / pymodules / dist-packages. So PIL may work in the command line but not in mod_wsgi. It is important that only one version of PIL is running and preferably the one that work!