Rstan installation troubles

I deleted the file, and it still isn’t working.

pkgbuild::check_compiler(debug = TRUE)

Trying to compile a simple C file
Running “C:/PROGRA~1/R/R-43~1.0/bin/x64/Rcmd.exe” SHLIB foo.c
Error: Failed to compile C code

Ah worth a shot! At this point it does look like the uninstall and reinstall is probably your best bet unfortunately

I don’t mind doing a fresh install if it’s needed. I’ve never had this much trouble getting stan installed though, its weird.

And just to emphasise that this has nothing to do with stan/rstan, this is purely R and rtools

Ah yeah don’t worry, I don’t hold this against rstan! I shall try a completely fresh installation and let you know how that goes… Thanks for all your help so far.

Okay, a full reinstall of R, Rstudio and R tools has still not fixed the problem.

I’m completely stumped to be honest, is there anything else I can try?

Your best bet would be either/both of the rstudio forum or the r-help mailing list

Okay, I’ll do that. Thank you very much for your help

Sorry to revive this thread, but I have made some progress, so wondered if there is maybe a small extra step that needs doing to get everything working.

I installed an older version of R (4.2.3) and rtools42 and tried installing stan again. Installing not from source, I still get the f compile error. However, I do now seem to be able to install the packages using the code you pasted above.

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

Then, library(rstan) shows:
Loading required package: StanHeaders rstan version 2.26.22 (Stan version 2.26.1)

Which seems to me like I have the latest version of stan installed - unless I am mistaken?

Sadly, running the test model and my own models still fails with the same error:
Error in compileCode(f, code, language = language, verbose = verbose) :
Error in sink(type = "output") : invalid connection

I also still cannot install from source… IE

install.packages("Rcpp", type="source")

Gives this as before.

Warning in install.packages : installation of package ‘Rcpp’ had non-zero exit status

Unfortunately your situation is still the same. There are binary versions of StanHeaders and rstan 2.26 on R4.2 which have now been installed, but you still cannot compile C++ in R so they’re not usable

Ah right, thats unfortunate. Do you have any intuition about what the problem with the C++ compiler might be? Could it be a windows permissions problem for example? Just so I know approximately what I can ask others

No idea sorry, first time I’ve seen something like this

I have a related problem.
I cannot install cmdstan or cmdstanr on my mac (I have rstan which works fine but need the others to install and use the “rethinking” package). I have the latest versions of everything (R, RStudio, c++ compiler [which works without problem], etc). Yet I still get the following message:

Warning in install.packages :
unable to access index for repository
cannot open URL ‘
installing the source package ‘cmdstanr’

trying URL ‘
Content type ‘application/gzip’ length 667799 bytes (652 KB)

downloaded 652 KB

Error: 3:49: unexpected string constant
3: if (Sys.getenv(“RSTUDIO”) == “1” && RSTUDIO_TERM")) &&
Execution halted
Warning in install.packages :
installation of package ‘cmdstanr’ had non-zero exit status

The downloaded source packages are in

Any ideas? Please help!! Many thanks!

I too have not yet found a solution to my issues either. It seems to me that rtools is just not configuring properly.

I would be really grateful if anybody could help, I have about exhausted all my options

I am not sure about Windows 11 (my machines are still running Windows 10), but I needed to follow the steps in this blog post to get rstan running with R 4.2.x and Rtools 42, or R 4.3.0 and Rtools 43.
I noticed that StanHeaders 2.26.25 has appeared on CRAN. Does this mean that rstan 2.26.x will appear on CRAN soon too? And that it will work with Rtools43 installed out of the box?

That error message comes from RStudio? It seems there is a spurious " after RSTUDIO_TERM.
Did you try to install from R directly.
I have occasionally the issue that a package will not install/update if I try to do so from RStudio but that everything works smoothly if I start R directly and work with R. If memory serves correctly, it typically happens with packages that RStudio loads on start up, or packages that need compilation and the computer does not have the necessary toolchain installed (the latter mostly a Windows issue).

Thanks for your additional reply.

Unless I’m mistaken, it seems the blog post just talks about installing rstan from source, rather than CRAN? If so, unfortunately that is precisely where I have become stuck.

At this point, it’s clear that my issue is both A) not specific to stan (I am unable to install ANY packages from source) and B) specific to this PC (I have successfully installed stan on my laptop running Windows 11, rtools43 and r version 4.3 and everything seems fine. In addition, no amount of installing and reinstalling has helped.

To offer a few further details on the offchance somebody who sees this thread can help. The package installation gives no errors, aside from the “non zero exit status”. Adding the verbose = TRUE tag doesn’t reveal anything further. Rtools43 seems to have been added to my path successfully - though I am not certain. Rolling back to R version 4.2x didn’t solve the problem, nor did running as administrator and disabling the firewall.

If I had to speculate, I suspect that some configuration with rtools has gone wrong somewhere and, for whatever reason, it is not being changed / changed back during further reinstallation attempts. Of course this is a pure guess, I honestly have no idea what is going on.

Hi Ruben,
Yes, these instructions install rstan from source.
As far as I can tell, this is necessary since R 4.2.0 as rstan does not work with the Rtools that are shipped since then.
Over the last few days I upgraded my office laptop to R 4.3.0 and Rtools 4.3, with rstan and StanHeaders from CRAN. In this configuration rstan does not work (when used in an R notebook in RStudio). I had to modify the Rtools setting using, essentially, the instructions from the above blog post.
Specfically after:

> remotes::install_github("stan-dev/cmdstanr")
> cmdstanr::check_cmdstan_toolchain(fix = TRUE)
> install.packages("rstan", repos = c("", getOption("repos")))

I had a set up in which I could use rstan with R 4.3.0 and Rtools 4.3.
Some notes:

  • Ensure that every other version of Rtools has been uninstalled, in particular Rtools 4.2
  • Install cmdstanr from GitHub, the version on the repository is out dated (it complains about Rtools 4.2 not being installed and asks you to install it, which would be wrong).
  • The second command seems to fix the toolchain so that it works with rstan. Not sure what changes. But it emits two lines of feedback “Installing mingw32-make and g++ with Rtools43.” and “The C++ toolchain required for CmdStan is setup properly!”.
  • As the StanHeaders version on CRAN is now 2.26.25, only rstan has to be installed from the repository.

Hope this helps.

1 Like

Thanks for the extra input, I really appreciate you taking the time to provide some further advice. Unfortunately, I cannot even get cmdstanr to install. This is the error I receive:

Downloading GitHub repo stan-dev/cmdstanr@HEAD
Installing 1 packages: viridisLite
Installing package into ‘C:/Users/user/AppData/Local/R/win-library/4.3’
(as ‘lib’ is unspecified)
trying URL ''
Content type 'application/zip' length 1300028 bytes (1.2 MB)
downloaded 1.2 MB

package ‘viridisLite’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
── R CMD build ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
WARNING: Rtools is required to build R packages, but no version of Rtools compatible with R 4.3.0 was found. (Only the following incompatible version(s) of Rtools were found: 4.3.5550)

Please download and install Rtools 4.2 from or
Error: Failed to install 'cmdstanr' from GitHub:
  ! System command 'Rcmd.exe' failed

This is quite strange.
I have Rtools 4.3.5550 installed on my machine and that is appropriate for R 4.3.0. Rtools 4.2 would definitely be inappropriate for R 4.3.0. No idea why this message is issued, or by which software.
Which version of RStudio are you using? Perhaps that one is too old?
What happens if you start R directly and type those commands into R? I cannot remember the exact circumstances, but sometimes I can update certain packages only when I start R and not from RStudio (well, at least in the past).

1 Like

It is indeed very strange…

I believe I am on the most recent version of Rstudio - 2023.03.1 Build 446.

Unfortunately, running on regular R (as opposed to R studio) makes no difference, I get exactly the same error.

Yes, that is the RStudio version that I am using too, and the most recent one AFAIK.

Interesting that the problem also occurs if you run the commands in R, so that message must come from the package and not from RStudio.

Perhaps it is a Windows 11 issue?

Yesterday I uninstalled R 4.3.0 and Rtools 4.3 on my Windows virtual machine. After reinstalling both, I actually only needed the following commands to get a working rstan installatioon:

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

The first call to install.packages() installed the rstan package (2.21.8) from CRAN and the second then installed rstan 2.26.22 from source from the mc-stan repository.
So it looks as if the cmdstanr package is not needed after all. But one needs a working toolchain to compile from source.

All this is on Windows 10.

I just hope that not too many of my students next semester will have Windows 11 machines, as the use of that OS seems to be the common denominator of all postings on RStan on Windows post R 4.2.0. :-)