Installing NumPy / SciPy / Matplotlib / IPython on Mac OS 10.7 (Lion)

Hi all,

This is a topic that I have spent way too many hours on... Ever since
Mac OS 10.4 I have been fine-tuning installation instructions for
these packages, aiming for the shortest instructions installing as few
extras as possible. With Lion it is finally getting very simple :slight_smile:

First off, I agree that the Enthought Python distribution is a good
way to go. It has the downsides of being a separate Python
installation (at least pretty well implemented) and costing quite a
bit of money to get the full functionality. So if you want an
alternative costing only a little bit of effort, read on...

Second, I would not recommend installing another Python on a Mac
unless you know what you are doing. Just use the system-provided
Python! In my experience the Mac environment is not very well
configured to handle multiple Pythons and you can easily find your
Python code using some bits of the one Python distribution and some of
the other, leading to strange bugs. Ask yourself whether you really
need Python 2.7.2 when you already have 2.7.1 installed. [Yes, I know
you can "brew install python", but it goes against the whole
philosophy of Homebrew, doesn't it?]

This installs the latest stable releases of these packages, using only
Homebrew and easy_install. I use the system-provided NumPy 1.5.1, as
this is good enough for most purposes and therefore one less package
to install. I assume that you have Lion, Xcode 4 and not much else
(see the more detailed instructions below if you have more than this).

For the impatient, here are my instructions ready to be entered on
your Terminal:

/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"
brew update
brew install pkg-config gfortran zeromq pyqt
echo "export PYTHONPATH=/usr/local/lib/python:\$PYTHONPATH" >> ~/.bash_profile
sudo easy_install nose Sphinx readline Pygments pyzmq IPython matplotlib
sudo CC=clang CXX=clang++ FFLAGS=-ff2c easy_install scipy

I also attach a more detailed description below, including tests to
check whether your installation works. Now to put all of this on a
blog... :slight_smile:

Enjoy and good luck,
Ludwig

Installing NumPy / SciPy / Matplotlib / IPython on Mac OS 10.7 (Lion)

···

=====================================================================

Updated 21/02/2012

Apple Software
--------------

- Install Mac OS 10.7 (Lion) from the App Store

- Do a Software Update

- Install Xcode 4 from the App Store. You need to run the "Install Xcode" app
  that is downloaded to /Applications. Test Xcode by confirming that "gcc" runs
  in the Terminal.

Homebrew
--------

- In preparation for homebrew, the best option is to delete /usr/local via::

  sudo rm -rf /usr/local

  and install any software in it via homebrew instead. If this step fills you
  with dread and you do not want to lose your beloved third-party software, the
  second-best option is to make sure you have write permission for the directory
  via::

  sudo chown -R username:staff /usr/local

- Install Homebrew (http://mxcl.github.com/homebrew/) by running the following
  command in the Terminal::

  /usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"

- Run "brew update" to get the latest formulas

- Install the first package, just to have an existing homebrew Cellar::

  brew install pkg-config

- Run "brew doctor" and check the suggestions. Specifically, comment out
  references to Mac OS 10.6 in ~/.profile, contained in environment variables
  such as MACOSX_DEPLOYMENT_TARGET, CFLAGS, LDFLAGS and FFLAGS.

- Now install the following packages::

  brew install gfortran zeromq pyqt

- Add the following lines to your ~/.bash_profile (or ~/.profile if it exists)::

  # This is for SIP (and PyQT) as installed (and suggested) by Homebrew
  export PYTHONPATH=/usr/local/lib/python:$PYTHONPATH

IPython
-------

- Install the following Python packages (nose is for running unit tests, Sphinx
  creates documentation and readline, Pygments and pyzmq enhance IPython)::

  sudo easy_install nose Sphinx readline Pygments pyzmq IPython

NumPy
-----

- Lion comes with NumPy 1.5.1 out of the box. This is sufficient for our
  purposes, and we therefore use the system version. We can verify it by running
  the test suite from within Python::

  import numpy as np
  np.test('full')

  The result for NumPy version 1.5.1 is (the failures may be ignored)::

  FAILED (KNOWNFAIL=4, SKIP=1, failures=8)
  <nose.result.TextTestResult run=3019 errors=0 failures=8>

SciPy
-----

- The current stable version of SciPy compiles on Lion, as long as you pass the
  appropriate compiler flags (you need at least 0.10.0)::

  sudo CC=clang CXX=clang++ FFLAGS=-ff2c easy_install scipy

- Test that the installation worked by running the test suite from within
  Python (first change to another directory before running Python)::

  import scipy
  scipy.test()

  The result for SciPy version 0.10.0 is (the failures may be ignored)::

  FAILED (KNOWNFAIL=12, SKIP=36, failures=8)
  <nose.result.TextTestResult run=5095 errors=0 failures=8>

Matplotlib
----------

- If you want support for TeX rendering in matplotlib, install MacTeX first from
  http://www.tug.org/mactex.

- Matplotlib is now easy_installable on Lion (you need at least version 1.1.0)::

  sudo easy_install matplotlib

- Test that the installation worked by running the test suite from within
  Python (first change to another directory before running Python)::

  import pylab
  pylab.test()

  The result for Matplotlib version 1.1.0 is::

  OK (SKIP=1)
  <nose.result.TextTestResult run=156 errors=0 failures=0>

- A more thorough test of Matplotlib involves the following::

  brew install libjpeg
  wget http://effbot.org/downloads/Imaging-1.1.7.tar.gz
  tar xzvf Imaging-1.1.7.tar.gz
  cd Imaging-1.1.7
  <... edit setup.py so that 'FREETYPE_ROOT = libinclude("/usr/X11R6")' ...>
  python setup.py build
  sudo python setup.py install
  <... start python ...>
  import matplotlib
  matplotlib.test()

  The result for Matplotlib version 1.1.0 is (this takes a while!)::

  Ran 993 tests in 208.338s
  OK (KNOWNFAIL=264)

  This generates a lot of test images in the current directory - feel free to
  wipe the result_images/ directory afterwards.

- Install the matplotlibrc file in your home directory and edit it to select the
  default backend. If you followed these instructions you should be able to use
  the MacOSX, TkAgg and Qt4Agg interactive backends::

  mkdir ~/.matplotlib
  cp /Library/Python/2.7/site-packages/matplotlib-1.1.0-py2.7-macosx-10.7-intel.egg/matplotlib/mpl-data/matplotlibrc
~/.matplotlib
  vi ~/.matplotlib/matplotlibrc

- For fun, try out the new IPython Qt console and HTML notebook with inline
  Matplotlib plots (assuming you have at least IPython version 0.12). This is a
  good test to verify that all the previous steps worked. In the Qt console,
  type `plot([1,2])` to see an inline plot appear (this also highlights the
  function help docstring that pops up as you type). In the notebook, click on
  "New Notebook" on the initial HTML page that pops up in your browser, and type
  `plot([1,2])` in the first cell of the notebook page, followed by Shift-Enter
  to see the inline plot::

  ipython qtconsole --pylab=inline
  ipython notebook --pylab=inline