DEV Community

Cover image for Setup OSX for R
Bhaskar Karambelkar
Bhaskar Karambelkar

Posted on

Setup OSX for R

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Bash

Install Bash and verify

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


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
fi

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

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

brewSrcPkgWgcc() {
  pkg=$1
  shift
  (
    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
Enter fullscreen mode Exit fullscreen mode


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
Enter fullscreen mode Exit fullscreen mode

GCC Compiler and Autotools

Install gcc, without multilib so that OpenMP works.

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


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 -
Enter fullscreen mode Exit fullscreen mode


Install ccache to speed up compilation

brewPkg ccache
/usr/local/bin/ccache --version
Enter fullscreen mode Exit fullscreen mode
ccache version 3.2.7
Enter fullscreen mode Exit fullscreen mode


Install autotools, pkg-config, and cmake

brewPkg cmake pkg-config autoconf automake
Enter fullscreen mode Exit fullscreen mode


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());
}
END
gcc-6 -fopenmp -o omp-test omp-test.c 
./omp-test
Enter fullscreen mode Exit fullscreen mode


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
Enter fullscreen mode Exit fullscreen mode

Misc libs

brewPkg freetype fontconfig  pixman gettext
Enter fullscreen mode Exit fullscreen mode

SSL/SSH Libs

Setup and verify openssl and libressl.

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

wget and curl

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

GPG

brewPkg gpg2 --with-readline
/usr/local/bin/gpg2 --version
Enter fullscreen mode Exit fullscreen mode
gpg (GnuPG) 2.0.30
libgcrypt 1.7.2
Enter fullscreen mode Exit fullscreen mode
brewPkg gpgme
/usr/local/bin/gpgme-config --version
Enter fullscreen mode Exit fullscreen mode
1.6.0
Enter fullscreen mode Exit fullscreen mode

Java

brew cask install java
java -version
Enter fullscreen mode Exit fullscreen mode
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)
Enter fullscreen mode Exit fullscreen mode

X-server

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

brew cask install xquartz
Enter fullscreen mode Exit fullscreen mode

Python

Install python2

brewPkg python
pip install --upgrade pip setuptools
/usr/local/bin/python -V
Enter fullscreen mode Exit fullscreen mode
Python 2.7.12
Enter fullscreen mode Exit fullscreen mode


Install Python3

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

Git

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

Boost libs w/ dependencies

Install icu4c library for Unicode and globalization

brewPkg icu4c
/usr/local/opt/icu4c/bin/icu-config --version
Enter fullscreen mode Exit fullscreen mode
57.1
Enter fullscreen mode Exit fullscreen mode


Install libxml2, libiconv, and libxslt.

brewPkg libxml2 libiconv libxslt
brew link libxml2 --force
/usr/local/bin/xml2-config --version
Enter fullscreen mode Exit fullscreen mode
2.9.4
Enter fullscreen mode Exit fullscreen mode


Install boost. Ignore the warning at the end.

brewPkg boost --with-icu4c --with-mpi --without-single
Enter fullscreen mode Exit fullscreen mode


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
#include<iostream>
#include<boost/any.hpp>
int main()
{
    boost::any a(5);
    a = 1.61803;
    std::cout << boost::any_cast<double>(a) << std::endl;
}
END
clang++ -o first first.cpp
./first
Enter fullscreen mode Exit fullscreen mode
1.61803
Enter fullscreen mode Exit fullscreen mode
cat > second.cpp <<END
#include<iostream>
#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;
    }
}
END
clang++ -o second second.cpp \
    -lboost_filesystem-mt -lboost_system-mt
./second
Enter fullscreen mode Exit fullscreen mode
Found second.cpp file in "/Users/brewmaster"
Enter fullscreen mode Exit fullscreen mode

Latex Support

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

brew cask install mactex
Enter fullscreen mode Exit fullscreen mode

R


Install libsvg, librsvg, and cairo.

brewPkg cairo 
brewPkg libsvg
brewPkg librsvg
brewPkg pandoc
Enter fullscreen mode Exit fullscreen mode


Openblas for speedier linear algebra in R.

brewPkg openblas --with-openmp
Enter fullscreen mode Exit fullscreen mode


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]);
  printf("\n");
}
END

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

./test-openblas 
Enter fullscreen mode Exit fullscreen mode
11.000000 -9.000000 5.000000 -9.000000 21.000000 -1.000000 5.000000 -1.000000 3.000000 
Enter fullscreen mode Exit fullscreen mode


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

brewPkg eigen
brewPkg armadillo --with-hdf5
brewPkg v8-315
brew link v8-315 --force
Enter fullscreen mode Exit fullscreen mode


Test Armadillo

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


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
...
Enter fullscreen mode Exit fullscreen mode


Test v8

echo 'quit()' | v8
Enter fullscreen mode Exit fullscreen mode
V8 version 3.15.11.18 [sample shell]
Enter fullscreen mode Exit fullscreen mode


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 \
    JAVA_CPPFLAGS=-I/System/Library/Frameworks/JavaVM.framework/Headers

# 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++
FC=gfortran-6
F77=gfortran-6
MAKE=make -j8
PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/opt/X11/lib/pkgconfig:/usr/local/opt/icu4c/lib/pkgconfig
PKG_LIBS += -L/usr/local/opt/icu4c/lib -L/usr/local/lib
END


# 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
END

# add same stuff to .bash_profile
cat $HOME/.Renviron >> $HOME/.bash_profile
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Other Programming Languages

Some other programming languages I use occasionally.

Node.js

brewPkg node
brewPkg phantomjs casperjs
Enter fullscreen mode Exit fullscreen mode

Scala

brew install scala
Enter fullscreen mode Exit fullscreen mode

golang

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
END
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

GUI Apps

Apps related to Securing your Mac.

brew cask install blockblock knockknock \
    dhs taskexplorer kextviewr
brew cask install suspicious-package
Enter fullscreen mode Exit fullscreen mode


Quicklook Plugins for Developers.

brew cask install qlcolorcode qlstephen qlmarkdown \
    quicklook-json qlprettypatch quicklook-csv betterzipql \
    qlimagesize webpquicklook
Enter fullscreen mode Exit fullscreen mode


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 
Enter fullscreen mode Exit fullscreen mode

Top comments (5)

Collapse
 
shayanlinux profile image
Shayan Shahand • Edited

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.

Collapse
 
bhaskar_vk profile image
Bhaskar Karambelkar

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.

Collapse
 
chipoglesby profile image
Chip Oglesby

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

Collapse
 
habukagumba profile image
Herbert Kagumba

Is it normal for the gcc install to take forever?

Collapse
 
bhaskar_vk profile image
Bhaskar Karambelkar

Wait till you get to the mactex part. ;)