Computational Mathematics

Adventures in Computing, Statistics, and R

How to install R packages from source on Mac OS X


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!