Editor’s Note: This is an updated post that covers how to install the macOS toolchain for versions of R starting at 4.y.z.
For R versions between R:
- 3.0.0 - 3.3.3, please see R Compiler Tools for Rcpp on OS X before R 3.4.0.
- 3.4.0 - 3.4.4, please see R Compiler Tools for Rcpp on OS X before R 3.5.0.
- 3.5.0 - 3.5.3, please see R Compiler Tools for Rcpp on OS X before R 3.6.0.
- 3.6.0 - 3.6.3, please see R Compiler Tools for Rcpp on OS X before R 4.0.0.
Intro
The objective behind this post is to provide users with information on how to setup the macOS toolchain for compiling used in the 4.y.z series of R. The post is structured primarily for macOS Catalina (10.15.z) users.
Removing Old Installation Files
If you previously used either the clang4
, clang6
, clang7
, clang8
, or the macos-rtools
installer, please consider deleting the old components that were installed.
Instructions for uninstallation can be found here:
Uninstalling the R development toolchain on macOS
That said, please remove both ~/.R/Makevars
and ~/.Renviron
files prior to continuing as they were set in a prior iteration. You can achieve this by using:
unlink("~/.R/Makevars")
unlink("~/.Renviron")
Alternatively, if there is contents inside of the ~/.Renviron
file that must be retained, please look for where the PATH
variable is listed and remove any part with clang
.
Open the file in R using:
file.edit("~/.Renviron")
Next, find and remove:
="/usr/local/clang7/bin:${PATH}" PATH
Then, save and close the file. You may need to restart R for the changes to take effect.
Installation Instructions
One of the primary ways to setup the R toolchain for compiled code on macOS is to individually installing each element yourself.
There are two components to the R 4.y.z toolchain based on Section: C.3 macOS of R Installation and Administration. These components are:
- Xcode Command Line Tools (“Xcode CLI”)
- gfortran
- gfortran 6.3: Sierra (10.12) and High Sierra (10.13)
- gfortran 8.2: Mojave (10.14) and Catalina (10.15)
The gfortran
component is dependent on the version of macOS being used.
For historical information about the toolchain, please see: R macOS toolchain evolution
Install Guide
This guide provides a step-by-step breakdown of the actions required to setup the toolchain. As a result, this guide will use both installers and script commands ia Terminal.app
found in /Applications/Utilities/
. Terminal is macOS’ equivalent to Linux’s shell and Window’s command line. From Terminal, we will install only the XCode Command Line Tools (“Xcode CLI”). These provide the system headers used to build the official CRAN binary for R.
XCode Command Line Tools
- Open the
Terminal
from/Applications/Utilities/
- Type the following into
Terminal
- Note: Anytime the Xcode CLI toolchain updates, you must re-run this command.
xcode-select --install
- Press “Install”
- Verify installation by typing into terminal:
gcc --version
Install OS-specific gfortran binary
Determine the version of macOS the computer is being run on.
- Click the Apple menu in the corner of your screen
- Choose “About This Mac”
- Look for Version
10.1*
or the name of the operating system.
For additional help in determining this, please see Apple’s support page on Find out which macOS your Mac is using.
Then, download and install the appropriate gfortran
binary from the https://github.com/fxcoudert/gfortran-for-macOS
- Mojave (10.14) - Catalina (10.15) uses
gfortran8.2
- Sierra (10.12) - High Sierra (10.13) uses
gfortran6.3
Both of the installers will place the gfortran
binary into /usr/local/gfortran
. This will be picked up by the default implicit variable set by R during compilation.
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() {
::Rcout << "Hello World!" << std::endl;
Rcpp}
// After compile, this function will be immediately called using
// the below snippet and results will be sent to the R console.
/*** R
hello_world()
*/
Compile the function using:
::sourceCpp('path/to/file/helloworld.cpp') Rcpp
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:
hello_world()
Common Errors
The following are debugged errors that you may run into.
clang: warning: argument unused during compilation: '-fopenmp'
fatal error: 'omp.h' file not found
Unfortunately, with R 4.0.0 the CRAN distributed version of R loses the ability to use OpenMP without a custom setup.
Acknowledgements
Thanks to dr.ing. MPH Verouden for catching a couple of errors in the post and letting me know!