Tristan Penman's Blog

Installing R and RStudio on Mac OS X

20 October 2015

Although installing R and RStudio on Mac OS X wasn’t particularly difficult, I thought I would make some notes here, in case they’re useful to someone else. These instructions make use of Homebrew, a convenient command line package manager for Mac OS X.

Homebrew

Let’s begin with some prerequisites.

The first thing you’ll need is Homebrew, which is described as ‘The missing package manager for OS X’. Once installed, Homebrew allows you to compile and install open source packages (or formulas) with a command as simple as:

brew install [formula]

where [formula] is the name of a package.

Follow the latest instructions on the Homebrew website to get that installed. Note that you may be asked to install the Xcode Command Line Tools - this will ensure that your system has a compiler, and the necessary libraries, to build other packages.

You will also need to install Homebrew Cask, which extends Homebrew with a workflow for managing Mac applications distributed as binaries:

brew install caskroom/cask/brew-cask

Install XQuartz

To make the most of R’s plotting and interactive tools, you’ll also need XQuartz. Mac OS X used to include an X11 implementation out-of-the-box, but it was later released as its own open source project. You can install XQuartz as a Homebrew cask:

brew cask install xquartz

If instructed to restart/log out, do so. R’s X11 support relies on certain environment variables being set, which is most easily achieved with a fresh log-in.

Compile R

Although R is not available in Homebrew’s default repository, you can provide a formula for R by installing an additional ‘tap’ before installing R itself:

brew tap homebrew/science

Now you can compile and install R itself. Note that we have to include the --with-x11 flag so that R is compiled with X11 support:

brew install --with-x11 r

Alternatively, you can do this as a one-liner by referencing the tap in the formula name:

brew install --with-x11 homebrew/science/r 

Either approach will ensure that all of R’s dependencies are installed.

Install RStudio

Finally, you can install RStudio. This can be installed using an official DMG file from the RStudio website, or you can install it from Homebrew Cask:

brew cask install --appdir=/Applications rstudio

Note the inclusion of the --appdir option. By default, Cask formula’s will create aliases in ~/Applications, but this option tells Homebrew to use /Applications instead.

You can check that everything is working (including X11 support) by starting RStudio and typing:

x11()

This should open a blank R Graphics window. Note that X11 support relies on certain environment variables being set, so if you have just installed XQuartz, you may need to restart your computer (or log out then log in again) to ensure that R can find X11.

That’s it! You’re ready to go.

Note: This post was updated 12 May 2016.