Computational Mathematics

Adventures in Computing, Statistics, and R

How to install R packages from source on Mac OS X

3 Comments »

In my last post about installing the rgdal R package on Mac OS X, I had apparently glossed over the last step of compiling the R package from source.  I hadn’t realized that Mac OS X actually doesn’t come default with the necessary software to do this.  Most times, prepackaged binaries work fine but occasionally, packages may not have a binary available or may need to be customized.  For example, in rgdal, where the default search paths are incorrect for the GDAL frameworks on Mac OS X.

In order to compile a package from the source code, you’ll need the package source (typically a .tar.gz file on Mac OS X) and a compiler (e.g. gcc).  In my opinion, the easiest way to set up the necessary tools is to install Xcode from the Apple Developer site. You’ll need to register (free) for an Apple Developer Connection account to access the download.
(Update, May 20, 2011: Since version 4, Xcode is no longer free for non-developers but can be purchased for $4.99 from the Mac App Store. Xcode 3 remains free from the Apple Developer site and is sufficient if you just need the compilers installed).

Once Xcode has been installed, you’ll then have all the required tools to compile R packages from source.  To do so, simply navigate in Terminal to the folder you saved the source to (the .tar.gz file), and run:

R CMD INSTALL packagename.tar.gz

or some variety of the above with any custom configurations. The R command will automatically run the appropriate commands to compile the package. Happy compiling!

3 Responses

  1. It didn’t work for me. After downloading that massive file (at a hotel where I paid for premium service), I still get the same error:

    sh: make: command not found

    Suggestions welcome.

    Amber

    • In the latest version of Mac OS X (Lion), it appears that Xcode doesn’t install the command line tools necessary. However, if you open up Xcode and go to Preferences -> Downloads -> Components -> Command Line Tools, you can download them from there.

      Furthermore, you can also get osx-gcc-installer from https://github.com/kennethreitz/osx-gcc-installer instead of the very large developer tools.

  2. [...] a Mac, it is slightly more complicated (see e.g. Jon’s blog): one has to open a Terminal and to [...]

Leave a Reply