Installing RStan on Windows

Brett Melbourne edited this page Sep 11, 2018 · 26 revisions

This page contains instructions for installing RStan for Windows users only. Mac and Linux users should consult the separate instructions at Installing RStan on Mac or Linux. If you are using Windows >= 10 but trying to install RStan inside Windows Subsystem for Linux (WSL), then you should instead use the aforementioned link / instructions for Linux.

Jump to:



R version 3.4.0 or later is required, which is available from

Follow the download link, then choose a mirror (we recommend because it redirects to the closest reliable mirror), then click on the link for Windows. On the next page select the link to the "base" package.


Although it is not required, for most users we strongly recommend installing RStudio which has basic support for .stan file types and syntax highlighting for Stan 2.10.0 and higher (if using RStudio version >= 0.99.1259).


Rtools provides a toolchain for Windows that works well with R. It mainly includes GNU make, GNU gcc, and other utilities commonly used on UNIX-ish platforms.

Download Rtools

You can download Rtools from Choose the most recent "frozen" version that supports the version of R you have installed. If you have more than one version of R, it is possible to have more than one version of Rtools, but you may have to adjust some environmental variables to compile a Stan program.

Install Rtools

When installing version of Rtools you downloaded, pay careful attention during the step where you can edit the system PATH. As shown in the image below, check the box for this option (you don't need to type anything, just check the box). This will allow the C++ compiler included in Rtools to be used by R.


Verify that Rtools can be used in R

  • Close your current R session.

  • Open a new R session.

  • Execute Sys.getenv("PATH") and check that Rtools is included in system PATH. If Rtools is installed to c:\\Rtools then you should see something like this:

> Sys.getenv('PATH')
[1] "c:\\\\Rtools\\\\bin;...

If Rtools is installed to c:\\Rtools (or in some cases C:\RBuildTools...) and these do not appear you should make sure you followed the instructions in the Install Rtools section above. If you followed the instructions without success one option is to remove and reinstall Rtools and another option is to edit system PATH manually (or using some other tool). For simplicity, we suggest removing and reinstalling Rtools and make sure PATH is edited (by checking the box as described above) during the installation.

  • Check that g++ can be called from R. For example, we can see the version of gcc in R as follows.
> system('g++ -v')
Using built-in specs.
Target: x86_64-w64-mingw32
Configured with: 
Thread model: posix
gcc version 4.9.3 (x86_64-posix-seh, Built by MinGW-W64 project) 
> system('where make')

If you do not see any output from the system call, remove and reinstall Rtools as above.


You need to execute the following once. Depending on where Rtools is installed, you need to execute one and only one of the following two commands. If Rtools is installed in c:\\Rtools:

cat('Sys.setenv(BINPREF = "C:/Rtools/mingw_$(WIN)/bin/")',
    file = file.path(Sys.getenv("HOME"), ".Rprofile"), 
    sep = "\n", append = TRUE)

If Rtools is installed in C:\RBuildTools... then this command is (note that you may have to adjust if version numbers are nested, e.g 3.4 is installed under C:\RBuildTools\3.4\...):

cat('Sys.setenv(BINPREF = "C:/RBuildTools/3.4/mingw_$(WIN)/bin/")',
    file = file.path(Sys.getenv("HOME"), ".Rprofile"), 
    sep = "\n", append = TRUE)

Optional Configuration

This subsection is optional in the sense that RStan should work without it. Nevertheless, the following is recommended to obtain maximum performance when using RStan.

If you do not already have one, create a personal Makevars file as described at Edit the Makevars file to include the following line:

CXXFLAGS=-O3 -Wno-unused-variable -Wno-unused-function

Be advised that setting the optimization level to 3 (CXXFLAGS=-O3) may prevent some other R packages from installing from source if they are only tested with the stock R configuration.

The code below should work to create or edit this file programmatically from R:

dotR <- file.path(Sys.getenv("HOME"), ".R")
if (!file.exists(dotR)) 
M <- file.path(dotR, "Makevars")
if (!file.exists(M)) 
cat("\nCXXFLAGS=-O3 -Wno-unused-variable -Wno-unused-function", 
    file = M, sep = "\n", append = TRUE)

Installing RStan

You're finally ready to install RStan!

You can install RStan by following these steps in order:

  • Open R (either the R GUI, in the terminal using command R, or by opening the recommended RStudio application).

  • Install the latest version of RStan and the packages it depends on and suggests from CRAN exactly like this:

install.packages("rstan", repos = "", dependencies=TRUE)

Make sure not to omit dependencies=TRUE.

  • If all else fails you can try to install the rstan package from source via
# note: replace the '4' with the number of cores you want to devote to the build
Sys.setenv(MAKEFLAGS = "-j4") 
install.packages("rstan", type = "source")
  • Restart R after the installation but do not load the rstan package. Verify that no objects created by an older version of RStan are (perhaps auto-)loaded into R .

  • Verify that your toolchain works by executing the code below in R and checking that it returns the value 10:

fx <- inline::cxxfunction( signature(x = "integer", y = "numeric" ) , '
	return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
' )
fx( 2L, 5 ) # should be 10
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.