Installing RStan in Ubuntu

Based on RStan Getting Started

R packages

Assuming you have the most up to date version of R, the following packages need to be installed. This assumes you have the c2d4u PPA available. See here for more information.

 sudo apt-get install r-cran-rcpp r-cran-inline r-cran-rcpp 

C++ Compiler

You probably have a C++ compiler installed, but just in case:

 sudo apt-get install build-essential 

The “RStan Getting Started” page suggests modifying the C++ compiler optimization level. On my install of Ubuntu, the optimization was set as the page suggested. You can check with:

 grep "CXXFLAGS =" /usr/lib/R/etc/Makeconf 

Among the output of grep, you should see:

 CXXFLAGS = -O3 -pipe -g $(LTO) 

If you do not, use your favorite editor to edit “/ust/lib/R/etc/Makeconf” so that the CXXFLAGS line has “-03” as well as everything else that is there.

Install RStan

From within R, run the following commands. You will need to select a mirror, although it shouldn’t actually use it.

 options(repos = c(getOption("repos"), rstan = "")) install.packages('rstan', type = 'source') 

It is a large download (for R, 2.3 Mb) and will take some time to compile.

Testing RStan

Now that RStan is installed, you can test it with the following code. RStan compiles the model, then runs it. It will take much longer to compile the code than it takes to generate the samples.

Leave a Reply