Installing matplotlib on OS-X (10.3.7) Here are my notes on how to build matplotlib (0.81) installed and working on OS-X. I have so far kept a fink-free system, so that's what I've done here as well. I use it with the AGG back end for generating images for a web site, and hopefully with the wx backend for interactive use and embedding in wx Applications. I've also quickly got it working with TK. Followiong these instructions shouls allow you to build a matplotlib for your system, as well as an installer for similar systems. Note that this is kind of a running commentary, not well edited. I'd read the whole thing before starting. 1) Requirements: ------------------------------------------- According to the matplotlib install docs (http://matplotlib.sourceforge.net/installing.html), you need the following: zlib freetype (>= 2.1.7) libpng Personally, I've been avoiding Fink, as it doesn't seem to play well with the rest of OS-X, including the Apple supplied Python, so I've looked elsewhere for these libs. a) zlib is installed by default on OS-X b) Freetype: There is some version included with Apple's X11, but no one I know of has gotten it to work with matplotlib. I got a version from sourceforge: http://freetype.sourceforge.net/index2.html I got: freetype-2.1.9.tar.gz Following the instructions in docs/INSTALL.UNX: $ ./configure --disable-shared --enable static $ make I've built the static libs, so that matplotlib can link against them, and the resulting package can be put on a stock OS-X. While "--disable-shared" should have prevented teh shared libs (*.so or *.dylib) from being built, there were built in my case, I don't know why. You'll be putting the libs somewhere specific anyway, so it shouldn't matter. Don't install the shared ibs if you're trying to build it statically, they might get found and used instead of teh static ones. If you are building matplotlib jsut for installation on the sytem you're building it on, you can just use: $ ./configure $ make $ sudo make install Which puts the shared lib in in /usr/local/..., which is a good place for it. c) libpng: I didn't have this on my system, except inside the wxWidgets source tree, so I went looking for it. I did a google search for "libpng OS-X". I found: http://www.libpng.org/pub/png/pngcode.html Which led me to the libpng sourceforge site. From there I downloaded: libpng-1.2.8.tar.gz unpacked it, and opened a terminal in the libpng-1.2.8 directory, and did: $ cp scripts/makefile.darwin ./makefile This will make a copy of the MakeFile for OS-X, and put it where it needs to be. (note that according to the INSTALL, there is supposed to be a makefile.macosx, but it wasn't there) I took a look in the makefile, and found: ZLIBLIB=/usr/local/lib ZLIBINC=/usr/local/include Which is not where zlib is on my system. However, while I can find zlib.h, I couldn't fine the actual lib, so I tried make without changing anything. $ make Which seemed to work fine. zlib must be installed in a standard location, and gcc found it. You might want to do a: sudo make install-static That installs the static libs (*.a files) in /usr/local/lib, which is a pretty good place for them. You may hnot need to do this if you're only using libpng for matplotlib. See below. If you want to do a dynamicl install, do: $ sudo make install to install the lib into /usr/local/ (this was specified in the makefile, and it's a good place for it) Optional: $ make test and $ ./pngtest pngnow.png Which both seemed to pass. d) Numeric or numarray: You'll need Numeric and/or numarray. Use whichever you prefer, or if you want to build something that others can use, install both, adn matplotlib will be build against both, and which one is used can be selected at runtime. You can get them from: http://www.pythonmac.org/packages/ Make sure to get them for the version of Python and OS-X that you have. Note that stuff built for OS-!0 10.3 wil work on 10.4, but maybe not the other way around. 2) Installing the wanted back-ends: Matplolib can be used with Agg, for rendering PNG image files for web sites, etc, or be used interactively with wxPyton TkInter and PyGTK. PyGTK can only be used with X11, and therefore not really with the native Mac. I don't support that, so if you want that, go with and all-fink (or darwinports) system. If you want wxPython or TK, you need to install then first. wxPython can be installed with the installer on www.pythonmac.org/packages. TK is a little trickier. There may be other ways to accomplish this, but this worked for me: I downloaded the BI (Batteries Included) installer for tcl/Tk Aqua. It's got a lot of stuff I don't need, but it's only disk space. I then used the MacPython PackageManager and the standard package list, and installed Tkinter from there. It seemed to work. I seem to have it working with my 2.4.1 install also, so maybe Tkinter comes with that installer. 3) Building matplotlib --------------------------------------------------------- First, I took a look at the matplotlib setup.py. It had "auto" for the back-end flags, so I thought I would give it a try that way: $python setup.py build If you have only the static version of libpng and libfreetype (*.a, and not *.dylib) in /usr/local/lib (or anywhere else on your lib search path), then it should link those statically. That worked for me. It will buld against any back-ends you have installed. It will only build the Agg back-end if you haven't installed TK or wx. To install it: $ sudo python setup.py install NOTE: in both these cases, the above will use whatever the default python is on your PATH. If you havn't installed a Python yourself, that will be the Apple-supplied Python. If you have, it may be Apple's and it may be yours, depending on whther you've messed with your PATH. What I do is use: python2.4 setup.py ..... When I want to make sure to get version 2.4. I only recommend "Framework Build" Pythons for OS-X, rather than anything you get from fink, etc. If you want to use fink, use it for everything, including installing matplotlib. The "best" Python for OS-X today is 2.4.1, available from: http://undefined.org/python/ 5) Editing matplotlibrc. matplotlibrc is the preferecnce fiel for matplotlib. There are a $python >>> import pylab Could not open font file /Library/Fonts/NISC18030.ttf No module named pygtk PyGTK version 1.99.16 or greater is required to run the GTK Matplotlib backends This turns out to be because the matlabrc file sets the GTKAgg back end as the default. You have two choices. 1) you can set the back end before importing pylab. >>> import matplotlib >>> matplotlib.use('Agg') >>> import pylab This works fine 2) Edit the matplotlibrc file. I found it in: /System/Library/Frameworks/Python.framework/Versions/2.3/share/matplotlib/.matplotlibrc Change the line: backend : GTKAgg # the default backend to backend : Agg # the default backend And you're all set to make images for the web, etc. I'm going to leave getting it to work with wxPython for another day. 3) Building a matplotlib Binary Package for OS-X: First you need to make sure you've got libpng and libfreetype staticaly linked. I did this by copying the headers and *.a files for them into a directory I created called "StaticLibs", in the main matplot lib directory (the one setup,py is in). You could just as easily put in links, rather than copies, probably a better idea, actually. Then I edited setupext.py, so that distutils would only look there: basedir = { 'win32' : ['win32_static',], 'linux2' : ['/usr/local', '/usr',], 'linux' : ['/usr/local', '/usr',], # 'darwin' : ['/usr/local', '/usr', '/sw', '/usr/X11R6'], 'darwin' : ['StaticLibs'], 'freebsd4' : ['/usr/local', '/usr'], 'sunos5' : [os.getenv('MPLIB_BASE') or '/usr/local',], } I'm talking to John Hunter about having a conditional Static setup in the official setup.py to support this. This seemed to so the job. To test, you can run: $ otool -L *.so in the matplotlib directory that is buried in the build directory. it will tell you what libs the matplotlib extensions are linked to. They should not be linked to libfreetype or libpng. libz is OK, it's included with the stock OS-X. Once you've got that built right, you can make an installer package with Py2App. This is a note from Bob Ippolito on the macPython mailing list: """use bdist_mpkg from py2app to make a redistributable .pkg installer for it. After installing py2app, you should have a tool in /usr/local/bin called "bdist_mpkg" that will Just Do It without any setup.py modifications to the target lib... so go into the matplotlib directory, type bdist_mpkg, and cross your fingers that a dist/matplotlib-xx.pkg """ Here's exactly what I tried: In the matplotlib directory (the same place as setup.py) $ bdist_mpkg and it worked! Note: there are occasionally troubles with installing a newer matplotlib over an older one. You may want to remove an older version before installing, if you have one. To do this, delete: /Library/Python/2.3/matplotlib/ Then click the mpkg. to install.