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.





Intel Launches Nehalem-based Xeon Processors

3 04 2009

This week Intel officially launched their new Nehalem-based Xeon processors.

Nehalem-based processors for desktops were launched last year under the Core i7 brand.

Intel claims that in addition to offering significantly higher level of performance than the processors that they replace, the Nehalem systems will consume up to 20 per cent less energy.

The architecture boasts the following features:

  • Integrated memory controller supporting two or three memory channels of DDR3 SDRAM or four FB-DIMM channels.
  • Integrated graphics processor (IGP) located off-die, but in the same CPU package.
  • A new point-to-point processor interconnect, the Intel QuickPath Interconnect, replacing the legacy front side bus.
  • Simultaneous multithreading (SMT) by multiple cores and hyperthreading, which enables two threads per core.
  • Turbo Boost which allows the operating system to request more performance, and if the chip is within its thermal power constraints, it can ramp up the clock speed for each individual core.
  • Improved virtualization performance

This appears to be one of the most significant releases of an x86 architecture processor in the last several years.

The new processor interconnect (QPI) in and of itself in rectifies a long standing architectural weakness in Intel processors.

OpenSolaris and Solaris have already been optimized for the Nehalem and provide good examples of the improvements enabled by this new architecture.

I guess it is safe to declare that my current desktop dream machine is the new Nehalem-based Mac Pro.





Installing Ubuntu 9.04 beta Under VMware Fusion

28 03 2009

This morning I installed the recently released Ubuntu 9.04 Beta in virtual machine under VMware Fusion on OS X.

I usually download the “Alternate Install” ISO so that I have an image that will work for the widest variety of hardware and situations.

The installation went fine, but after the first boot following the installation the automatic installation of the VMware tools did not work and continued to fail. Initiating the “Install VMware Tools” function from the menu caused an ever increasing number of instances of Nautilus file browser windows to be opened. (Note: If this is happening to on your system use the “Cancel Install of VMware Tools” function from the menu.)

The first thing I tried was to run the Update Manager to apply any updates since the beta ISO was released. Sadly, despite applying about 165MB in updates the problem remained.

Next, I opened a Terminal session and ran the following command to ensure that I had an adequate minimal build environment for the installation of the tools. I thought that perhaps a build component was missing that VMware Tools was expecting to find.
$ sudo apt-get install build-essential

When that did not work I decided to try manually installing the tools.

I started by initiating the “Install VMware Tools” function from the VMware Tools menu, and then opened a Terminal window and performed the following actions:

  • $ df -v (to see the mount point of the virtual cdrom image that contains the install tools archive)
  • $ cd /media/cdrom (use the actual mount point on your system)
  • $ cp VMwareTools-7.9.3-146107.tar.gz ~/Desktop (use the actual filename on your system)
  • Clicked “Cancel the VMware Tools Installation” from the VMware Tools menu and went back to the Terminal
  • $ cd ~/Desktop
    $ tar xvfz VMwareTools-7.9.3-146107.tar.gz
    $ cd vmware-tools-distrib
    $ sudo ./vmware-install.pl
    and take the defaults to each question.

So far everything else seems to be functioning normally.

The next step is to begin working on a 9.04 release of pywebdev package.

Ubuntu v9.04 Beta under VMware Fusion

Ubuntu v9.04 Beta under VMware Fusion





Embedding Videos in a WordPress Post

5 03 2009

I wanted to embed a video in a post earlier today and a Google Search yielded a link to the VodPod bookmarklet makes the process very simple. Recommended.

P.S. It works in Safari.





Simple Script to check the battery on OS X

4 03 2009

Simple Script to check the battery on OS X.
#!/bin/bash
ioreg -l | grep -i IOBatteryInfo





Simple script to tell the Mail.app to prefer text over HTML

4 03 2009

Run the following script when the Mail.app (on OS X) is not running to set the preference to plain text over HTML.

#!/bin/bash
# false is the default for mail.app
#defaults write com.apple.mail PreferPlainText -bool false
set -v
defaults write com.apple.mail PreferPlainText -bool true





Simple periodic maintenance script for OS X

4 03 2009

Because my macs are not always awake at the scheduled times for the periodic cron system maintenance tasks, I’ve started to run them from a script every week or so.

The simple script I use is shown below:
#!/bin/bash
set -v
# Running the daily,weekly and monthly periodic scripts
sudo periodic daily && sudo periodic weekly && sudo periodic monthly
# Updating pre-binding
sudo update_prebinding -root / -force
# Repair disk permissions
sudo diskutil repairPermissions /








Follow

Get every new post delivered to your Inbox.