The Pain: Installing Matplotlib in Snow Leopard on a Mac
I've said it before, and I'll say it again, python combined with matplotlib is one of the most potent tools for scientific computation. But gee, why does it have to be so hard to install it on my Macbook with Snow Leopard?
I've finally figured it out how to install it on my computer with some measure of understanding. So here are some tips.
If you really want to get it working RIGHT NOW, I suggest you use macports, and just do
sudo port install py26-matplotlib
which will install a separate version of python2.6 in the '/opt/local' directory. However, everything will work. The down side of this approach is that other packages that you install through a .dmg will fail to find that version of python installed in '/opt/local'. Alas, the standard Python location is in
And that's where most .pkg installers assume Python to be.
At the moment there is some kind of mass confusion as to which Python, 2.6 or 2.7, or even 3.0. If you have a new Macbook, it's Intel and not Universal binaries. Snow Leopard is 64 bit, and everything runs only in Cocoa. Meaning that a whole bunch of Python libraries in Carbon are utterly useless to you. On Snow Leopard, I've haven't been able to get Python to work in 32-bit mode with Carbon. So you really must compile from source if you want matplotlib to work on a recent Macbook installed with Snow Leopard.
So if you haven't installed from source before, you'd better learn how to use the terminal and install Apple's Xcode on your computer before anything else (don't forget to reboot the terminal after the installation).
You can then install Python 2.6 or Python 2.7 from their website. Now you might be tempted to stick with the default Python that comes with Snow Leopard, but for some reason unfathomable to me, Apple puts Python 2.3, 2.5 and 2.6 in
which is not where most .pkg python libraries expect python to be.
I suggest that you set /usr/bin/python as a symbolic link to the python that you want to use, as pip and easy_install searches there.
It turns out that numpy and matplotlib talk to each other through the C API, so they must be built from the same compiler. The method of installation of the two must be the same (pip, easy_install, macports, gcc etc.). In fact, you should find the 'site-packages' directory and delete existing 'numpy', 'matplolib' and 'pylab' modules before you continue.
Actually, easy_install and pip is broken for matplotlib on Snow Leopard. For some reason, the pypi entry is corrupted and they pull down a much older version of matplotlib that is not compatible with python 2.6 and python 2.7.
Which means, that you must install numpy and matplotlib from source.
Installing numpy is straightforward. Download the latest stable source, and do the classic 'sudo python setup.py install'. I installed numpy 1.6.0 and it seems to work fine with both Python 2.6 and Python 2.7
In contrast, installing from source for matplotlib requires quite a few extra steps. You need to install freetype2, zlib, and libpng. These are proper unix packages, and once you've downloaded the source, you should do the old
./configure sudo make install
If you don't want to install them into the standard unix locations, then you can do the prefix magic, but then you have to ensure that the gcc include, library and link flags are set properly in you .cshrc or .bashrc file.
The versions I used were the latest stable freetype2 and zlib, but the latest libpng had problems, so I used libpng 1.2.44
For some reason the matplotlib setup.py could not figure out where the freetype2 headers were. So I had to copy the include directory in the freetype2 source package into the matplotlib source directory and renamed that directory to 'freetype2'. Only then could I do
sudo python setup.py install
Once installed, I had to do one more thing. As I often run matplotlib in batch mode, you have to make sure the backend is TkAgg. I had to find the matplotlib resource file, which in a Python command line
import matplotlib matplotlib.matplotlib_fname()
tells me that it is located in
In that file, I choose 'backend = tkagg' to enable silent batch mode.