Thursday, May 10, 2012

Installing pycairo or py2cairo on OS X Lion

This is for folks that don't like using macports, fink, or homebrew, but want to install cairo and possibly use it with Python.

First of all, if you're comfortable with the version of python that ships with OS X, I'd recommend these binaries - they're much more mac-user friendly. You have to install a couple of them in the right order, though, but the page explains it pretty well.
(disclaimer: I didn't actually go all the way of getting py2cairo installed with these frameworks, but py2cairo's install scripts should just work with these binaries... feel free to comment if you try it and it doesn't!)

For those of us that are using a "real" version of python from, things aren't as pretty... there are some guides for building on Leopard (note, not Snow Leopard, just Leopard!), but a lot has changed since then, especially if you're talking about 64-bit architectures. In case it's helpful to anyone, here's a script I hacked together over a couple of days (with help from this stackoverflow thread) to get py2cairo up and running on OS X Lion with a 64-bit installation:



rm -rf ~/Downloads/temp
mkdir ~/Downloads/temp
cd ~/Downloads/temp

curl -o pkgconfig.tar.gz
curl -o libpng.tar.gz
curl -o pixman.tar.gz
curl -o cairo.tar.gz
curl -o py2cairo.tar.bz2

tar -xzvf pkgconfig.tar.gz
tar -xzvf libpng.tar.gz
tar -xzvf pixman.tar.gz
tar -xzvf cairo.tar.gz
tar -xzvf py2cairo.tar.bz2

mv pkg-config-* pkgconfig
mv libpng-* libpng
mv pixman-* pixman
mv cairo-* cairo
mv py2cairo-* py2cairo

cd pkgconfig
sudo make install

export PKG_CONFIG=/usr/local/bin/pkg-config
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

export PYTHONPATH=/Library/Frameworks/Python.framework/Versions/2.7/
export LD_LIBRARY_PATH=/Library/Frameworks/Python.framework/Versions/2.7/:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/Library/Frameworks/Python.framework/Versions/2.7/lib:$LD_LIBRARY_PATH
export LINKFLAGS='-search_dylibs_first -L /Library/Frameworks/Python.framework/Versions/2.7/lib/'
export ARCHFLAGS='-arch x86_64'

export CC="gcc"
export LDFLAGS="-arch x86_64"
export CFLAGS="-arch x86_64"

cd ../libpng
sudo make install

cd ../pixman
sudo make install

cd ../cairo
sudo make install

cd ../py2cairo
python waf clean

python waf configure --prefix=$PYTHONPATH
python waf build
python waf install

cd ../..
rm -rf ~/Downloads/temp

Make sure to try "import cairo" in the interpreter to test if everything worked.


  1. Very nice step-by-step page! Thanks.
    Unfortunately I get this on Mountain Lion:
    >>> import cairo
    Fatal Python error: PyThreadState_Get: no current thread

    1. Hmmm... that sounds like there's some kind of PYTHONPATH conflict with the default OS X installation. Apple may have changed stuff yet again from Lion to Mountain Lion... as I haven't upgraded yet, I can't test this on Mountain Lion. That error seems to get a lot of hits on google - my advice is to start there.

      Sorry I'm not much more help than that!