R Compiler Tools for Rcpp on macOS

Editor’s Note: This is an updated post that covers how to install the macOS toolchain for versions of R starting at 3.4.z.

For R versions between R 3.0.0 - 3.3.z, please see R Compiler Tools for Rcpp on OS X before R 3.4.0.


The objective behind this post is to provide users with information on how to setup the macOS toolchain for compiling used in the 3.4.* series of R. This has been a bit problematic for many R users since OS X Mavericks, which resulted in gfortran binaries being dropped from the R installer. More curiously, the additional demand to have access to a compiler vs. downloading a binary from CRAN became apparent slightly after Rcpp’s 0.10.0 version, when attributes where added that removed the necessity to use R’s SEXP objects.


There are two ways to go about setting up the R toolchain for compiled code on macOS:

  1. An online installer that installs all elements.
  2. Individually installing each element yourself.

Pick one and follow it to completion.

Automated Installer

The automated installer is just that, an automatic way to acquire all of the official components of the R toolchain on macOS. Download and run the installer while connected to the internet to acquire the appropriate software and configuration files.

Installer Download: https://uofi.box.com/v/r-macos-rtools-pkg

Source Code for Installer: https://github.com/coatless/r-macos-rtools

Manual Install Guide

This guide provides a step-by-step breakdown of the actions the automated installer takes. As a result, this guide will use both installers and script commands ia Terminal.app found in /Applications/Utilities/. Terminal is macOS’s equivalent to Linux’s shell and Window’s command line. From Terminal, we will install only the XCode Command Line Tools. These provide the system headers used to build the official CRAN binary for R.

XCode Command Line Tools

  1. Open the Terminal from /Applications/Utilities/
  2. Type the following into Terminal
xcode-select --install
  1. Press “Install”
  2. Verify installation by typing into terminal:
gcc --version

Installing the clang4 R binary

There are two options to install the clang4 binary that is prebuilt by the CRAN macOS maintainer at: http://r.research.att.com/libs/.

  1. Use the clang4-r binary installer
  2. Use Terminal commands

Pick only one of these options.

Option 1: clang4-r binary installer

Download and install the clang4-r binary installer from: https://uofi.box.com/v/r-macos-clang-pkg

The clang4-r installer performs two actions that require the users password to accomplish. These actions are:

  1. unpack a set of pre-made binary files into the /usr/local/clang4 directory
  2. establish the proper paths for CC, CXX, CXX**, and LDFLAGS in the ~/.R/Makevars file

In essence, it provides a graphical user interface installation guide, more secure path manipulation, and a smarter handling of a pre-existing ~/.R/Makevars

To view how the installer was created please see: https://github.com/coatless/r-macos-clang

Option 2: Terminal approach

# Download clang4 binary
curl -O http://r.research.att.com/libs/clang-4.0.0-darwin15.6-Release.tar.gz

# Extract clang4 binary into root directory
tar fvxz clang-4.0.0-darwin15.6-Release.tar.gz -C /

# Verify file exists
touch ~/.R/Makevars

# Overwrites ~/.R/Makevars file if present otherwise creates it
cat <<- EOF > ~/.R/Makevars
# The following statements are required to use the clang4 binary
# End clang4 inclusion statements

# Clean up after ourself
rm -rf clang-4.0.0-darwin15.6-Release.tar.gz

Install gfortran binary

  1. Visit https://gcc.gnu.org/wiki/GFortranBinaries#MacOS
  2. Select the appropriate gfortran binary for your system.
  3. Install gfortran via the installer package.

Note: If you are on macOS Sierra / High Sierra (10.12 - 10.13), you will need to set an FLIBS variable in the ~/.R/Makevars to avoid a compilation warning. The FLIBS variable sets the linker flags needed to link Fortran code. Execute the following code in Terminal:

touch ~/.R/Makevars
cat <<- EOF >> ~/.R/Makevars
# The following statement changes the Fortran linking path
# End Fortran linking path statement

Doing so will avoid the compilation error of:

ld: warning: directory not found for option

The reason for this warning message is because R 3.4.* is compiled on El Capitan that uses gfortran v6.1.0. Due to the way R’s site-wide Makevar file is structured, this largely is a cosmetic fix as it has a generic catch-all inclusion of /usr/local/gfortran/lib/gcc.

Quick check

To verify that everything is working appropriately, let’s do a quick C++ program using Rcpp and Armadillo.

First, let’s install Rcpp and RcppArmadillo within R.

install.packages(c('Rcpp', 'RcppArmadillo'))

Create a new file, name the follow: helloworld.cpp

By adding the .cpp extension, the file is viewed as being C++ code.

Within the file write:

#include <RcppArmadillo.h>   

// [[Rcpp::depends(RcppArmadillo)]]

// [[Rcpp::export]]
void hello_world() {
  Rcpp::Rcout << "Hello World!" << std::endl;  

// After compile, this function will be immediately called using
// the below snippet and results will be sent to the R console.

/*** R

Compile the function using:


where 'path/to/file/' is the location containing helloworld.cpp

If everything is installed appropriately, then you should see the following in the console:

> hello_world()

Hello World!

In addition, you should have a new function within the global environment scope called “hello_world”. You can call this function like a normal R function via: