Unable to find gcc installing rstan on Windows 10

I have been struggling for a day to get Rstan installed and working on my Windows 10 machine and I cannot figure out what isn’t working, even though my problem seems to be similar to some I’ve found on this forum by googling. I think it has something to do with my path and where R is looking for g++ or something. I was tinkering with my path variables and wonder if I messed something up. The errors I’m reporting now are after a clean reinstall of R 4.0.2 and Rtools 40.

Right now, I was able to install Rstan and load the library. When I call stan_demo() to test it I get

Error: package or namespace load failed for ‘rstan’ in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):
 there is no package called ‘matrixStats’

If I try install.packages("matrixStats"), I get the following error:

C:/Rtools/mingw32/bin/gcc  -I"C:/PROGRA~1/R/R-40~1.2/include" -DNDEBUG          -O2 -Wall  -std=gnu99 -mfpmath=sse -msse2 -mstackrealign -c 000.init.c -o 000.init.o
sh: C:/Rtools/mingw32/bin/gcc: No such file or directory
make: *** [C:/PROGRA~1/R/R-40~1.2/etc/i386/Makeconf:222: 000.init.o] Error 127
ERROR: compilation failed for package 'matrixStats'

Here is the output from a few other things that may be relevant based on problems that seem similar to mine:

[1] "C:/Rtools/mingw$(WIN)/bin/"

[1] "C:\\rtools40\\usr\\bin;C:\\Program Files\\R\\R-4.0.2\\bin\\x64;C:\\RBuildTools\\3.5\\bin;C:\\Program Files\\Amazon Corretto\\jdk1.8.0_212\\bin;C:\\Program Files (x86)\\Measurement Computing\\DAQ\\;C:\\Program Files\\ImageMagick-7.0.8-Q16;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\Calibre2\\;C:\\Program Files\\nodejs\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Program Files\\PuTTY\\;C:\\HashiCorp\\Vagrant\\bin;C:\\ProgramData\\chocolatey\\bin;C:\\Program Files\\Git\\cmd;C:\\Program Files\\Docker\\Docker\\resources\\bin;C:\\ProgramData\\DockerDesktop\\version-bin;C:\\rtools40\\usr\\bin;C:\\rtools40\\mingw64\\bin;C:\\Users\\wittm094\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Program Files\\qpdf-8.3.0\\bin;C:\\Users\\wittm094\\AppData\\Roaming\\npm;C:\\Users\\wittm094\\AppData\\Roaming\\TinyTeX\\bin\\win32;C:\\Program Files (x86)\\FAHClient"

writeLines(readLines(file.path(Sys.getenv("HOME"), ".R/Makevars")))
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file 'C:\Users\wittm094\Documents/.R/Makevars': No such file or directory
  • Operating System - Windows 10
  • RStan Version - 2.21.2

Try first doing


although I don’t know why you would need to install the matrixStats package from source in the first place.

Ah my bad… I assumed when I got the message “Do you want to install from sources the package which needs compilation?” message it meant I had to compile. I’ve never tried hitting no, or noticed the message about binaries being available.

I got matrixStats installed but and then ran into the “RMU” error dealt with in this thread: New error: cleanup_makevar(old) : argument "RMU" is missing, with no default. I change the name of the makevars file as per the solution in there, and tried stan_demo() and got the same message

C:/Rtools/mingw32/bin/gcc: No such file or directory

Using Sys.unsetenv("BINPREF") and then running stan_demo() worked! I restarted R and tried again but had to enter Sys.unsetenv("BINPREF") again - is there a step I can take so I don’t have to enter that command everytime I restart R? I’m new to working with path and system environmental variables, so I’m not sure what I’d need to do.

Hi Jake,

In your Documents folder there is a probably a file called .Rprofile, which contains a command to set the BINPREF variable every time that R starts. Try deleting/renaming this file and then restarting R and trying again

Thanks so much for your help. I really appreciate the work the Stan team does!