onsdag den 30. marts 2016

Installing python with matplotlib and basemap on mac

In the last couple of months I have installed python on two different macs. It took forever to get matplotlib and basemap to work the first time, before I understood that I could not use the build-in python from apple. I ended up installing python enthought using homebrew, and I promised myself that I would write down how to do it after the second installation. But I forgot. And now it's been a while, I got a new laptop and I have to install python once again.
So here it goes, for my future self and anybody else who can use it.


  • Install Xcode from the app store. It's free and can be found here. Make sure to open Xcode and agree to it's terms and conditions.

  • Download and install the enthought canopy python distribution. This is their free python version, which entails numpy, scipy and matplotlib.
    To finish the canopy installation it is necessary to open the application Canopy. If this is done by double-clicking the icon, it will give the error message "Enthought Canopy can't be opened because it is from an unidentified developer". To get around this, it is necessary to right-click (CTRL-click) on the icon of Canopy and choose "open". In the pop-up window choose "Open" - and the program installs.
    After this a new pop-up window appears asking "Do you want to make Canopy your default Python environment?", to which you answer "yes". 

  • Now we want install homebrew. Homebrew is a package manager similar to macports and fink. It is installed by opening the terminal and typing:

    $ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 

    You will be asked for your password and need to wait a few moments for the installation.
  • To install the Geos package type:

    $ brew install gdal 


    Note the location of the Geos library. For me:  /usr/local/Cellar/geos/3.5.0 
    The path written in the last line after installation indicates the location of the geos library, for me this was: /usr/local/Cellar/gdal/1.11.3_1  
  • To install Basemap it must first be downloaded. On this page, choose the newest version and download the tar-file for mac. Then go to the Downloads folder in the terminal and unpack the files:

    $ cd ~/Downloads/
    $ tar xf basemap-1.0.7.tar.gz 

    This creates a new folder with the name of the tar file, in my case ~/Downloads/basemap-1.0.7. You need to enter that folder:

    $ cd basemap-1.0.7
  • Then export the directory of geos library noted above and install baseman:

    $ export GEOS_DIR=/usr/local/Cellar/geos/3.5.0/
    To build and install write:

    $ sudo python setup.py build
    $ sudo python setup.py install

    You will be asked for your password, and the installation will take a little while to run.
  • And now python should work with basemap. You can test in the terminal by writing opening python and loading basemap:

    $ python
    >>> from mpl_toolkits import Basemap

    If this works without error messages everything is fine. For me it worked for the last two computers - this time it worked after a restart. Awesome.