Skip to content

Configuring C Toolchain for Linux

bgoodri edited this page Apr 10, 2024 · 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.

Ubuntu LTS (16.04, 18.04, 20.04)

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

Ubuntu LTS users on R>=4.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

Ubuntu Non-LTS

Non-LTS users will need to install the R packages from source. First, the required system packages need to be installed:

sudo apt-get install r-base r-base-dev libcurl4-openssl-dev 

Then in R, install the package:

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

Debian Testing

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("\nCXX17FLAGS=-O3 -march=native -mtune=native -fPIC",
    "CXX17=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.