Setup OSX for R
Tags: r, rstats, osx, homebrew
Published: 2017-01-20 01:44:23

---
title: "Setup OSX for R"
published: true
description: "My notes on setting up OSX for R use/development"
cover_image: "https://images.unsplash.com/photo-1454165205744-3b78555e5572?dpr=2&auto=format&fit=crop&w=767&h=511&q=80&cs=tinysrgb&crop="
tags: ["r", "rstats", "osx", "homebrew"]
date: "2017-01-19T20:44:23-05:00"
---

_Originally Published on my [Personal Blog](https://www.karambelkar.info/2017/01/setup-osx-for-r/)._

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.

<!--more-->

### Github API Token

Get a github account and setup an API token as described [here](https://help.github.com/articles/creating-an-access-token-for-command-line-use/). This helps you avoid hitting Github API limits when using homebrew.

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

### Install Homebrew and tap some taps

```bash
/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
```

### Bash

Install Bash and verify

```bash
brew install bash bash-completion2
/usr/local/bin/bash --version
```

	GNU bash, version 4.3.46(1)-release (x86_64-apple-darwin15.5.0)

<br/>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.

```bash

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
```

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

```bash
/usr/local/bin/bash -l
```

### GCC Compiler and Autotools

Install gcc, without multilib so that [OpenMP works](http://stackoverflow.com/questions/29057437/compile-openmp-programs-with-gcc-compiler-on-os-x-yosemite).

```bash
brewPkg gcc --without-multilib
/usr/local/bin/gcc-6 --version
```

	gcc-6 (Homebrew gcc 6.1.0 --without-multilib) 6.1.0

```bash 
/usr/local/bin/gfortran --version
```

	GNU Fortran (Homebrew gcc 6.1.0 --without-multilib) 6.1.0

<br/>Setup aliases for homebrew's gcc

```bash
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 -
```

<br/>Install ccache to speed up compilation

```bash
brewPkg ccache
/usr/local/bin/ccache --version
```

	ccache version 3.2.7

<br/>Install autotools, pkg-config, and cmake

```bash
brewPkg cmake pkg-config autoconf automake
```

<br/>Let's make sure OpenMP is working as expected.

```bash
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
```

<br/>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

```bash
brewPkg freetype fontconfig  pixman gettext
```

### SSL/SSH Libs

Setup and verify openssl and libressl.

```bash
brewPkg  openssl
/usr/local/opt/openssl/bin/openssl version
```

	OpenSSL 1.0.2h  3 May 2016

```bash
brewPkg  libressl
/usr/local/opt/libressl/bin/openssl version
```

	LibreSSL 2.3.6

```bash
brewPkg libssh2
```	

#### wget and curl

```bash
brewPkg wget
/usr/local/bin/wget --version
```

	GNU Wget 1.18 built on darwin15.6.0.

```bash
brewPkg curl
/usr/local/opt/curl/bin/curl-config --version
```

	libcurl 7.50.0

### GPG

```bash
brewPkg gpg2 --with-readline
/usr/local/bin/gpg2 --version
```

	gpg (GnuPG) 2.0.30
	libgcrypt 1.7.2

```bash
brewPkg gpgme
/usr/local/bin/gpgme-config --version
```

	1.6.0

### Java

```bash
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)

### X-server

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

```bash
brew cask install xquartz
```

### Python

Install python2

```bash
brewPkg python
pip install --upgrade pip setuptools
/usr/local/bin/python -V
```

	Python 2.7.12

<br/>Install Python3

```bash
brewPkg python3
pip3 install --upgrade pip setuptools wheel
/usr/local/bin/python3 -V
```

	Python 3.5.2


### Git

```bash
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

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

	57.1

<br/>Install libxml2, libiconv, and libxslt.

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

	2.9.4

<br/>Install boost. Ignore the warning at the end.

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

<br/>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](https://tabreziqbal.wordpress.com/2006/03/16/how-to-test-c-boost-installation/). Ignore the warning messages spewed by the compiler.

```bash
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
```

	1.61803

```bash
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
```

	Found second.cpp file in "/Users/brewmaster"

### Latex Support

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

```bash
brew cask install mactex
```

### R 

<br/>Install libsvg, librsvg, and cairo.

```bash
brewPkg cairo 
brewPkg libsvg
brewPkg librsvg
brewPkg pandoc
```

<br/>Openblas for speedier linear algebra in R.

```bash
brewPkg openblas --with-openmp
```

<br/>Test openblas. Shamelessly copied from [Intertubes](https://github.com/xianyi/OpenBLAS/wiki/User-Manual#code-examples).

```bash
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 
```

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

<br/>Eigen and Armadillo for Rcpp and v8 for [R+v8](https://github.com/jeroenooms/V8).

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

<br/>Test Armadillo

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

<br/>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
	...


<br/>Test v8

```bash
echo 'quit()' | v8
```

	V8 version 3.15.11.18 [sample shell]

<br/>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.

```bash
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
```

### GIS Stuff

Mostly PostGIS + Geo libs.

```bash
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.

#### Node.js

```bash
brewPkg node
brewPkg phantomjs casperjs
```

#### Scala

```bash
brew install scala
```

#### golang

```bash
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
```


### Other Interesting Stuff

```bash
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.

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

<br/>Quicklook Plugins for Developers.

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

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

```bash
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 
```