Yet Another How to Install PIL on OS X in a virtualenv Recipe

15 05 2011

The Python Library PIL can sometimes be challenging to install in OS X.

Installing PIL in a virtualenv can also add another layer of complexity to the task.

The need for this recipe arose from the fact that although our development and deployment environments at work are Linux, we frequently have the need to get a “local” environment running for our front-end developers and designers that are running OS X.

I’m posting it on the chance that it might be useful to others that need to get a workable virtualenv based environment on OS X setup that can be built to parallel their Linux environments and aren’t afraid to build some components from source.

Characteristics of this approach:

  • Doesn’t interfere with the Python build provided in OS X.
  • Doesn’t install software into system directories.
  • Doesn’t require “root” or “admin” level privileges.
  • Does allows for as many different builds/versions of Python as one needs.
  • Does require building Python and the relevant supporting libraries from source.

In this approach we’ll build a copy of Python and the necessary supporting libraries that we can install in a “local” directory of choice. In other words instead of installing into the /usr/local directory we’ll install into another specified directory (e.g. ~/local).

In order to build PIL with the necessary support for JPEG and FreeType we’ll also patch the setup.py file in the PIL source code to reference our new local installation of the libraries.

There are four (4) parameters in the bash script below that can be easily modified to meet one’s specific needs.

The script assumes that one will run it in the directory that one wants to use as the parent directory for the software to be built and installed (e.g. something like ~/ProjectName) .

The script will create a ./local , ./distfiles , ./src and a ./ve directory below the parent directory.

The example script below utilizes Python 2.7.1 but should work equally well with the appropriate changes for other Python versions.

Xcode must be installed.

#!/bin/bash
set -v
# ############################################################################
# script to build a python environment that
# includes PIL for OSX that can be easily used
# in a virtualenv
#############################################################################
# PARAMETER 1
# LOCAL_INSTALL_DIR sets the primary directory
# for the build to be installed into
#
LOCAL_INSTALL_DIR=`pwd`/local; export LOCAL_INSTALL_DIR
#
mkdir local
mkdir distfiles
mkdir src
mkdir ve
cd distfiles
curl -O http://www.python.org/ftp/python/2.7.1/Python-2.7.1.tgz
curl -O http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.6.1.tar.gz
curl -O http://www.ijg.org/files/jpegsrc.v8c.tar.gz
curl -O http://mirror.its.uidaho.edu/pub/savannah/freetype/freetype-2.4.4.tar.gz
curl -O http://effbot.org/media/downloads/Imaging-1.1.7.tar.gz
cd ..
cd src
tar xvfz ../distfiles/Python-2.7.1.tgz && \
tar xvfz ../distfiles/virtualenv-1.6.1.tar.gz && \
tar xvfz ../distfiles/jpegsrc.v8c.tar.gz && \
tar xvfz ../distfiles/freetype-2.4.4.tar.gz && \
tar xvfz ../distfiles/Imaging-1.1.7.tar.gz
# #################################################################################
#
cd ./jpeg-8c
./configure --prefix=$LOCAL_INSTALL_DIR && make && make install
cd ..
#
cd ./freetype-2.4.4/
./configure --prefix=$LOCAL_INSTALL_DIR && make && make install
cd ..
#
#############################################################################
#
LDFLAGS=-L$LOCAL_INSTALL_DIR/lib ; export LDFLAGS
#
cd Python-2.7.1/
./configure --prefix=$LOCAL_INSTALL_DIR && make && make altinstall
cd ..
#
PATH=$LOCAL_INSTALL_DIR/bin:$PATH; export PATH
#
cd virtualenv-1.6.1/
python2.7 setup.py install
cd ..
#
#############################################################################
#
LDFLAGS=-L$LOCAL_INSTALL_DIR/lib ; export LDFLAGS
#
cd Imaging-1.1.7/
cp setup.py.orig setup.py
cp setup.py setup.py.orig
sed "s@JPEG_ROOT = None\$@JPEG_ROOT = \'$LOCAL_INSTALL_DIR\'@" setup.py > setup.new
sed "s@FREETYPE_ROOT = None\$@FREETYPE_ROOT = \'$LOCAL_INSTALL_DIR\'@" setup.new > setup.new2
cp setup.new2 setup.py
python2.7 setup.py install
cd ..
cd ..
# #############################################################################
#
# PARAMETER 2
# LOCAL_VE_DIR sets the primary directory
# for the virtualenv to be installed into
#
# PARAMETER 3
# VIRTUALENV_NAME sets the name of the
# virtualenv to be created
#
# PARAMETER 4
# VIRTUALENV_DIR sets the primary directory
# for the individual virtualenvs to be installed into
#
LOCAL_VE_DIR=`pwd`/ve; export LOCAL_VE_DIR
VIRTUALENV_NAME=test_virtualenv1; export VIRTUALENV_NAME
VIRTUALENV_DIR=$LOCAL_VE_DIR/$VIRTUALENV_NAME; export VIRTUALENV_DIR
#
virtualenv $VIRTUALENV_DIR && cd $VIRTUALENV_DIR && source bin/activate && ./bin/easy_install pip
#

Hopefully this will be of some help to others.





pywebdev for Ubuntu v9.10 released

31 10 2009

I’ve posted a release of the pywebdev Python Web Developer package for Ubuntu that has been updated for compatibility with the recently released Ubuntu v9.10 Karmic Koala .

Version 2.6 (updated to the recently released 2.6.4) is the default Python environment in Karmic.   For compatibility and flexibility pywebdev also includes complete environments for Python versions 2.4 and 2.5.

A small number of packages have changed since the v9.04 release:

Packages Added in v9.10
=========================
libevent-1.4-2
libevent-dev
vim-gnome
vim-gtk
vim-nox

Packages Removed in v9.10
=========================
linux-headers-server
libevent1
pypy
pypy-dev
python-xml
python-libgmail
zope3
zope-common
vim-python




New Django hosting option available

4 09 2009

A recently launched startup named Kutoken has begun to offer Django hosting.

Per Kutoken home page:

Kutoken is a first-class platform for hosting Django applications. We’ve taken the knowledge we’ve acquired building big clusters for large organisations with deep pockets, and made it accessible to all.

If you’re a perfectionist with a deadline, Kutoken is the logical choice.

I’m glad to see the options available for easily deploying Django applications increase. It is an encouraging sign for the future of Django that companies like Kutoken feel confident building a businesses whose success is based on Django’s continued adoption and use.





pywebdev for Ubuntu v9.04 released

19 04 2009

I’ve posted a release of the pywebdev Python Web Developer package for Ubuntu update for the soon to be released v9.04-Jaunty Jackalope.

Version 2.6 (updated to the recently released 2.6.2) is the default Python environment in Ubuntu v9.04.   For compatibility and flexibility pywebdev also includes complete environments for Python versions 2.4 and 2.5.

A small number of packages have changed since the v8.10 release:

Packages Added in v9.04
=========================
python2.6
python2.6-dev
python2.6-examples
python2.6-minimal
python2.6-doc
python-cherrypy3
python-sphinx 
libapache2-mod-python-doc
pypy
pypy-dev
python-django-lint
python-git

Packages Removed in v9.04
=========================
python-urljr




An example of the Agile Cloud in action

3 04 2009

An example of the Agile Cloud.

“You can build prototypes in the time it takes to have a meeting”
Simon Willison – Open Platform launch

This is phrase which sticks in my mind from Tuesday and I make no apologies for including it in 2 blog posts in a day. It has to be said that I don’t want ever to be in a 7 hour meeting (that was roughly how long ContentTagger took to build). However it’s still quite a quick turnaround and it got some extra niceness from a couple of “watercooler” moments (although one of them may have added on a couple of hours).

I definitely have attended meetings that have lasted at least seven hours.

The prototype was built using the default Google App Engine framework, Django templates and the Python client library for OpenPlatform.

A nice combination of Google App Engine, Python, and jQuery.





Tim Bray on the Future of the Web

24 03 2009

An interesting interview with Tim Bray on The Future of the Web.

The topics covered are fairly wide ranging with the more interesting questions and answers in the last two thirds of the interview.

One of my favorite web frameworks Django is mentioned favorably several times.

I completely agree with his position regarding REST/WOA as being the architectural pattern that matters today.

On the topic of cloud computing Mr. Bray talks about the alignment of agile methods, open source, and cloud computing working to deliver “monetization at the point of value”. This seems to be a recurring theme.

He poses three questions concerning adoption of the cloud computing.

  1. What is the right level of abstraction for the cloud?
  2. How can one avoid vendor lock-in?
  3. How can one ensure security is handled appropriately?

Noting that the economics of cloud computing are compelling but the above questions need to be addressed.

His comments about enterprise adoption of what is currently today’s startup technology platforms is also interesting.








Follow

Get every new post delivered to your Inbox.