Please note that the current version of this guide is available here. This page is no longer updated.
I decided to clear the cobwebs out of my MacBook Pro (update 8/20/11: new MacBook Air in hand!) and start fresh with Lion, Python and all the other tools I regularly use (virtualenv, NumPy, SciPy, matplotlib and IPython). It was a lengthy process and I was unable to find a comprehensive walkthrough online, so I had to figure it out through trial and error (mostly error). I was surprised at the lack of available information; with such a major release I expected more complete documentation. So, once again quoting Tom Lehrer, I have a modest example here.
The important things we'll accomplish in this guide are (click any section to jump directly there):
- A clean install of Mac OS 10.7 Lion
- Installing Homebrew
- Installing Python
- Installing virtualenv
- Installing NumPy, SciPy and matplotlib
- Installing IPython and the qtconsole
As you may recall, this has been a somewhat more-than-trivial process for Lion's early adopters, as enough was changed behind the scenes to mess up some of the current versions of these tools. I'll detail how to get around those issues by getting cutting-edge distributions. Note that today's cutting-edge works on my machine; tomorrow's may introduce new bugs. When stable Lion-compatible versions of these programs are available, I strongly encourage you to use those instead.
If you would rather skip my commentary, you should be able to simply execute every line of code in sequence.
As always, remember that some of these lines will take you off the beaten path and could cause serious irrevocable damage to your computer! In particular, typing sudo gives you root access and therefore the ability to erase everything with a single keystroke. Please do not execute any code without understanding exactly what it does. In the worst case, I suppose you could always jump back to square one by wiping your hard drive and running a Lion clean install, but use caution nonetheless. YMMV.
Lion Clean Install
To begin, there has been some concern that users can't perform a clean install of Mac OS X 10.7 (Lion) without quite a bit of hacking around with physical media. I'm happy to report that isn't true -- as long as you performed a standard Lion install first. The OSX installer (App Store link) automatically creates a "Recovery HD" partition on your hard drive, just in case something goes wrong. The partition contains just two things: Disk Utility and the ability to run the Lion installer. To access this recovery mode, hold Command-R while your computer is starting up. Use Disk Utility to erase your hard drive, then run the installer to re-download and load Lion. When you're done, you'll have a clean install of Lion. Download your favorite browser and you're good to go. I have to say, discovering this feature was a pleasant surprise -- hats off to Apple for thinking ahead on this one.
Next, install Xcode 4 -- its bundled compilers have to be available for the next step to work. Here's a link to it in the App Store; it's a free (but large) download. Note that like Lion, the download does not actually install the application. Instead, it puts an "Install Xcode" icon in your applications folder, which you must run to complete the installation. I don't understand this behavior of installing the installer, but it is what it is.
Next up, one of my new favorite tools -- Homebrew. The homepage says it pretty well: "Homebrew is the easiest and most flexible way to install the UNIX tools Apple didn't include with OS X." If you've messed around with MacPorts (or tried to do this sort of stuff -- gasp -- by yourself), you'll be shocked by the drop-dead simplicity of this tool. Even installing it is a no-brainer; just fire up Terminal and execute the following line:
ruby <(curl -fsSkL raw.github.com/mxcl/homebrew/go)
That's it -- now you have Homebrew. Try running brew update and brew doctor at the command line to make sure it's working properly.
You'll need to add the Homebrew directory to your system path, in order to make sure that Homebrew-installed software is given higher priority than any other version. To do so, open your .bash_profile in your user directory (that's the ~ directory or, more verbosely, /Users/[your_user_name]) and add the following line (if you don't have a .bash_profile, create one with your favorite text editor -- you may need to do this in order to see it, as it will be a hidden file):
export PATH=/usr/local/bin:$PATH
If you haven't already, restart Terminal so it picks up the new path. Homebrew is generally very good about alerting you to any action you need to take after it finishes running, including path modifications. Make sure to pay attention to its output.
Python
Now on to the main event: Python. As of this writing, 2.7.3 is the most recent version. If that's no longer true as you read this, be sure to change any version-specific references! The following instruction will install Python, first installing any prerequisites. Mountain Lion users may have to install XQuartz; simply follow the instructions if prompted to do so. The --framework option tells it to build as a Framework, which has some downstream niceties, and --universal builds a universal (32/64 bit) version:
brew install python --framework --universal
This will install a version of disutils, but you must update your path in order to run it properly. Add the following to your .bash_profile (you may combine it with the previous .bash_profile edit, if you choose):
export PATH=/usr/local/share/python:$PATH
Once again, make sure you reload Terminal to update your path variable. Finally, you need to change Lion's symlink to point at your new Python install. Run the following three lines in sequence:
cd /System/Library/Frameworks/Python.framework/Versions sudo rm Current ln -s /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/Current
You will be prompted for your password after executing the sudo command.
To verify that your installation went as planned, type which python at the command line. You should see /usr/local/bin/python in response. Furthermore, if you type python, you should see Python 2.7.3 launch (type quit() to return to Terminal).
You should now have pip available on your system. If you discover that you don't have pip, simply execute:
easy_install pip
Note that depending on your permissions settings, you may have to prepend sudo to the front of many of these lines. Always remember that sudo gives you root access!! Therefore, in the interest of caution, I'm not going to include the sudo command here, but if your installs are failing because of permissions, you may have to add it.
virtualenv
Virtualenv is a tool that allows you to create isolated Python environments, each with its own set of packages and dependencies. This is useful for testing or managing package requirements (for example, if you build an application that is dependent on a certain version of a third-party package but another application requires a more recent version, you may break the first application by upgrading). This is not a required step, and all the commands below should work whether or not you are using virtualenv, so I'm listing it here for convenience only. The only difference will be that directories (such as that returned by which pip) will point to the virtualenv rather than /usr/local.
First, install virtualenv and virtualenvwrapper (a tool which makes working with virtualenv somewhat easier):
pip install virtualenv pip install virtualenvwrapper
Next, source the virtualenvwrapper script:
source /usr/local/share/python/virtualenvwrapper.sh
This will create a hidden virtualenv directory at ~/.virtualenv. Now you can make your first virtual environment:
mkvirtualenv test1
Your new virtualenv test1 comes with a complete install of Python 2.7.3 and its own version of Pip. It is activated by default, so running any pip command will only impact this environment. Note that if you deactivate the virtualenv, you will lose access to any packages installed in it. You can switch between virtualenvs with the workon command.
In order to delete your test virtualenv, run rmvirtualenv test1. Also, to specify a different version of Python, just use mkvirtualenv -p python3.2 test1 (replacing python3.2 with the shell name of your preferred version).
NumPy
If pip installed properly (executing which pip should return /usr/local/share/python/pip), then you can install NumPy simply with:
pip install numpy
Check your work by executing:
python
followed by:
import numpy print numpy.__version__ print numpy.__file__ quit()
The version should be 1.6.2 (as of July 2012), and the file should be somewhere in /usr/local/Cellar/....
SciPy
SciPy requires a Fortran compiler, which Lion lacks. To acquire one, use Homebrew:
brew install gfortran
Under Lion, install the stable version of SciPy (0.10) by running:
pip install scipy
Mountain Lion users will need to install the development version of SciPy (0.11) by executing the following line:
pip install -e git+https://github.com/scipy/scipy#egg=scipy-dev
As with NumPy, make sure you have everything working by running:
python
and then:
import scipy print scipy.__version__ print scipy.__file__ quit()
On Lion, install matplotlib like any other package -- if you like, you can check your work once it finishes.
pip install matplotlib
Mountain Lion users will have to use the development version as of this writing:
pip install git+https://github.com/matplotlib/matplotlib.git#egg=matplotlib-dev
Installing IPython itself is a fairly straightforward pip command:
pip install ipython
Getting the new qtconsole to run takes a little more work -- but it's well worth it. The qtconsole is like running IPython on steroids -- my favorite feature is that matplotlib output can be displayed inline.
To begin, you'll need Nokia's qt library, which is available here. Make sure you download the library, not the SDK.
Once that's done, begin installing the prerequisites:
brew install pyqt
Note that after installing pyqt, brew will prompt you to add the following to your pythonpath in .bash_profile:
export PYTHONPATH=/usr/local/lib/python:$PYTHONPATH
Continue installing:
brew install zmq pip install pyzmq pip install pygments
And that's it -- you should now be able to launch the qtconsole by executing ipython qtconsole. If you want to see the matplotlib output inline (and why wouldn't you?), then execute:
ipython qtconsole --pylab=inline
Fin.
Phew! I hope this works for everyone out there. Until the next software upgrade, anyway.
Updated 9/1/11 to include virtualenv.
{ 136 comments… read them below or add one }
← Previous Comments
Thanks for the detailed tutorial!
Everything went as written, except for when running “which python” I got usr/bin/python instead of usr/local/bin/python. I had to run “brew link python” to get it to work.
I had just upgraded from Leopard to Mountain Lion (through Snow Leopard), and had previous versions of python installed.
Unfortunately I’m still getting “/usr/bin/python” when I’m doing “which python”. More precisely I do:
cd /System/Library/Frameworks/Python.framework/Versions
sudo rm Current
sudo ln -s /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/Current
dave@shadowfax$ which python
/usr/bin/python
Comments:
1. I need to use sudo for linking as otherwise I get an error.
2. The latest version of python appears to be 2.7.3 at the moment.
3. If I run python I get an older version (2.7.1).
4. I’m still on Lion.
Any idea?
OK, I have just found out, sorry.
My problem was that /usr/bin was coming before /usr/local/bin in my $PATH.
http://stackoverflow.com/questions/5157678/python-homebrew-by-default
Thanks for this useful guide. I tried using an academic license of Enthought Python distribution before, but ran into some trouble with incompatible 32bit and 64bit libraries when installing additional python packages. Building everything from scratch using your guide solved all my troubles and avoids the licensing issues with Enthought as well.
At the point of installing scipy, I get the error:
numpy.distutils.npy_pkg_config.PkgNotFound: Could not find file(s) ['/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/lib/npy-pkg-config/npymath.ini']
Unfortunately the installation of numpy had been fine, getting the same version of this tutorial:
>>> import numpy
>>> print numpy.__version__
1.6.2
>>> print numpy.__file__
/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/__init__.pyc
So, there must be something wrong with the numpy package? Any idea?
Dave, I was finally able to reproduce your problem. It seems to be an issue with NumPy and pip, and I can’t explain why it doesn’t always happen.
To get around it, you’ll need to install NumPy yourself. The instructions are available here. Basically you’ll git clone the NumPy repository into a temporary folder, then step into that folder and build/install the package. While you’re at it, you may want to install SciPy manually as well, with almost the exact same steps.
Thank you J, the tip of using git clone was the winning one!
Have a good day!
I also get this error. I fixed it by a trick.
Get numpy souce of the same version, just build it:
python setup.py build –fcompiler=gnu95
you can find the missing file “npymath.ini” in build/src.macosx-10.8-intel-2.7/numpy/core/lib/npy-pkg-config/, copy the directory npy-pkg-config to /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/lib/
a lib file libnpymath.a is also needed, which can be found in build/temp.macosx-10.8-intel-2.7, copy it to /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/lib/ too.
good luck!
Dear all,
I tried all the procedures and tricks you mentioned but I have still problems in installing Scipy… here the last lines coming out from the compiling procedure:
swig: scipy/sparse/linalg/dsolve/umfpack/umfpack.i
swig -python -I/usr/local/include -I/usr/local/include -I/usr/local/include -o build/src.macosx-10.7-intel-2.7/scipy/sparse/linalg/dsolve/umfpack/_umfpack_wrap.c -outdir build/src.macosx-10.7-intel-2.7/scipy/sparse/linalg/dsolve/umfpack scipy/sparse/linalg/dsolve/umfpack/umfpack.i
unable to execute swig: No such file or directory
error: command ‘swig’ failed with exit status 1
before that I tried with pip install scipy without succeed.
gcc –version 4.2.1
Thank you in advance,
Ale
Dear all,
I solved installing SWIG https://github.com/titanous/homebrew-gnuradio/issues/2.
I still have problems in the scipy.test()… several “ImportError: numpy.core.multiarray failed to import” but anyway it seems to work..
Thank you very much again for these very clear guide and your suggestions…
ale
I ran into the same problem as Dave. However, as I was going through the SciPy install – I got the error that “‘swig’ failed with exit status 1.” After a bit of searching, I found that SciPy requires swig, but you need to specifically download it. Simple fix with “brew install swig,” then build-and-install — but it had me worried for a while that this fix wasn’t quite what I needed. Everything is good to go now! Thanks for your walk-through!
I am with mountain now, and I can’t install scipy and matplotlib on my macboook pro.
short error message for matplotlib:
InstallationError: Command /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -c “import setuptools;__file__=’/var/folders/z6/tmxvx5rn21lblvzlkhdltqpm0000gn/T/pip-_YArQ_-build/setup.py’;exec(compile(open(__file__).read().replace(‘\r\n’, ‘\n’), __file__, ‘exec’))” install –record /var/folders/z6/tmxvx5rn21lblvzlkhdltqpm0000gn/T/pip-i3muKd-record/install-record.txt –single-version-externally-managed failed with error code 1 in /var/folders/z6/tmxvx5rn21lblvzlkhdltqpm0000gn/T/pip-_YArQ_-build
short error message of scipy:
numpy.distutils.npy_pkg_config.PkgNotFound: Could not find file(s) ['/usr/local/lib/python2.7/site-packages/numpy/core/lib/npy-pkg-config/npymath.ini']
—————————————-
Command python setup.py egg_info failed with error code 1 in /Users/hubuki/src/scipy
Storing complete log in /Users/hubuki/.pip/pip.log
I am on Mountain Lion. I tried installing python etc as described but cannot get past the following point:
ln -s /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/Current
gives the following output:
ln: ./Current: File exists
which python gives:
/usr/bin/python
brew doctor gives:
Warning: Your Homebrew is outdated
You haven’t updated for at least 24 hours, this is a long time in brewland!
Warning: You have unlinked kegs in your Cellar
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these: python
How should I proceed?
brew link python
brew update # will get rid of the first Homebrew warning, though its not essential
Double check your path, make sure that /usr/local/bin is before /usr/bin
1. I’m new to this – running mountain lion
after installing numpy it’s stored in /usr/local/lib/python2.7/site-packages/numpy/__init__.pyc as opposed to
I followed all the steps, double checked paths to make sure they’re correct. I had the same problems with scipy as above, but before continuing I wanted to be sure having numpy there won’t cause probs.
**as oppsed to in /usr/local/Cellar…
I ran into the same problem, my os x version is mountain lion 10.8.2… can this be the cause of the problem?
I have solved this problem! The installation of virtualenv is the cause in my case. Follow the instruction about install virtualenv in the hyperlink below, then ‘pip install numpy’ will do the right thing~
http://dubroy.com/blog/so-you-want-to-install-a-python-package/
I’m having trouble with the first step at the link:
“Use virtualenv to create a base Python environment for your user account:
python virtualenv.py ~/venv/base”
I saved the virtualenv.py file in my python folder /usr/local/Cellar/python folder, but when I type “python virtualenv.py ~/venv/base” into the command line I receive this error:
/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can’t open file ‘virtualenv.py’: [Errno 2] No such file or directory
Any ideas? Thanks in advance.
Hi there,
I’m installing on a macbook pro 13” with LION 10.7.5. I get this error on scipy installation:
numpy.distutils.npy_pkg_config.PkgNotFound: Could not find file(s) ['/usr/local/lib/python2.7/site-packages/numpy/core/lib/npy-pkg-config/npymath.ini']
—————————————-
Command python setup.py egg_info failed with error code 1 in /var/folders/v1/rhksf40d4y1343qqv_x134p80000gq/T/pip-build/scipy
ONE MORE THING: Ipython installation seemed successful but I’m not able to execute ipython console.
Thanks
Roberto
Same here.
“ipython qtconsole –pylab=inline” fails.
No PySide to import. And “pip install pyside” fails: Failed to locate the Python library /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.so.1
Thanks for this, saved me hours of pain! However, for me Homebrew has trouble finding Numpy from a pip install (if not using a virtualenv). The only solution seems to be to do a
>python setup.py install
for both Numpy and Scipy. After this, all good.
Tiny typo: disutils -> distutils
Kk, so I appear to have hit every branch on the way down here…
First off don’t forget to update recipes with brew update if you had brew already…
The “npymath.ini” nonsense goes away if you install numpy without using pip from the tarball, as previously trailed.
There is a pyside dependency for ipython at runtime it appears, also need a couple of other bits:
brew install freetype
brew install pyside
Finally I had to link from /usr/local/share/python the ipython and associated scripts to my bin, mainly because i wasn’t interested in running a virtual env.
Just letting you know that I had to install libpng and libfreetype before matplotlib would install:
brew install freetype
brew install libpng
Im getting;
Cannot import PySide >= 1.0.3 or PyQt4 >= 4.7
Even though I have both installed.
Hi,
Great tutorial. Thanks so much. Is there really a need to install the Qt library from Nokia, as brew installs a qt library too as it is a dependency for pyqt etc.?
Thanks!
Marc
Hi,
Brew easier than Macports ? I tried. I completely failed (running Lion 10.7.4, Xcode 4.4.1)…
>ruby -e “$(curl -fsSkL raw.github.com/mxcl/homebrew/go)”
Illegal variable name.
Never used ruby before, so I don’t know how to fix this.
Then I tried this way (I moved to /usr/local first) :
>mkdir homebrew && curl -L https://github.com/mxcl/homebrew/tarball/master | tar xz –strip 1 -C homebrew
but :
mkdir: homebrew: Permission denied
so I had to sudo every step of it and hand-link to /usr/local/bin
Then I tested brew, as explained but if failed:
>brew update
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/pathname.rb:444:in `readlink’: Permission denied – /usr/local/bin/brew (Errno::EACCES)
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/pathname.rb:444:in `realpath_rec’
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/pathname.rb:474:in `realpath’
from /usr/local/bin/brew:14
So I have to conclude I really have no brains or that the claim that it is much better than Macports is preposterous or both… Too bad.
If someone can lend me a brain or some explanation, I would be glad…
Thanks,
Lorenzaccio
I’m on Mountain Lion and after installing numpy I got:
>>> import numpy
>>> print numpy.__version__
1.6.2
>>> print numpy.__file__
/usr/local/lib/python2.7/site-packages/numpy/__init__.pyc
instead of /usr/local/Cellar… can you help? thanks!
same here. There are some comments above having the same issue. I followed the suggestions of “install numpy, and scipy from github” and I am able to install all packages and tested successfully, even though they still in /usr/local/lib/python2.7/site-packages/ instead of /usr/local/Cellar…
yeah I saw the above comments and tried and it seemed the imported numpy did point to /usr/local/Cellar..only when I was in that virtualevn.
so I gave up and tried the ScipySuperpack(http://fonnesbeck.github.com/ScipySuperpack/). after installed I got:
>>> print numpy.__version__
1.8.0.dev-3abd869
>>> print numpy.__file__
/Library/Python/2.7/site-packages/numpy-1.8.0.dev_3abd869_20121222-py2.7-macosx-10.8-intel.egg/numpy/__init__.pyc
This guide is very useful. Did you try running numpy and scipy’s tests on Mountain Lion after installing them? I get several (over 70) scipy failures and I don’t know how important they are.
Just wanted to post props for this (arrived here from SO). I fought my PC for a day and half before giving up and getting out my iMac for this, and though matplotlib is still the toughest part, I was able to get both numpy and scipy from the regular pip script (no special download required).
Maybe update post to reflect that?
scipy installation fails for me on mountain lion, with the following message:
ld: library not found for -lg2c
thanks nonetheless for this info, I am surprised there is less on this on the web.
This article is just amazing. Thank you J!
Though I needed couple days to accomplish every single step.
I stumbled upon problem regarding numpy installation. I’ve solved it (kind of) by using homebrew tap:
bash-$ brew tap samueljohn/python
bash-$ brew install gfortran #(execute this if your system doesn’t have gfortran)
bash-$ pip install nose # for testing purposes (more here: http://www.scipy.org/Installing_SciPy/Mac_OS_X)
bash-$ brew install numpy
bash-$ brew install scipy
After testing both numpy and scipy there were some failures but no errors. Good enough.
It was great experience, maybe too tiresome but still very exciting.
Also, Uri Nieto ( http://bit.ly/XwSiZU ) recommends to delete default python version which comes with clean Mountain Lion install. One may try it if one is having problems regarding incorrect numpy installation location.
Good luck!
Thanks for this guide. I ran into a few road blocks even with theses instructions.
In case it helps others:
Installing X-Code did not automatically install the Command Line Tools. I had to open X-Code > File > Preference > Downloads > Install Command Line Tools in order to have a compiler available for build and make commands in the terminal.
Without doing that I could not install NumPy or SciPy.
Also, As of 3/17/2013 you can just run pip install scipy too.
Matplotlib would not install either because packages freetype, png would not build. I search the web for hours to find a solution, which turned out to be very simple: http://stackoverflow.com/questions/12363557/matplotlib-install-failure-on-mac-osx-10-8-mountain-lion
run these commands to install those packages:
brew install freetype
brew install libpng
then I installed the development version of matplotlib successfully.
everything else worked fine. thanks again.
Alternative to XCode:
https://github.com/kennethreitz/osx-gcc-installer
Don’t use this. It doesn’t work, despite what I read elsewhere.
I had to do
pip install cython
before I could get scipy to compile and install.
I get this error at the end when I try to install scipy:
gfortran: error: libgfortran.spec: No such file or directory
gfortran: error: libgfortran.spec: No such file or directory
error: Command “/usr/local/bin/gfortran -Wall -Wall -undefined dynamic_lookup -bundle build/temp.macosx-10.7-intel-2.7/build/src.macosx-10.7-intel-2.7/scipy/fftpack/_fftpackmodule.o build/temp.macosx-10.7-intel-2.7/scipy/fftpack/src/zfft.o build/temp.macosx-10.7-intel-2.7/scipy/fftpack/src/drfft.o build/temp.macosx-10.7-intel-2.7/scipy/fftpack/src/zrfft.o build/temp.macosx-10.7-intel-2.7/scipy/fftpack/src/zfftnd.o build/temp.macosx-10.7-intel-2.7/build/src.macosx-10.7-intel-2.7/scipy/fftpack/src/dct.o build/temp.macosx-10.7-intel-2.7/build/src.macosx-10.7-intel-2.7/scipy/fftpack/src/dst.o build/temp.macosx-10.7-intel-2.7/build/src.macosx-10.7-intel-2.7/fortranobject.o -L/usr/local/lib/gcc/x86_64-apple-darwin11.0.0/4.6.1 -Lbuild/temp.macosx-10.7-intel-2.7 -ldfftpack -lfftpack -lgfortran -o build/lib.macosx-10.7-intel-2.7/scipy/fftpack/_fftpack.so” failed with exit status 1
How do I fix this problem? I’m using Mac OSX 10.7.5
Hi Thomas, did you remember to install the Fortran compiler:
brew install gfortranAh, it turns out gfortran WASN’T installed properly.
I executed ‘brew install gfortran’ again and it turned out I needed to ‘brew link –overwrite’ some stuff.
Thanks!
← Previous Comments
{ 22 trackbacks }