Thursday 31 May 2012

Webfaction - How to Install PIL with jpeg, free type, littlecms and zlib support.

Newer versions of virtualenv don’t include the system wide site-packages folder (which is a good thing) so if you’re using a virtualenv you’ll have to install your own copy of PIL. The problem is that by default pip install PIL on webfactions servers won’t provide support for jpeg, png, freetype or littlecms. Maybe you can live without freetype and littlecms, but PIL is essentially useless without support for jpg or png. This blog post outlines how to install your own version of PIL with full support for all the things.

Before I get stuck in I want to point out that the this post was a great help to me credit has to go to seanf. What follows in this post is very close to what he suggested. The main difference in this post is that in my web faction .bashrc file I always alias python=python2.7 so in the below commands I’m implicitly using python2.7. In seanf’s solution he’s explicitly stating python2.7.

Check Python Version

Type which python at the command line and you should see the /usr/local/python2.7 as part of the output. If you don’t then you’ll want to alias python=python2.7 in your .bashrc file.

Download PIL

Grab the source for PIL, extract it and get into the directory –

 mkdir ~/src cd ~/src wget http://effbot.org/media/downloads/PIL-1.1.7.tar.gz tar -xzvf PIL-1.1.7.tar.gz cd PIL-1.1.7 

Edit setup.py

PIL won’t install with support for jpeg, zlib, freetype and littlecms because it can’t find the setup.py script can’t find the libraries. We have to give it a little hand, so crack open setup.py in vim (vim setup.py) and go to line 36(ish) and specify the library paths manually –

 TCL_ROOT = None JPEG_ROOT = '/usr/lib64','/usr/include' ZLIB_ROOT = '/lib64','/usr/include' TIFF_ROOT = None FREETYPE_ROOT = '/usr/lib64','/usr/include/freetype2/freetype' LCMS_ROOT = None 

When you’ve done that save and quit out of VIM.

Build it

Time to run setup.py and build it –

 python setup.py build_ext -i 

You should be greeted with a lot of output, the most pleasing of which be this –

 ----------------------------------------------------- PIL 1.1.7 SETUP SUMMARY ----------------------------------------------------- version       1.1.7 platform      linux2 2.7.3 (default, May 18 2012, 14:51:16)               [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] ----------------------------------------------------- --- TKINTER support available --- JPEG support available --- ZLIB (PNG/ZIP) support available --- FREETYPE2 support available --- LITTLECMS support available ----------------------------------------------------- 

Don’t do what I did at this stage, which is forget to install the compiled code and then spend half an hour wondering why all ImageFields in django where returning an error complaining about corrupted images. The battle isn’t over yet soldier, keep going …

Install It

If you’re using virtualenv double check that your virtual environment is activated. If it isn’t do that now before proceeding –

 workon yourprojectnamehere 

Now install it –

 python setup.py install 

And now your done.

Credits

This post was heavily inspired by seanf’s solution posted on the webfaction community.