Not able to install RStan


I wanted to demonstrate rstan (and rstanarm too) using RStudio Cloud for my class, as students would run into different issues installing it on their computer and I want them to have some hands on experience with it first. I followed the instruction in, and I think I’ve checked g++, build-essential, and libssl-dev are all present in the system. However, I failed to install rstan, with the message

* installing *source* package ‘rstan’ ...
** package ‘rstan’ successfully unpacked and MD5 sums checked
** libs
g++  -I/opt/R/3.4.2/lib/R/include -DNDEBUG -I"../inst/include" -I"../inst/include/boost_not_in_BH" -I"`"/opt/R/3.4.2/lib/R/bin/Rscript" --vanilla -e "cat(system.file('include', 'src', package = 'StanHeaders'))"`" -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -DFUSION_MAX_VECTOR_SIZE=12 -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -I"/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include" -I"/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/RcppEigen/include" -I"/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/BH/include" -I"/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include" -I/usr/local/include   -fpic  -Wno-unused-variable -Wno-unused-function  -Wno-macro-redefined -flto -Wno-unused-local-typedefs -c chains.cpp -o chains.o
g++  -I/opt/R/3.4.2/lib/R/include -DNDEBUG -I"../inst/include" -I"../inst/include/boost_not_in_BH" -I"`"/opt/R/3.4.2/lib/R/bin/Rscript" --vanilla -e "cat(system.file('include', 'src', package = 'StanHeaders'))"`" -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -DFUSION_MAX_VECTOR_SIZE=12 -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -I"/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include" -I"/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/RcppEigen/include" -I"/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/BH/include" -I"/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include" -I/usr/local/include   -fpic  -Wno-unused-variable -Wno-unused-function  -Wno-macro-redefined -flto -Wno-unused-local-typedefs -c init.cpp -o init.o
g++  -I/opt/R/3.4.2/lib/R/include -DNDEBUG -I"../inst/include" -I"../inst/include/boost_not_in_BH" -I"`"/opt/R/3.4.2/lib/R/bin/Rscript" --vanilla -e "cat(system.file('include', 'src', package = 'StanHeaders'))"`" -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -DFUSION_MAX_VECTOR_SIZE=12 -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -I"/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include" -I"/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/RcppEigen/include" -I"/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/BH/include" -I"/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include" -I/usr/local/include   -fpic  -Wno-unused-variable -Wno-unused-function  -Wno-macro-redefined -flto -Wno-unused-local-typedefs -c lang__ast_def.cpp -o lang__ast_def.o
g++  -I/opt/R/3.4.2/lib/R/include -DNDEBUG -I"../inst/include" -I"../inst/include/boost_not_in_BH" -I"`"/opt/R/3.4.2/lib/R/bin/Rscript" --vanilla -e "cat(system.file('include', 'src', package = 'StanHeaders'))"`" -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -DFUSION_MAX_VECTOR_SIZE=12 -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -I"/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include" -I"/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/RcppEigen/include" -I"/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/BH/include" -I"/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include" -I/usr/local/include   -fpic  -Wno-unused-variable -Wno-unused-function  -Wno-macro-redefined -flto -Wno-unused-local-typedefs -c lang__grammars__bare_type_grammar_inst.cpp -o lang__grammars__bare_type_grammar_inst.o
In file included from /home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/BH/include/boost/spirit/include/phoenix_limits.hpp:11:0,
                 from /home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/BH/include/boost/spirit/home/support/meta_compiler.hpp:16,
                 from /home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/BH/include/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from /home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/BH/include/boost/spirit/home/qi/action/action.hpp:14,
                 from /home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/BH/include/boost/spirit/home/qi/action.hpp:14,
                 from /home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/BH/include/boost/spirit/home/qi.hpp:14,
                 from /home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/BH/include/boost/spirit/include/qi.hpp:16,
                 from /home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/src/stan/lang/grammars/bare_type_grammar_def.hpp:4,
                 from /home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/StanHeaders/include/src/stan/lang/grammars/bare_type_grammar_inst.cpp:1,
                 from lang__grammars__bare_type_grammar_inst.cpp:18:
/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/BH/include/boost/phoenix/core/limits.hpp:44:0: warning: "BOOST_PHOENIX_NO_VARIADIC_EXPRESSION" redefined
<command-line>:0:0: note: this is the location of the previous definition
g++: internal compiler error: Killed (program cc1plus)
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-5/README.Bugs> for instructions.
/opt/R/3.4.2/lib/R/etc/Makeconf:168: recipe for target 'lang__grammars__bare_type_grammar_inst.o' failed
make: *** [lang__grammars__bare_type_grammar_inst.o] Error 4
ERROR: compilation failed for package ‘rstan’
* removing ‘/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.4/rstan’
Warning in install.packages :
  installation of package ‘rstan’ had non-zero exit status

Any help would be greatly appreciated.


This indicates that the available memory was exceeded, and the compilation terminated:

Short term, early next week we should have a pre-built version of rstan available.
Longer term, we are looking to make more memory available.

I’ll post an update when the former is in place.


That would be great and thank you!


A pre-built version is now available, could you give it another try please?


Thank you josh, and I can install rstan now. However, when I call rstan it needs to compile the stan code, and I think I run into the same issue of not have enough memory during that compilation phase.

In the short term, would it be possible to have a pre-built version of rstanarm that have the common models I use in my class pre-compiled? That way I think I can do class demonstration without the need to compile anything. Thank you again.


I can install rstan, but I run into problems when trying to compile and run a model; this works fine on my standalone R install. Any help would be much appreciated!


Hi Josh, I was just wondering if there was any update on installing rstanarm --- rstan installs fine, but was hoping to use rstanarm for a class demonstration.



I'm sorry, I totally dropped the ball here.
Let me see about caching a pre-built rstanarm right now.


Could you give it a try and let me know how it goes?
I did verify that it installs and loads, but haven't exercised it at all.


I was able to install rstanarm now. I tried to run an example analysis and it looked fine. Thank you very much!


Worked like a charm Josh, thanks much!


Is anybody running into problems using brms? Now might be a good time to check since that's a natural stepping stone after rstanarm.


I was just able to install brms after installing rstanarm, but I could not get the example to work, I get the error:

Compiling the C++ model Error in compileCode(f, code, language = language, verbose = verbose) : Compilation ERROR, function(s)/method(s) not created! In file included from /home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.5/BH/include/boost/config.hpp:39:0, from /home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.5/BH/include/boost/math/tools/config.hpp:13, from /home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.5/StanHeaders/include/stan/math/rev/core/var.hpp:7, from /home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.5/StanHeaders/include/stan/math/rev/core/gevv_vvv_vari.hpp:5, from /home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.5/StanHeaders/include/stan/math/rev/core.hpp:12, from /home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.5/StanHeaders/include/stan/math/rev/mat.hpp:4, from /home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.5/StanHeaders/include/stan/math.hpp:4, from /home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.5/StanHeaders/include/ In addition: Warning message: In system(cmd, intern = !verbose) : running command '/opt/R/3.5.0/lib/R/bin/R CMD SHLIB file1b8e6b33bb72.cpp 2> file1b8e6b33bb72.cpp.err.txt' had status 1


@josh Can you install clang++ on the RStudio Cloud? It uses less RAM when dealing with Stan than does g++ but is not currently installed


If that works, I can post some instructions on how to utilize it.


Now available:

rstudio-user@df157f188266:/cloud/project$ which clang++
rstudio-user@df157f188266:/cloud/project$ clang++ --version
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin


Thanks @josh for installing clang++. The rstan package seems to work now, at least for simple examples. To use it, execute the following R code once

dotR <- file.path(Sys.getenv("HOME"), ".R")
if (!file.exists(dotR)) dir.create(dotR)
MAKEVARS <- file.path(dotR, "Makevars")
if (!file.exists(MAKEVARS)) file.create(MAKEVARS)

  "\nCXXFLAGS=-Os -mtune=native -march=native",
  "CXXFLAGS += -Wno-unused-variable -Wno-unused-function  -Wno-unknown-pragmas",
  file = MAKEVARS, 
  sep = "\n", 
  append = TRUE

to configure your C++ toolchain as mentioned at
Then, do


but you may also want to install brms, shinystan, etc.

To test that it is working correctly, you can run an example such as

schools_dat <- list(J = 8, 
                    y = c(28,  8, -3,  7, -1,  1, 18, 12),
                    sigma = c(15, 10, 16, 11,  9, 11, 10, 18))
mc <-
data {
  int<lower=0> J;          // number of schools 
  real y[J];               // estimated treatment effects
  real<lower=0> sigma[J];  // s.e. of effect estimates 
parameters {
  real mu; 
  real<lower=0> tau;
  vector[J] eta;
transformed parameters {
  vector[J] theta;
  theta = mu + tau * eta;
model {
  target += normal_lpdf(eta | 0, 1);
  target += normal_lpdf(y | theta, sigma);
post <- stan(model_code = mc, data = schools_dat, seed = 12345678)

@josh Do you think it is possible for RStudio Cloud to detect whether the rstan package is loaded and if so

Sys.setenv(R_MAKEVARS_USER = "/read-only/path/to/Makevars")

behind the scenes (if it is unset originally) where /read-only/path/to/Makevars just contains

CXXFLAGS=-Os -mtune=native -march=native -Wno-unused-variable -Wno-unused-function  -Wno-unknown-pragmas

which would make it unnecessary for each user to run the code at the top of this post to self-configure their toolchain?


I don't think we'd want to do this globally as there may be users that load rstan but nevertheless are using other packages where they depend on using the default compiler toolchain. We

However, I think there is a way for you to create an rstan template/project for RStudio Cloud that has these customizations (then users could just clone that).

@josh What do you think the most robust/convenient way to handle this would be?


Our template support helps with getting going with installed R packages and the project itself.

Anything that belongs in a user's home directory, e.g. ~/.R and ~/.rstudio, are not included in a project template.

That said, a template project could include the Makevars file in the project itself, and have a setup.R that contained the Sys.setenv call.

Thoughts, @Robby?


It seems like the project could also include a .Rprofile that arranged for the right Makevars


Sounds like a good approach - we definitely want to keep the behavior contained within the project so it can be shared reliably with others.