A virtualized Python setup on OS X (Mountain) Lion
Written by Arvinder Singh / July 06, 2012 / 4 mins read / Filed under Python, / Osx, / Numpy, / Scipy, / Matlibplot, / Virtualenv
Update 2012-07-26: Tested the install successfully for Mountain Lion.
Yesterday I replaced my slow old hard drive with a shiny new SSD drive. In the process I decided to reinstall Python on my machine and document the process for future reference.
I have a strong preference for command line installer such as homebrew
, versioning system git
and isolating installations into virtual environments. Come follow along.
Xcode
Start by installing Xcode. You can download it for free from Apple app store.
Then install Command line tools for Xcode from https://developer.apple.com/downloads/
Git
From the website
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
Git is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenient staging areas, and multiple workflows.
- Download and run installer from http://git-scm.com/.
- Restart bash/zsh sessions
- Run provided shell script to add PATH and MANPATH to your ~/.MacOSX/environment.plist file
Homebrew
Homebrew allows you to install Unix tools on OS X. A much better replacement of fink.
/usr/bin/ruby -e "$(/usr/bin/curl -fsSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"
brew doctor
If your system has no issues, brew would return
Your system is raring to brew.
Python
Lion already comes installed with version 2.7.1. However we will build virtual environment to isolate and install more than one versions of python as well as corresponding modules.
Prerequisites
readline: The GNU Readline library provides a set of functions for use by applications that allow users to edit command lines as they are typed in.
sudo easy_install readline
gdbm: GNU dbm is a library of database functions that use extensible hashing and work similar to the standard UNIX dbm. These routines are provided to a programmer needing to create and manipulate a hashed database.
pkg-config : pkg-configis a helper tool used when compiling applications and libraries and helps you insert the correct compiler options on the command linerather than hard-coding values on where to find libraries. It is language-agnostic.
sqlite: SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine.
brew install readline sqlite gdbm pkg-config
pip: A better package manager for Python
sudo easy_install pip
virtualenv
Virtualenv lets us install isolated Python environment, each with its own set of packages and dependencies. It is similar to rvm
in Ruby.
Lets install virtualenv and virtualenvwrapper (a tool to easily manage virtualenv enviroments).
sudo pip install virtualenv
sudo pip install virtualenvwrapper
To load virtualenv, we have to source /usr/local/bin/virtualenvwrapper.sh
script. We can automate by adding to ~/.bashrc
or in my case ‘~/.zshrc’ depending on the shell you use.
To use virtualenv refer http://www.doughellmann.com/docs/virtualenvwrapper/command_ref.html#command
Python tools
mkvirtualenv ML
workon ML
As numpy and scipy will fail without fortran compiler
brew install gfortran
Install NumPy
pip install numpy
Install SciPy
pip install scipy
Although OSX comes with the prerequisites for matplotlib installed, setup.py is not able to locate them. Therefore we’ll use
export LDFLAGS="-L/usr/X11/lib"
export CFLAGS="-I/usr/X11/include -I/usr/X11/include/freetype2 -I/usr/X11/include/libpng12"
Now install matplotlib
pip install matplotlib
R
R is a free software environment for statistical computing and graphics.
brew install R
Link R
ln -s "/usr/local/Cellar/r/2.15.1/R.framework" /Library/Frameworks
Enable rJava support
R CMD javareconf JAVA_CPPFLAGS=-I/System/Library/Frameworks/JavaVM.framework/Headers
Last we’ll install iPython. If you like the default python shell I recommend a good ~/.pythonrc
. Here is a good starting point by Seth House.
pip install ipython
Now Python shell is ready.
Workflow
After launching terminal, to move into virtualenv - ML
in our case
workon ML
Listing installed packages installed in the virtualenv (pip packages stored here for env ML ~/.virtualenvs/ML/)
pip freeze
Install/Upgrade/Uninstall packages
pip install packageName
pip install -U packageName
pip uninstall packageName
Leave virtualenv
deactivate ML