Cover image for Setup OSX for R

Setup OSX for R

bhaskar_vk profile image Bhaskar Karambelkar ・7 min read

Originally Published on my Personal Blog.

This is a rather details documentation of my OSX setup for R usage using homebrew. You can get away with a lot less than what I have installed below, but this is fairly comprehensive and I have also given some steps to verify installation of various packages.

Github API Token

Get a github account and setup an API token as described here. This helps you avoid hitting Github API limits when using homebrew.

echo 'export HOMEBREW_GITHUB_API_TOKEN="your_new_token"' >> $HOME/.bash_profile
. $HOME/.bash_profile

Install Homebrew and tap some taps

/usr/bin/ruby -e \
    "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew analytics off # full on paranoid mode
brew tap caskroom/cask # GUI apps
brew tap caskroom/fonts # fonts
brew tap homebrew/science
brew tap homebrew/completions
brew tap homebrew/services
brew tap homebrew/versions
brew tap osgeo/osgeo4mac # If you need Geospatial stuff


Install Bash and verify

brew install bash bash-completion2
/usr/local/bin/bash --version
GNU bash, version 4.3.46(1)-release (x86_64-apple-darwin15.5.0)

Setup bash along with some utility functions. Primarily we setup brewPkg bash function which will not only install homebrew packages but also log the output for debugging purposes if so desired.

sudo sh -c 'echo "/usr/local/bin/bash" >> /etc/shells'

mkdir -p $HOME/Library/Logs/Homebrew/$USER

echo '
if [ -f $(brew --prefix)/share/bash-completion/bash_completion ]; then
  . $(brew --prefix)/share/bash-completion/bash_completion

brewPkg() {
    brew install ${pkg} $*  2>&1 |
        tee $HOME/Library/Logs/Homebrew/$USER/${pkg}-$(date +"%F_%H%M").txt

brewSrcPkg() {
    brew install --build-from-source  ${pkg} $*  2>&1 |
        tee $HOME/Library/Logs/Homebrew/$USER/${pkg}-$(date +"%F_%H%M").txt

brewSrcPkgWgcc() {
    export CC=gcc-6
    export CXX=g++-6
    export HOMEBREW_CC=gcc-6
    export HOMEBREW_CXX=g++-6
    brew install --build-from-source  ${pkg} $*  2>&1 |
        tee $HOME/Library/Logs/Homebrew/$USER/${pkg}-$(date +"%F_%H%M").txt

' >>  ~/.bash_profile

Load the new bash shell, so we can use all the auto-complete goodies. Or simply close and restart the Terminal App.

/usr/local/bin/bash -l

GCC Compiler and Autotools

Install gcc, without multilib so that OpenMP works.

brewPkg gcc --without-multilib
/usr/local/bin/gcc-6 --version
gcc-6 (Homebrew gcc 6.1.0 --without-multilib) 6.1.0
/usr/local/bin/gfortran --version
GNU Fortran (Homebrew gcc 6.1.0 --without-multilib) 6.1.0

Setup aliases for homebrew's gcc

cd /usr/local/bin
ln -s gcov-6 gcov
ln -s gcc-6 gcc
ln -s g++-6 g++
ln -s cpp-6 cpp
ln -s c++-6 c++
cd -

Install ccache to speed up compilation

brewPkg ccache
/usr/local/bin/ccache --version
ccache version 3.2.7

Install autotools, pkg-config, and cmake

brewPkg cmake pkg-config autoconf automake

Let's make sure OpenMP is working as expected.

cat > omp-test.c <<"END"
#include <omp.h>
#include <stdio.h>
int main() {
    #pragma omp parallel
    printf("Hello from thread %d, nthreads %d\n", omp_get_thread_num(), omp_get_num_threads());
gcc-6 -fopenmp -o omp-test omp-test.c 

You should see something similar but not exactly the same.

Hello from thread 1, nthreads 8
Hello from thread 6, nthreads 8
Hello from thread 4, nthreads 8
Hello from thread 2, nthreads 8
Hello from thread 5, nthreads 8
Hello from thread 0, nthreads 8
Hello from thread 3, nthreads 8
Hello from thread 7, nthreads 8

Misc libs

brewPkg freetype fontconfig  pixman gettext


Setup and verify openssl and libressl.

brewPkg  openssl
/usr/local/opt/openssl/bin/openssl version
OpenSSL 1.0.2h  3 May 2016
brewPkg  libressl
/usr/local/opt/libressl/bin/openssl version
LibreSSL 2.3.6
brewPkg libssh2

wget and curl

brewPkg wget
/usr/local/bin/wget --version
GNU Wget 1.18 built on darwin15.6.0.
brewPkg curl
/usr/local/opt/curl/bin/curl-config --version
libcurl 7.50.0


brewPkg gpg2 --with-readline
/usr/local/bin/gpg2 --version
gpg (GnuPG) 2.0.30
libgcrypt 1.7.2
brewPkg gpgme
/usr/local/bin/gpgme-config --version


brew cask install java
java -version
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)


We need an X-Server. This takes a lot of time so be patient.

brew cask install xquartz


Install python2

brewPkg python
pip install --upgrade pip setuptools
/usr/local/bin/python -V
Python 2.7.12

Install Python3

brewPkg python3
pip3 install --upgrade pip setuptools wheel
/usr/local/bin/python3 -V
Python 3.5.2


brewPkg git --with-blk-sha1 --with-gettext \
    --with-pcre --with-persistent-https
/usr/local/bin/git --version
git version 2.9.2

Boost libs w/ dependencies

Install icu4c library for Unicode and globalization

brewPkg icu4c
/usr/local/opt/icu4c/bin/icu-config --version

Install libxml2, libiconv, and libxslt.

brewPkg libxml2 libiconv libxslt
brew link libxml2 --force
/usr/local/bin/xml2-config --version

Install boost. Ignore the warning at the end.

brewPkg boost --with-icu4c --with-mpi --without-single

Boost is a beast of a library, so we need some quick programs to test whether it has installed successfully.
Shamelessly copied/adapted from the Intertubes. Ignore the warning messages spewed by the compiler.

cat > first.cpp <<END
int main()
    boost::any a(5);
    a = 1.61803;
    std::cout << boost::any_cast<double>(a) << std::endl;
clang++ -o first first.cpp
cat > second.cpp <<END
#include <boost/filesystem.hpp>
int main() 
    boost::filesystem::path full_path( boost::filesystem::current_path() );
    if ( boost::filesystem::exists( "second.cpp" ) )
        std::cout << "Found second.cpp file in " << full_path << std::endl;
    } else {
        std::cerr << "Argh!, Something not working" << std::endl;
        return 1;
clang++ -o second second.cpp \
    -lboost_filesystem-mt -lboost_system-mt
Found second.cpp file in "/Users/brewmaster"

Latex Support

Take a Coffee break and then some, because this is a huge one.

brew cask install mactex


Install libsvg, librsvg, and cairo.

brewPkg cairo 
brewPkg libsvg
brewPkg librsvg
brewPkg pandoc

Openblas for speedier linear algebra in R.

brewPkg openblas --with-openmp

Test openblas. Shamelessly copied from Intertubes.

cat > test-openblas.c <<"END"
#include <cblas.h>
#include <stdio.h>

void main()
  int i=0;
  double A[6] = {1.0,2.0,1.0,-3.0,4.0,-1.0};         
  double B[6] = {1.0,2.0,1.0,-3.0,4.0,-1.0};  
  double C[9] = {.5,.5,.5,.5,.5,.5,.5,.5,.5}; 
  cblas_dgemm(CblasColMajor, CblasNoTrans, CblasTrans,
    3,3,2,1,A, 3, B, 3,2,C,3);

  for(i=0; i<9; i++)
    printf("%lf ", C[i]);

clang -L/usr/local/opt/openblas/lib \
    -I/usr/local/opt/openblas/include \
    -lopenblas -lpthread \
    -o test-openblas test-openblas.c

11.000000 -9.000000 5.000000 -9.000000 21.000000 -1.000000 5.000000 -1.000000 3.000000 

Eigen and Armadillo for Rcpp and v8 for R+v8.

brewPkg eigen
brewPkg armadillo --with-hdf5
brewPkg v8-315
brew link v8-315 --force

Test Armadillo

cd /usr/local/opt/armadillo/examples/
clang++   -O2   -o example1  example1.cpp  -larmadillo -framework Accelerate

You should see something like below and lot more.

Armadillo version: 7.200.2 (Plutocratic Climate Change Denialist)
A.n_rows: 2
A.n_cols: 3

Test v8

echo 'quit()' | v8
V8 version [sample shell]

Finally install R itself, and also setup R to use Apple's clang compiler. We can also setup R to use gcc compiler to take advantage of openmp support, but I've noticed that not all R packages compile correctly when using GCC.

brewPkg r --with-openblas --with-pango
# for rJava to work.
R CMD javareconf \

# Setup $HOME/.r/Makevars file to properly link against homebrew packages.
mkdir $HOME/.r
cat > $HOME/.r/Makevars << END
CC=ccache clang
CXX=ccache clang++
SHLIB_CXXLD=ccache clang++
MAKE=make -j8
PKG_LIBS += -L/usr/local/opt/icu4c/lib -L/usr/local/lib

# Also setup a R_LIBS_USER directory to install R packages locally.
mkdir -p $HOME/Library/R/3.x/library
cat > $HOME/.Renviron <<END
export R_LIBS_USER=$HOME/Library/R/3.x/library

# add same stuff to .bash_profile
cat $HOME/.Renviron >> $HOME/.bash_profile

GIS Stuff

Mostly PostGIS + Geo libs.

brewPkg postgresql
brewPkg geos
brewPkg proj
brewPkg gdal2 \
    --with-armadillo --with-complete --with-libkml \
    --with-opencl --with-postgresql --with-unsupported
brewPkg postgis --with-gui

Other Programming Languages

Some other programming languages I use occasionally.


brewPkg node
brewPkg phantomjs casperjs


brew install scala


brew install golang
cat >> $HOME/.bash_profile <<END
export GOPATH=$HOME/golang
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin

Other Interesting Stuff

brewPkg imagemagick --with-fontconfig --with-ghostscript \
    --with-librsvg --with-pango --with-webp
brewPkg vim --with-python3 --with-client-server \
    --with-lua --with-luajit --with-override-system-vi

brewPkg macvim --with-python3 --with-client-server \
    --with-lua --with-luajit --with-override-system-vim

brewPkg jq # json processing

brew cask install font-hack font-fira-code #extra fonts

brewPkg mlpack # Fast Machine Learning
brewPkg protobuf --devel # Google's Protocol Buffer Library
brewPkg gsl # GNU Scientific Library
brewPkg libyaml # YAML Support

GUI Apps

Apps related to Securing your Mac.

brew cask install blockblock knockknock \
    dhs taskexplorer kextviewr
brew cask install suspicious-package

Quicklook Plugins for Developers.

brew cask install qlcolorcode qlstephen qlmarkdown \
    quicklook-json qlprettypatch quicklook-csv betterzipql \
    qlimagesize webpquicklook

These are some GUI apps I use. Pick and chose as you like. They are not necessarily related to R or even development.

brew cask install google-chrome chrome-devtools firefox iterm2 seil \
 slate keepassx free-mind itsycal flux caffeine alfred beardedspice \
 macdown mysqlworkbench osxfuse smcfancontrol torbrowser vagrant\
 vagrant-manager vlc cog yed slack owncloud 


Editor guide
shayanlinux profile image
Shayan Shahand

Awesome stuff! Many thanks. Just wanted to add that using Anaconda to install and manage Python and R environments, and using SDKman to install Java and Scala stacks is generally a better idea.

bhaskar_vk profile image
Bhaskar Karambelkar Author

The R channel of anaconda is woefully outdated. I won't recommend it just for that reason. I was not aware of SDKman so thanks for bringing it to my attention.

chipoglesby profile image
Chip Oglesby

Looks like this would be the perfect case for a docker image, no?

habukagumba profile image
Herbert Kagumba

Is it normal for the gcc install to take forever?

bhaskar_vk profile image
Bhaskar Karambelkar Author

Wait till you get to the mactex part. ;)