Installing R on OS X – “100% Homebrew Edition”

In a previous post I provided “mouse-heavy” instructions for getting R running on your Mac. A few of the comments suggested that an “all Homebrew” solution may be preferable for some folks. Now, there are issues with this since getting “support” for what may be R issues will be very difficult on the official mailing lists as you’ll immediately be told to “use the official distribution” by some stalwart R folks (this happens on StackOverflow and other forums as well). However, if you have a thick skin and can be somewhat self-sustaining, Homebrew is a superb alternative to setting up your R environment (and other things) on your OS X system.

What is ‘Homebrew’?

Homebrew is the “missing package manager for OS X”. It’s similar to apt, yum and other package managers on linux/BSD that enable you to install open source (and other types of) packages without having to do the download→unarchive→compile→curse→google→compile→curse→google some more→compile→smile→test→install dance manually. MacPorts is another third-party package manager for OS X, but I use Homebrew, so you get Homebrew examples here.

Homebrew’s inventory of packages comes from github repositories that contain “Formulas” for where to get package components and how to (literally) make them work on OS X. Here’s a small-ish example (some Formula are fairly long/involved/complex) of a Homebrew Formula for the cowsay utility (what, you don’t like to have ASCII animals give you handy messages?):

class Cowsay < Formula
  desc "Configurable talking characters in ASCII art"
  homepage "https://web.archive.org/web/20120225123719/http://www.nog.net/~tony/warez/cowsay.shtml"
  url "http://ftp.acc.umu.se/mirror/cdimage/snapshot/Debian/pool/main/c/cowsay/cowsay_3.03.orig.tar.gz"
  sha256 "0b8672a7ac2b51183780db72618b42af8ec1ce02f6c05fe612510b650540b2af"

  bottle do
    cellar :any_skip_relocation
    revision 1
    sha256 "c041ce7fbf41fd89bf620ae848e3b36fe1e69ab3e2dfca18bc2f2e79cfe8063a" => :el_capitan
    sha256 "ffacfb987481394174267fd987dea52607825e3542d1ea3d0b7aa4ccf7ea5cc5" => :yosemite
    sha256 "12c41b969af30817a4dc7ec25572fe1b707b9d4dcb46d8cc06d22264594219c1" => :mavericks
  end

  # Official download is 404:
  # url "http://www.nog.net/~tony/warez/cowsay-3.03.tar.gz"

  def install
    system "/bin/sh", "install.sh", prefix
    mv prefix/"man", share
  end

  test do
    output = shell_output("#{bin}/cowsay moo")
    assert output.include?("moo")  # bubble
    assert output.include?("^__^") # cow
  end
end

It has:

  • a description of what the package is
  • the official location of the program/libraries “home”
  • where the main URL of the contents of the program/library is
  • weird hex strings to help the Homebrew ecosystem now pwn you
  • instructions for how to install (with optional patching of problematic code on particular setups)
  • test/validation instructions

(You can/should overlook the fact they use icky Ruby for this whole thing.)

There are thousands of Formula in the main Homebrew repository and you can “tap” other (properly organized) GitHub repositories for other (usually task-specific) formula. We’ll need to do this for R.

Finally, the Homebrew community has also come up with the notion of Casks where actual binary OS X programs (and other things) can be installed. We’ll use this as well for some ancillary components.

Apart from the ease of initial setup, the truly compelling part of using Homebrew is that all it takes to update components/libraries is to do a:

brew update && brew upgrade

from a Terminal prompt. You should get into the habit of issuing those commands daily-ish.

Yes, you will need to become comfortable in the Terminal (or, preferably, iTerm 2) to use the Homebrew ecosystem (though there are some efforts to make this more GUI-user friendly).

Using Homebrew to Create & Maintain Your R Installation

I won’t provide much (if any) color commentary to the commands below. Suffice it to say that in a few short lines of a script, you’ll end up having:

  • R (with gfortran and the vast majority of required support libraries for many packages)
  • Oracle Java (a later step in the sequence ensures R knows about your Java install)
  • X11 (XQuartz)
  • MacTex
  • RStudio
  • extra SVG, XML, curl, geo-munging and C++ support libraries
  • A cool font for RStudio (FiraCode, though that’s not necessary)
  • iTerm 2 (optional)
  • GitUp git gui (optional)

If it’s your first time using Homebrew you’ll need to do this:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

NOTE that I’m generally against piping output of curl to run raw on your system without validation, so you can download https://raw.githubusercontent.com/Homebrew/install/master/install and verify it (or ask a security friend to verify it), but you’ll be trusting the Homebrew ecosystem to not pwn your system for the rest of your time using it, so as long as you trust that I gave you the proper URL to get to the installer, cut/paste away.

Once Homebrew is setup I’d recommend copying and pasting each line (below) one-by-one to get familiar with Homebrew operations and messaging.

This can be a pretty scary experience if you’re not used to the command-line, so drop a note in the comments or on Twitter (target @hrbrmstr and use the #rstats #homebrew tags) if some things go awry or get confusing and I’ll try to help as much as I can.

brew tap caskroom/cask
brew install brew-cask

brew install Caskroom/cask/xquartz
brew cask install java

brew tap homebrew/science
brew install R --with-openblas # added --with-openblas based on a spiffy comment by Lincoln Mullen

brew install Caskroom/cask/rstudio


# For latex:
brew cask install mactex

# OR YOU CAN DO
brew install gnupg
brew cask install basictex # suggested by @noamross
# plus the following, provided by Kras.Pten
# read why here: http://rud.is/b/2015/10/22/installing-r-on-os-x-100-homebrew-edition/#comment-10603
sudo tlmgr update –-self 
sudo tlmgr update –-all 
sudo tlmgr install titling framed inconsolata

# DO NOT DO BOTH!

brew install libsvg curl libxml2 gdal geos boost

R CMD javareconf JAVA_CPPFLAGS=-I/System/Library/Frameworks/JavaVM.framework/Headers

brew tap caskroom/fonts
brew cask install font-fira-code
brew cask install iterm2
brew cask install gitup           # if you want a GUI for git stuff (h/t @jennybryan)

That’s quite a bit less clicking/typing from what was required in the previous post.

Fin

I validated that entire configuration on a completely fresh installation of El Capitan (OS X 10.11) in a VM. At the end, I had a fully-functioning data-science setup. Hopefully, you will as well.

If you have suggestions for other Homebrew things to add to make a good “data science OS X setup”, drop a note in the comments!

P.S.

Once you have a full Homebrew & “Cask” setup, the way to keep up-to-date with everything is more like:

brew update && brew upgrade brew-cask && brew cleanup && brew cask cleanup

but an complete “how to use Homebrew” guide is beyond the scope of this post.

Cover image from Data-Driven Security
Amazon Author Page

27 Comments Installing R on OS X – “100% Homebrew Edition”

    1. Kras.Pten

      Wth basictex, to make pdfs on Rstudio Rmarkdown I needed to add some stuff:

      sudo tlmgr update –self
      sudo tlmgr update –all
      sudo tlmgr install titling framed inconsolata

      Reply
  1. Lincoln Mullen

    One of the downsides to using the homebrew version instead of the R binary from CRAN is that the homebrew version can only use options(pkgType = "source"), while the CRAN binary can use options(pkgType = "both"). The ability to install binary packages for, say, dplyr and rgdal can save quite a bit of time and effort.

    If one is going to install R via homebrew, I think it is worth doing so again OpenBLAS.

    brew install R --with-openblas

    This can lead to a significant speedups, the last time that I checked it against this benchmark.

    Reply
    1. hrbrmstr

      thx. there are prbly a cpl other compile options i shld add at some point to a more advanced “compile R on your own on OS X” post. however, i added that one (with credit) to the main post.

      Reply
    2. Lincoln Mullen

      I re-ran these benchmarks, and the version of R that come pre-compiled with brew install r marginally beats the version compiled agains OpenBLAS with brew install r --with-openblas. Given that it can be a pain to compile yourself with homebrew against OpenBLAS, I retract my suggestion.

      Reply
  2. Pingback: Distilled News | Data Analytics & R

  3. Pingback: Installing R on OS X – “100% Homebrew Edition” | Dinesh Ram Kali.

  4. Noam Ross (@noamross)

    One more thing: You can install the binary of Revolution R Open with brew cask install rro. This is essentially R built with MKL instead of OpenBLAS, whichI believe are similar in performance. But this installs the binary, so you should be able to use `options(pkgType = “both”).

    Reply
  5. JacobMiller

    Thanks for composing this, super clear and helpful.

    I ended up opting for mactex, b/c it just worked. Whereas, after the basictex install I ran into (I think) a path problem with tlmgr. t was installed but I ran the update cmds

    Reply
  6. Simon Garnier

    I think the following lines are incorrect:

    sudo tlmgr update –self
    sudo tlmgr update –all

    It should be:

    sudo tlmgr update –self
    sudo tlmgr update –all

    It returns an error if you use the first version.

    Reply
    1. hrbrmstr

      It leaves out “A” GUI :-) RStudio > R.app, I’m afraid (though I will ACK that this is a personal pref more than an absolute standard)

      Reply
  7. Pingback: Installing R in OS X with Homebrew and Cask | BionicProfessor

  8. Esmaeil

    Homebrew seems to think “brew install brew-cask” is not necessary as brew-cask is now a part of brew.

    me:bsssetup khaksari$ brew install brew-cask
    ==> Installing brew-cask from caskroom/cask
    ==> Cloning https://github.com/caskroom/homebrew-cask.git
    Updating /Users/me/Library/Caches/Homebrew/brew-cask–git
    ==> Checking out tag v0.60.1
    ==> Caveats
    You must uninstall this formula. It is no longer needed to stay up to date,
    as Homebrew now takes care of that automatically.

    Reply
  9. middlepathway

    I had some serious issues installing R with homebrew. Everything from installing packages to odd forced lib paths. I would recommend people running OSX El Capitan don’t installing R using homebrew unless they actually know what they are doing. A quick good reveals a lot of others with similar problems. I wan’t stability, not hackability.

    Reply
    1. hrbrmstr

      This has not been my experience on numerous installations and is not the experience of many, many other MacOS (new name for the OS coming this week I think) and R users. This is a very viable option for folks who like to be able to manage their systems from a Terminal prompt via GUI.

      Reply
      1. Johan

        Agreed on both sides here. Normally a big fan of using brew but had a nightmare getting packages to install with many failing without a forced CPPFLAGS or LDFLAGS.

        Reply
  10. carycorreia

    This is a great article but it didn’t quite work for me. Turns out that X11 would not be seen by R….I eventually had to remove R, Rstudio and xquartz and install them the conventional way.

    Has anyone else seen this issue ?
    If so, is there a fix ?.. l like the convenience of using a package manager to keeps things all up to date.
    any help would be appreciated.

    Reply
  11. Pingback: Homebrew intro – onesixx.com

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.