Skip to content

Configuring C Toolchain for Linux

Jonah Gabry edited this page Dec 13, 2020 · 6 revisions

This page contains instructions for configuring your C++ toolchain on Linux in order to use RStan.

Using RStan requires either g++ version 4.9 and up or clang++ version 3.4 and up as they support the C++14 standard. Such a compiler is almost always available and is likely already installed system wide but may not be the default compiler on older systems such as RHEL7.

Installing from the repository

It is possible to install a packaged (pre-built) RStan binary from your distribution's repositories.

Ubuntu users on R>=3.5.0 can install a binary version of RStan with

# Add Michael Rutter's c2d4u3.5 PPA (and rrutter3.5 for CRAN builds too)
sudo add-apt-repository -y "ppa:marutter/rrutter3.5"
sudo add-apt-repository -y "ppa:marutter/c2d4u3.5"
sudo apt update
sudo apt install r-cran-rstan

Ubuntu users on R 3.4.* or older should drop the "3.5" part of the above PPA's (e.g. use c2d4u instead of c2d4u3.5). More details here.

Debian users of DebianTesting can use

apt-get install r-cran-rstan

C++ toolchain configuration

The following will create or edit a configuration file for the C++ toolchain

dotR <- file.path(Sys.getenv("HOME"), ".R")
if (!file.exists(dotR)) dir.create(dotR)
M <- file.path(dotR, "Makevars")
if (!file.exists(M)) file.create(M)
cat("\nCXX14FLAGS=-O3 -march=native -mtune=native -fPIC",
    "CXX14=g++", # or clang++ but you may need a version postfix
    file = M, sep = "\n", append = TRUE)

Note that your compiler may have a version number postfix, such as g++-7 or clang++-6.0.

As of version 2.21, RStan depends on the V8 R package. For Linux, this means that you need to have the libv8 library installed on your system. There are instructions for installing this library on a variety of distributions on the V8 Github: https://github.com/jeroen/V8#getting-started.

If, however, you are not able to install the libv8 library on your particular system, you can request that a static build be downloaded during the R package installation. This is done via:

Sys.setenv(DOWNLOAD_STATIC_LIBV8 = 1)
install.packages("V8")

At this point, you can proceed to How to Use RStan if you have installed the binary version of it or otherwise build it from source.