Build R with OpenBLAS and OpenMP support on MacOS 10.14 (Mojave)
These instructions are to compile R from source on MacOS 10.14 (Mojave) so that it both uses OpenBLAS libraries (for faster computations) and has OpenMP support (to take advantage of multiple cores).
While the Clang compilers shipped with R finally support OpenMP for
parallel processing, they require their own unique
flags. The GNU
family of compilers (
g++, etc) don’t play nice with all R
packages when building them from source. I figured both out the hard way.
To get R to work with both OpenMP (after compiling with OpenBLAS) and packages built from source, I use a newer, non-Apple version of the LLVM compiler.
Seriously, don’t try this if you don’t like the command line or have a low tolerance for potentially borking your R installation. This worked for me and was a (mostly) fun project while setting up a new machine, but YMMV.
- Install Command Line Tools through XCode
- You may need to type
xcode-select --installin the terminal after installation, but I got this message which makes me believe this is no longer necessary:
xcode-select: error: command line tools are already installed, use "Software Update" to install updates
- Install missing header
cp /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg ~/Desktop
Once saved, double click on the install package (which is now on your Desktop). This will place header files (e.g.,
math.h) in the
/usr/includedirectory, where they have historically been and R still looks for them.
NB: As 10.14 becomes more mature, this intial XCode work-around may not be necessary.
- Install Homebrew
- Brew install some packages:
brew install openblas --with-openmp
brew install r --with-openblas
brew install --with-toolchain llvm
brew install ccache
brew install gcc
brew install libgit2
brew install openssl
brew install gettext
Note that you don’t technically need the last three libraries, but
many R packages look for them if built from source. You’ll see that
they are place in the compilation path in
Set up ~/.R/Makevars
Makevars file in
~/.R directory that looks like this
Open R and install packages like normal using
found that installing Rcpp,
tidyverse will give a
good all-around indication of whether everything worked.