Requirements
- `Makevar` file in `~/.R/Makevars`
- minimum g++ compiler version
- 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.
- `V8` library
# create Makevar file
Create a `Makevars` file inside the directory `~/.R`. That is, `~/.R/Makevars`. This file should contain the text below specifying which compilers to use.
If you see the following error, it means you need a Makevar file: `Error: C++14 standard requested but CXX14 is not defined`. Makevar file from [C++14 standard requested but CXX14 is not defined · Issue #892 · stan-dev/rstan · GitHub](https://github.com/stan-dev/rstan/issues/892)
```sh
MAKEFLAGS = -j8
## C++ flags
CXX=g++
CXX11=g++
CXX14=g++
CXX17=g++
CXXFLAGS=-O3 -march=native -Wno-ignored-attributes
CXX11FLAGS=-O3 -march=native -Wno-ignored-attributes
CXX14FLAGS=-O3 -march=native -Wno-ignored-attributes
CXX17FLAGS=-O3 -march=native -Wno-ignored-attributes
CXXPICFLAGS=-fPIC
CXX11PICFLAGS=-fPIC
CXX14PICFLAGS=-fPIC
CXX17PICFLAGS=-fPIC
CXX11STD=-std=c++11
CXX14STD=-std=c++14
CXX17STD=-std=c++17
## C flags
CC=gcc
CFLAGS=-O3 -march=native
## Fortran flags
FC=gfortran
F77=gfortran
FFLAGS=-O3 -march=native
FCFLAGS=-O3 -march=native
```
# load modules in server
```sh
# in bash
# check compiler version
g++ --version
gcc --version
module load gcc/11.2.0 # stan needs g++ 4.9.3 or higher
module load R/4.1.0 # load R module in server
R # launch R
```
# install V8
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.
```r
# check g++ version in R
system("g++ --version")
# install packages in R
Sys.setenv(DOWNLOAD_STATIC_LIBV8 = 1)
install.packages("V8")
```
# install rstan
```r
install.packages("rstan", type = "source")
# alternatively
# install.packages("rstan", repos = "cran.r-project.org", dependencies = TRUE)
```
# in case you need to remove rstan
```r
remove.packages("rstan")
remove.packages("StanHeaders")
if (file.exists(".RData")) file.remove(".RData")
```
# References
- [Configuring C Toolchain for Linux · stan-dev/rstan Wiki · GitHub](https://github.com/stan-dev/rstan/wiki/Configuring-C-Toolchain-for-Linux)
- [GitHub - jeroen/V8: Embedded JavaScript Engine for R](https://github.com/jeroen/V8#getting-started)
- [Problem installing BRMS package on Linux server - Interfaces / brms - The Stan Forums](https://discourse.mc-stan.org/t/problem-installing-brms-package-on-linux-server/20817)
- [Failed when install packages rstan · Issue #304 · stan-dev/rstan · GitHub](https://github.com/stan-dev/rstan/issues/304)
- [rcpp - Understanding the contents of the Makevars file in R (macros, variables, ~/.R/Makevars and pkg/src/Makevars) - Stack Overflow](https://stackoverflow.com/questions/43597632/understanding-the-contents-of-the-makevars-file-in-r-macros-variables-r-ma)