Painless installation of scientific programming software on a Mac computer

1. Introduction

As a scientist who works with computer models, it took me a long time to adapt switching from a Linux computer to a Mac computer. This was essentially because all the information I needed was quite spread over many technical documents and forum discussions. I decided to write this post simply as a collection of notes that I used to put together my own system, and that hopefully could spare someone else to spend hours looking for them. What follows is intended to be straightforward instructions for Mac owners to set up their computers to work with scientific programming (which often relies on Fortran and C compilers). The following notes were tested and confirmed to work on macOS High Sierra (10.13), Mojave (10.14) and Catalina (10.15).

2. Xcode

First of all, you will need to install the latest Xcode and command line tools. These applications distributed by Apple itself will provide the fundamental tools to develop software: Xcode installs the Apple C compilers and command line tools will provide automake, autoreconf, basic libraries and etc. The easiest way to get Xcode is through your Mac's App Store. Just search for Xcode and install the first occurence returned.

Note: Xcode is currently (as of version 11.3) a massive - ~8 Gb - download. Downloading it will take a while, so in the meantime I suggest you to go grab a beer or keep using your computer for something else that doesn't rely on the internet connection.

Once Xcode is downloaded and installed, install the command line tools by typing the following command on your terminal application:

xcode-select --install

It will then download (about 150 Mb) and install all basic tools for development that we need.

3. X11 Server

The way to go here is to install the latest XQuartz at http://www.xquartz.org. Installing XQuartz is a requirement to the next step.

4. MacPorts

Now let's install MacPorts, which is a package manager for macOS. If you are familiar to unix, a good analogy is that MacPorts is similar to Ubuntu's aptitude or Suse's zypper. The MacPorts installer is available at http://www.macports.org/install.php. Once you finish installing MacPorts, go to the terminal and refresh its list of packages:

sudo port selfupdate

The fundamental packages we need to get are the GNU compilers gcc and gfortran. Virtually every scientific software we will install from now on depends on these compilers. First, let's check which versions of gcc are available in the repository:

port list | grep gcc

At the time of this posting we have all versions ranging from gcc43 to gcc9. Since I like to use the most up-to-date software version that is available, let's choose GCC 9. You can install both gcc and gfortran by typing:

sudo port install gcc9

The entire process will take a few hours to complete, since the packages will be compiled from the source. On my Macbook Pro Mid 2012 (dual-core 2.5 GHz), it took nearly 4 hours to finish installing gcc and its depedencies. In the meantime, time to go grab one more beer…

Note: by default MacPorts will install executables for the fortran and C compilers under /opt/local/bin. To set them as default on your system, just type:

sudo port select --set gcc mp-gcc9

From now on, all you have to do is go ahead and install your favourite libraries and programs. Since I work with climate data in my research, NetCDF is the first library I want to install:

sudo port install netcdf +gcc9 +dap +netcdf4 +hdf4
sudo port install netcdf-fortran +gcc9

Note that the plus sign (“+”) in the command above indicates a variant of the MacPorts package. To see all the variants available for a certain package, type port variants <package_name>. For example:

port variants netcdf

In the example above, the variants +gcc9 +dap +netcdf4 +hdf4 indicate that I want my NetCDF libraries to be compiled with gcc9 (which we already installed). It also tells MacPorts to install the DAP, NetCDF version 4 and HDF4 extra capabilities to the libraries.

Besides NetCDF, I will proceed installing CDO, NCO and GDAL, which are also useful applications to handle spatial gridded data.

# CDO
sudo port install cdo +gcc9 +szip +grib_api
# NCO
sudo port install nco
# GDAL
gdal +gcc9 +netcdf +geos +grib +hdf4 +hdf5 +curl +jasper +openjpeg

I hope these notes are useful to someone else. If you have any questions or comments do not hesitate to contact me. Cheers!