Skip to content

Configuring C Toolchain for Linux

Andrew Johnson edited this page Sep 14, 2021 · 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 c2d4u4.0 PPA (and rrutter4.0 for CRAN builds too)
sudo add-apt-repository ppa:marutter/rrutter4.0
sudo add-apt-repository ppa:c2d4u.team/c2d4u4.0+
sudo apt update
sudo apt install r-cran-rstan

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.