R 4.0, rstan, and you

I just had the same problem as @mbschrimpf, and your solution @andrjohns of using rstan::stan() worked for me too. Thanks a lot!

@Faith Glad that works! We’ll be releasing a new version of rstan sometime this month that will fix this, so using the rstan:: prefix shouldn’t be necessary for much longer.

1 Like

Hi Andrjohns
I try to run example(…). Then i also get the same error. And it still didn’t work when i run “rstan::stan(model,data)”.

make: *** [D:/ProgramFiles/...../Makeconf:229: file325024c96541.o] Error 127
Error in file(con, "r") : cannot open the connection
In addition: Warning messages:
1: In file.remove(c(unprocessed, processed)) :
  cannot remove file 'C:\Users\...RtmpwnxDM2\file3250af149e2.stan', reason 'No such file or directory'
2: In file(con, "r") :
  cannot open file 'file325024c96541.cpp.err.txt': No such file or directory

@andrjohns - Hi! I am trying to install rstan for the first time in RStudio 4, and am struggling to implement instructions online because I am not sure what is not updated. Can you please help?
RStudio is returning error messages when I install numerous packages “there is no package called x”

I just went through installing everything on a cloud server. YMMV, but I had to add
install.packages(c("remotes", "knitr", "xml2", "devtools") before installing the dev version (if that’s what you want)

remotes::install_github("stan-dev/rstan", ref = "develop", subdir = "rstan/rstan", build_opts = "")

I was planning on updating the install instructions over the weekend, but no better time for a first draft!

I’m going to assume that you’re on windows, let me know if that’s not the case.

Install RStan on R4.0 (Windows)

Remove old RStan configuration

If you had previously installed RStan under R3.6, you may have some configuration files left over that will cause issues under R4.0.

The first thing to check is whether the BINPREF environment variable is pointing to the old RTools installation by running Sys.getenv("BINPREF").

You can skip to the next section if your output looks like the following:

[1] ""

However, if your output looks like:

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

Then you need to check whether your .Rprofile file has been configured to set this variable on R startup. If you run the following command and get the output:

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

Then you need to delete your .Rprofile file. You can get the location of this file by running:

file.path(Sys.getenv("HOME"), ".Rprofile")

Delete that file and then restart R. If BINPREF is no longer set, then you should get the following output:

[1] ""

RTools4 Installation

The next step is to install RTools4 following the instructions here: https://cran.r-project.org/bin/windows/Rtools/

Make sure that you follow the instructions that put RTools on the path. Repeated here for clarity:

writeLines('PATH="${RTOOLS40_HOME}\\usr\\bin;${PATH}"', con = "~/.Renviron")

Then restart R and verify that RTools is being found via:

## "C:\\rtools40\\usr\\bin\\make.exe"

And that you can install packages from source:

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

Install RStan

Install Packages

You can now install RStan via:

install.packages("rstan", repos = "https://cloud.r-project.org/",
                 dependencies = TRUE)

Before moving to the next section verify that the following packages are at or above the indicated version:

  • rstan: 2.21.2
  • StanHeaders: 2.21.0-6
  • inline: 0.3.16


You can speed up the estimation of your models by enabling some compiler options. To do this, you should add the following to your Makevars.win file:

CXX14FLAGS += -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2

Note that earlier installation guides also recommended the -march=native flag, but this will cause R to crash with RTools4 and is no longer recommended.

If you have other CXXFLAGS, CXX11FLAGS, or CXX14FLAGS lines already in your Makevars.win file, make sure that you delete those first. If you do not already have a Makevars.win file, or do not know where it is, then you can create this via R:

dotR <- file.path(Sys.getenv("HOME"), ".R")
if (!file.exists(dotR)) dir.create(dotR)
M <- file.path(dotR, "Makevars.win")
if (!file.exists(M)) file.create(M)
cat("\nCXX14FLAGS += -O3 -mtune=native -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2",
     file = M, sep = "\n", append = FALSE)

Verify Installation

To verify your installation, you can run the RStan example/test model:

example(stan_model,run.dontrun = TRUE)

The model should then compile and sample. You may also see the warning:

Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found

This is safe to ignore and will be removed in the next RStan release.

Encountering Errors

If you are unable to install RStan or successfully run the example/test model, then please open a new thread on the Stan forums (https://discourse.mc-stan.org) with a description of your problem and any error message, and we will help troubleshoot your installation.


@jonah @bgoodri

I’ll be working on updating the installation instructions on the wiki over the weekend. The above is a draft of the instructions for working with R4.0 on Windows and identifying some of the common issues, can you have a look and see if there’s anything that I’ve missed?

Also, are there any specific things that I should put in the Linux & Mac writeups? The following is all that I’m aware of:


  • c++14 compatible compiler


  • Not recommended with R3.6
  • Use the r-macos-rtools package
  • No -march=native flag

@torkar Was there anything else you’ve come across on Mac that should go in the install guide?


“Do you want to install from sources the packages which need compilation?”


Instead of the warning message you indicated, I got:
Warning message:
In example(stan_model, run.dontrun = TRUE) : no help found for ‘stan_model’

Thank you for all your help by the way

Oops, I forgot to include the instruction to load RStan first:

example(stan_model,run.dontrun = TRUE)

This looks like what you were talking about (below), but I am still getting errors in RStudio when trying to run rstan commands or install rethinking.

Loading required package: StanHeaders
Loading required package: ggplot2
rstan (Version 2.21.2, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
Do not specify ‘-march=native’ in ‘LOCAL_CPPFLAGS’ or a Makevars file
example(stan_model,run.dontrun = TRUE)

stn_md> stancode <- ‘data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}’

stn_md> mod <- stan_model(model_code = stancode, verbose = TRUE)

TRANSLATING MODEL ‘73fc79f8b1915e8208c736914c86d1a1’ FROM Stan CODE TO C++ CODE NOW.
successful in parsing the Stan model ‘73fc79f8b1915e8208c736914c86d1a1’.
COMPILING THE C++ CODE FOR MODEL ‘73fc79f8b1915e8208c736914c86d1a1’ NOW.
OS: x86_64, mingw32; rstan: 2.21.2; Rcpp: 1.0.5; inline: 0.3.15
Error in withr::set_makevars(new, path, state, assignment = assignment) :
Multiple results for CXX14FLAGS found, something is wrong.FALSE
In addition: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
‘C:/rtools40/usr/mingw_/bin/g++’ not found

Something else I missed!

Can you try running:

dotR <- file.path(Sys.getenv("HOME"), ".R")
if (!file.exists(dotR)) dir.create(dotR)
M <- file.path(dotR, "Makevars.win")
if (!file.exists(M)) file.create(M)
cat("\nCXX14FLAGS += -O3 -mtune=native -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2",
     file = M, sep = "\n", append = FALSE)

And then restart R and try the example again?

1 Like

It ran a bunch of code and eventually spat out (below). In a new markdown file, I tried calling the rstan library and am still getting no package called rstan. I have to put this away for now but I really appreciate your help and will check back later.

<U+2714> Packages successfully installed.
There were 50 or more warnings (use warnings() to see the first 50)

Install R >4.0.1 , latest RStudio Desktop, latest Xcode development toolkit (check with xcode-select --install in the terminal), install rstan.

For fortran I don’t recall where I got it from, but my Makevars look like this:

CXX14FLAGS=-O3 -mtune=native 
CXX14FLAGS += -arch x86_64 -ftemplate-depth-256 -Wno-unused-local-typedefs -Wno-unused-function
FC = /usr/local/bin/gfortran
FLIBS = -L/usr/local/lib/gcc/10 -L/usr/local/lib -lgfortran -lquadmath -lm

i.e., no -march=native flag.


Thanks! I’ll ping you when I’ve got something drafted


I keep seeing non-zero exit status for pkgbuild and callr.
For rstan it says unpacked and md5 sums checked.
I googled non-zero status and found that that means error and it wasnt installed?

Also then I tried installing rethinking and got this (I have R version 4.0.2)
Installing package into �C:/Users/AACH4770/Documents/R/win-library/3.4�
(as �lib� is unspecified)
Warning in install.packages :
package ‘rethinking’ is not available (for R version 3.4.3)

Once rstan is installed you can install rethinking,


I don’t think that you’re on R4.0.2. This error:

package ‘rethinking’ is not available (for R version 3.4.3)

Implies that RStudio is using R 3.4.3. If you have have 4.0.2 installed, you can follow this guide to get RStudio to use it: https://support.rstudio.com/hc/en-us/articles/200486138-Changing-R-versions-for-RStudio-desktop

I realize how that sounds but…

Just realized I have 4.0.2 but I havent uninstalled prev. versions… Will try.