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.

About these ads

Actions

Information

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




Follow

Get every new post delivered to your Inbox.

%d bloggers like this: