Getting started with scientific Python on Mac and Linux

This post elaborates on a couple of ways to install Python libraries Numpy/Scipy/Matplotlib for Linux and Mac. This is something I seem to have to do every so often, and although the process should be straightforward, the details elude me each time. May this serve as a reminder to future me (and others).

Note: For all platforms (including Windows) it is possible to install compiled binaries as well. I won’t go into this here, because I prefer the use of package managers. They take care of compiling the software, putting it in the right place and updating so you don’t have to worry about it.

macOS

A fresh install comes with a default version of Python 2, where most of the scientific packages are provided. Unfortunately, Python 2 is deprecated and the new version, Python 3 is not included. Additionally, there is no mechanism to install new packages or to update them to recent versions. Therefore, it is recommended to install a Python distribution yourself.

For macOS, you have two popular options. Use general package manager Homebrew to install pip, the package manager for Python, or install a ready-made distribution like Conda, which offers already compiled packages in a complete ecosystem. I recommend the use of pip, the time-tested platform independent package installer. Although conda is very user-friendly and has a large repository for not only Python, but also related (scientific) software packages, it tends to be quite difficult to manage when software does not run out-of-the-box directly. Packages from pip integrate nicely with your system, although you sometimes need to install extra libraries (using Homebrew for instance).

Homebrew

If you don’t have it yet, download it here (and see what else they have to offer!). After installation, run the following commands to hit the ground running. Installing python3 gives you a Python 3 install containing pip and gfortran is required to compile modules in Scipy.

NB: when installing packages, use pip3 for Python 3! pip installs packages for Python 2.

brew install python3 gcc
pip3 install numpy scipy matplotlib ipython

Conda

Conda comes in two sizes: the fully packed Anaconda containing 150+ (scientific) libraries, and the light version Miniconda, which requires less space and allows you to selectively install the packages you need. Installing Anaconda requires no further steps. If you install Miniconda, you can install the other necessary packages with

conda install -c anaconda gcc freetype
conda install numpy scipy ipython gcc
conda install -c conda-forge matplotlib

freetype is required to use matplotlib, while gcc installs the Fortran compiler required for more custom Scipy packages.

NB: matplotlib does not work out of the box. It is not fully compatible with the GUI of macOS. The creaters of Matplotlib seem to be aware of the problem (check here), but apart from some workarounds, no solution has been proposed at this point (hence why I recommend going the Homebrew + pip route).

Linux

For most Linux distributions, the process is less involved. Usually, pip is installed by default, and if it isn’t, the software reposity has it available. These commands have been tested on Ubuntu 14.04 and 16.04. For other Linux distributions (Debian-like or otherwise), the process should be roughly the same.

The rest of the tutorial assumes a fresh Ubuntu 16.04 or 14.04 install, where apt-get is available.

sudo apt-get update
sudo apt-get install python3-pip # if you haven't installed the package manager yet
sudo apt-get install python3-tk # Backend for matplotlib
# sudo apt-get install python3-dev python3-devel
pip3 install numpy scipy matplotlib

Depending on your system, you may need to prepend the last command with sudo. The difference is that then, packages are installed system-wide.

Alternative: Using the apt-get

While using apt-get is a little less involved on a clean install, the disadvantage is that packages are usually a little outdated; this problem does not happen with pip.

Ensure that you have enabled the universe repository (‘Software & Updates > Select sources’)

sudo apt-get update
sudo apt-get install python3-numpy python3-scipy python3-matplotlib

Alternative: conda

If you have no sudo rights on your machine, conda is an available option here as well. Just follow the same steps as displayed for macOS.

Test your installation

Below follows a code snippet that makes use of each of the libraries. You can use it to make sure your installation completed sucessfully. The result should be a surface plot.

import numpy as np
from scipy.interpolate import RectBivariateSpline
import matplotlib.pyplot as plt

n = 6
data = np.random.random([n,n])
func = RectBivariateSpline(np.linspace(0,1,n),np.linspace(0,1,n),data)
grid = np.meshgrid(np.linspace(0,1),np.linspace(0,1))
plt.imshow(func.ev(grid[0],grid[1]))
plt.show()