Installing R on a Mac

The easiest way to install R on the Mac OS is by installing the binary from http://cran.us.r-project.org/. However, when you start to use compiled code with R (especially code you have written) the opportunity to encounter various errors at compile time or run time increases. The easiest way around this is to make sure your R-related compiled codes (including R) are '''all''' built with the sample compiler tools. One easy way to do this is with a source code manager like Macports (http://www.macports.org/).

Below are the steps for a Macports-based solution ($ is the UNIX/Linux command prompt):

==== Install Macports ====

  • Follow the instructions at http://guide.macports.org/ for installing X11 and Xcode if necessary
  • Go to http://www.macports.org/install.php
  • Download the appropriate "pkg" installer for Macports
  • Begin the installation of Macports. The installer edits ~/.profile automatically to add certain locations to your path.

==== Install gcc 4.8 ====

$ sudo port install gcc48

This takes a couple of minutes.

==== Set the compiler ====

$ sudo port select --set gcc mp-gcc48

This sets the default compiler to the GCC compiler provided from Macports that we just installed. Now the variable "gcc" maps to the version set from within MP.

You can test this by looking at the output of <pre>gcc --version</pre>.

$ gcc --version
gcc (MacPorts gcc48 4.8.1_3) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

==== Install R ====

$ sudo port install R configure.cc=gcc configure.cxx=g++

We have to (unfortunately) tell the port to build using "gcc" and "g++" which are associated with the correct versions from the above step. This step takes some time.

==== Installing R packages consistently ====

Any subsequent R packages will build (by default) with the compilers that built R. So, at R-package installation time, you don't need to do anything special.

==== If something goes wrong ... ====
You can start fresh with

$ sudo port clean R
$ sudo port uninstall -f R