Issues installing and running rstan in R on Windows

Hi there,

I’ve been having problems using R on a Windows computer that I haven’t been able to figure out just from browsing.

I’m currently using Windows 10, and R vers. 4.2.2

When I install rstan directly and run the test example with:

install.packages("rstan", repos = c("", getOption("repos")))

example(stan_model, package = "rstan", run.dontrun = TRUE)

I get a huge output which ultimately ends in the following error:

Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) :
F:/Software/R/R-4.2.2/library/BH/include/boost/math/special_functions/gamma.hpp:2105:45: required from 'typename boost::math::tools::promote_args<RT1, RT2>::type boost::math::gamma_q(RT1, RT2) [with RT1 = double; RT2 = double; typename boost::math::tools::promote_args<RT1, RT2>::type = double]'F:/Software/R/R-4.2.2/library/StanHeaders/include/stan/math/prim/fun/gamma_q.hpp:55:77: required from hereF:/Software/R/R-4.2.2/library/BH/include/boost/math/tools/fraction.hpp:84:48: error: ‘long double’ is not a class, struct, or union type using value_type = typename T::value_type; ^make: *** [F:/Software/R/R-4.2.2/etc/x64/Makeconf:260: file71b01e0828ef.o] Error 1

On the github it says to install from source if we run into this issue, so I tried this after restarting:

# Compile packages using all cores
Sys.setenv(MAKEFLAGS = paste0("-j",parallel::detectCores()))


But I end up seeing the packages still fail to install, ending its output with:

grammar_inst.cpp:3001:1: internal compiler error: Segmentation fault
} // namespace stan
Please submit a full bug report,
with preprocessed source if appropriate.
See for instructions.
make: *** [F:/Software/R/R-4.2.2/etc/x64/Makeconf:260: grammar_inst.o] Error 1
ERROR: compilation failed for package ‘rstan’
-removing ‘F:/Software/R/R-4.2.2/library/rstan’
Warning in install.packages :
installation of package ‘rstan’ had non-zero exit status

I’m really not sure what’s causing this, all the remedies I have found for others haven’t seemed to work so far. I’ve got it running completely fine on an Apple laptop as well.

Any direction or help would be much appreciated, thanks!

Can you try running:


Then restart R, making sure that no packages or workspaces are restored, and then run:

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

Thanks for the reply,

After running the new install line I still get trouble, here’s what it outputs at the end:

In file included from F:/Software/R/R-4.2.2/library/StanHeaders/include/stan/math/prim/fun/acos.hpp:7:0,
from F:/Software/R/R-4.2.2/library/StanHeaders/include/stan/math/prim/fun.hpp:6,
from F:/Software/R/R-4.2.2/library/StanHeaders/include/stan/math/prim.hpp:14,
from sparse_extractors.cpp:2:
F:/Software/R/R-4.2.2/library/StanHeaders/include/stan/math/prim/fun/constants.hpp: At global scope:
F:/Software/R/R-4.2.2/library/StanHeaders/include/stan/math/prim/fun/constants.hpp:155:14: warning: ‘stan::math::TWO_OVER_SQRT_PI’ defined but not used [-Wunused-variable]
const double TWO_OVER_SQRT_PI = 2.0 / SQRT_PI;
make: *** [F:/Software/R/R-4.2.2/etc/x64/Makeconf:260: sparse_extractors.o] Error 1
ERROR: compilation failed for package ‘rstan’

  • removing ‘F:/Software/R/R-4.2.2/library/rstan’
    Warning in install.packages :
    installation of package ‘rstan’ had non-zero exit status

What output do you get from:

fx <- inline::cxxfunction(signature(x = "integer", y = "numeric"),
                  "return ScalarReal(INTEGER(x)[0] * REAL(y)[0]);",
                  verbose = TRUE)

running that whole chunk gives me:

Program source :

1 :
2 : // includes from the plugin
3 :
4 : #include <R.h>
5 : #include <Rdefines.h>
6 : #include <R_ext/Error.h>
7 :
8 :
9 : // user includes
10 :
11 :
12 : // declarations
13 : extern “C” {
14 : SEXP file317424527af( SEXP x, SEXP y) ;
15 : }
16 :
17 : // definition
18 : SEXP file317424527af(SEXP x, SEXP y) {
19 : return ScalarReal(INTEGER(x)[0] * REAL(y)[0]);
20 : Rf_warning(“your C++ program does not return anything”);
21 : return R_NilValue;
22 : }
make cmd is
make -f “F:/Software/R/R-4.2.2/etc/x64/Makeconf” -f “F:/Software/R/R-4.2.2/share/make/” SHLIB_LDFLAGS=‘(SHLIB_CXXLDFLAGS)' SHLIB_LD='(SHLIB_CXXLD)’ SHLIB=“file317424527af.dll” WIN=64 TCLBIN= OBJECTS=“file317424527af.o”

make would use
C:/RBuildTools/3.5/mingw_64/bin/g++ -std=gnu++11 -I"F:/Software/R/R-4.2.2/include" -DNDEBUG -I"c:/rtools42/x86_64-w64-mingw32.static.posix/include" -O2 -Wall -mfpmath=sse -msse2 -mstackrealign -c file317424527af.cpp -o file317424527af.o
if test “zfile317424527af.o” != “z”; then
if test -e “file317424527af-win.def”; then
echo C:/RBuildTools/3.5/mingw_64/bin/g++ -std=gnu++11 -shared -s -static-libgcc -o file317424527af.dll file317424527af-win.def file317424527af.o -L"c:/rtools42/x86_64-w64-mingw32.static.posix/lib/x64" -L"c:/rtools42/x86_64-w64-mingw32.static.posix/lib" -L"F:/Software/R/R-4.2.2/bin/x64" -lR ;
C:/RBuildTools/3.5/mingw_64/bin/g++ -std=gnu++11 -shared -s -static-libgcc -o file317424527af.dll file317424527af-win.def file317424527af.o -L"c:/rtools42/x86_64-w64-mingw32.static.posix/lib/x64" -L"c:/rtools42/x86_64-w64-mingw32.static.posix/lib" -L"F:/Software/R/R-4.2.2/bin/x64" -lR ;
echo EXPORTS > tmp.def;
C:/RBuildTools/3.5/mingw_64/bin/nm file317424527af.o | sed -n ‘s/^.* [BCDRT] / /p’ | sed -e ‘/[.]refptr[.]/d’ -e ‘/[.]weak[.]/d’ | sed ‘s/[^ ][^ ]*/“&”/g’ >> tmp.def;
echo C:/RBuildTools/3.5/mingw_64/bin/g++ -std=gnu++11 -shared -s -static-libgcc -o file317424527af.dll tmp.def file317424527af.o -L"c:/rtools42/x86_64-w64-mingw32.static.posix/lib/x64" -L"c:/rtools42/x86_64-w64-mingw32.static.posix/lib" -L"F:/Software/R/R-4.2.2/bin/x64" -lR ;
C:/RBuildTools/3.5/mingw_64/bin/g++ -std=gnu++11 -shared -s -static-libgcc -o file317424527af.dll tmp.def file317424527af.o -L"c:/rtools42/x86_64-w64-mingw32.static.posix/lib/x64" -L"c:/rtools42/x86_64-w64-mingw32.static.posix/lib" -L"F:/Software/R/R-4.2.2/bin/x64" -lR ;
rm -f tmp.def;

There’s the issue:

make would use

Your R installation is using RTools35 with R4.2. Do you already have RTools42 installed?

1 Like

Oh I see.

I don’t think I have RTools42 installed. Is it just another package?

No, it’s the toolchain used for building R packages from source on windows:

It looks like you might have some old rstan configuration left over as well. What output do you get from:


Ohh, I’ll download that and retry, does it need to go into a specific folder?

And here’s my output:


Gives me:

Error in file(con, "r") : cannot open the connection

Gives me:

[1] "PATH=\"C:/RBuildTools/3.5/bin;${PATH}\""



Gives me:

[1] "Sys.setenv(BINPREF = \"C:/RBuildTools/3.5/mingw_$(WIN)/bin/\")"

Yep, those are also the issue.

Try running:


Then restart R and check that the readLines calls then return nothing:


If they do, then try the rstan install again

Wow, that worked!
Seems like everything runs smoothly now.

Thanks a lot!