Stan logo
  • Stan
  • Getting Started
  • Documentation
  • Learning Resources
    • Tutorials
    • Case Studies
    • StanCon Talks
    • Domain-specific
    • Publications
    • Diagnostics and Warnings
  • Tools
  • About

On this page

  • Download and Install Stan
    • Other Programming Environments
  • Prerequisite: C++17 toolchain
  • Local CmdStan installations for CmdStanPy, CmdStanR, and Stan.jl
  • Troubleshooting the Install
  • High-level Stan Interfaces
  • Introductory Notebooks,  Vignettes,  and Tutorials

Getting Started

To analyze your data with Stan, you can either

  • Use Stan directly from within your preferred programming environment. This option lets you write custom models using the Stan language and then fit them to data.

  • Use a Stan based-modeling package - skip to High-level Stan Interfaces. This option lets you specify your model using formula-based syntax, as in R packages lm and lme4, eliminating the need to learn how to write Stan programs.

Download and Install Stan

To compile and run Stan models directly from within R, Python, or Julia, select your OS, programming language interface, and preferred installation method in the grid below. For other programming environments, skip to Other Programming Environments

Prerequisites

Stan requires a C++17 compiler and some build utilities.. The conda option of certain packages can install these for you, otherwise these are bundled together by Rtools.

Stan requires a C++17 compiler. The conda option of certain packages can install this for you, or we recommend to install Xcode from the App Store and then run xcode-select --install.

Stan requires a C++17 compiler. The conda option of certain packages can install this for you, or on .deb based distros, sudo apt-get install build-essential will install what you need.

How to Install

Please select interface and preferred package manager.

Run pip install cmdstanpy. Then, in Python, run import cmdstanpy; cmdstanpy.install_cmdstan() or follow the manual installation instructions for CmdStan.

For more information, see the CmdStanPy documentation.

Run conda install -c conda-forge cmdstanpy.

Note: this will also install CmdStan and any system prerequisites.

Run pip install -e git+https://github.com/stan-dev/cmdstanpy@develop#egg=cmdstanpy. Then, in Python, run import cmdstanpy; cmdstanpy.install_cmdstan() or follow the manual installation instructions for CmdStan.

For more information, see the CmdStanPy documentation.

In R, run install.packages("cmdstanr", repos = c('https://stan-dev.r-universe.dev', getOption("repos"))). Then run cmdstanr::install_cmdstan() or follow the manual installation instructions for CmdStan.

For more information, see the CmdStanR documentation

Run conda install -c conda-forge r-cmdstanr.

Note: this will also install CmdStan and any system prerequisites.

In R, run remotes::install_github("stan-dev/cmdstanr").

Then run cmdstanr::install_cmdstan() or follow the manual installation instructions for CmdStan.

For more information, see the CmdStanR documentation

Download a release from GitHub: https://github.com/stan-dev/cmdstan/releases.

Then follow these instructions to build CmdStan.

Run conda install -c conda-forge cmdstan.

Note: this will also install CmdStan and any system prerequisites.

Run git clone https://github.com/stan-dev/cmdstan.git --recursive

This will download the source code from the current development branch of CmdStan into a directory named cmdstan, along with the submodules for core Stan code and the Stan math library.

Then follow these instructions to build CmdStan.

In R, run install.packages("rstan")

For more information, see the RStan Getting Started wiki

In R, run install.packages("rstan", repos = c('https://stan-dev.r-universe.dev', getOption("repos"))).

For more information, see the RStan Getting Started wiki

Run conda install -c conda-forge r-rstan.

Note: this will also install any system prerequisites.

remotes::install_github(“stan-dev/rstan”, ref = “develop”, subdir = “rstan/rstan”)

For more information, see the RStan wiki page Installing RStan from Source

To install Stan.jl e.g. in the Julia REPL: ] add Stan, then use Conda.jl or conda to install CmdStan.

See Stan.jl documentation for further details.

Run https://github.com/StanJulia/Stan.jl.git, then follow instructions in the README file.

Note

Conda simplifies the installation process by ensuring that all required libraries and tools are compatible with each other and is available for Linux, Mac, and Windows platforms.

You can either install miniconda, a free, minimal installer for conda or you can get the full Anaconda system which provides graphical installer wizards for MacOS and Windows users.

Julia users can install Conda.jl.

Other Programming Environments

Language Tool Description
R, Python Google Colab Prebuilt CmdStan binaries for Google Colab are available from the GitHub CmdStan releases page. Installing these binaries at the start of a Colab session is much faster than installing CmdStan during a Colab session.
Mathematica MathematicaStan Stan interface for Mathematica. Available from its GitHub repository.
MATLAB MatlabStan Installation instructions available on the MatlabStan wiki.
Python PyStan Available via pip. Run command: python -m pip install pystan.
Note

As of Release 3.10.0, PyStan is no longer being actively supported.

Prerequisite: C++17 toolchain

Stan models are specified using the Stan language which are then compiled to executable programs that can be run on your data to perform inference and make predictions. To use Stan from within your preferred programming environment, you need a C++ toolchain comprised of a C++17 compiler and the GNU Make utility.

  • On Linux, these are bundled into the meta-package build-essential. To install, run command:
    sudo apt-get install build-essential

  • On Mac, the Clang compiler and GNU Make are included with Xcode, the Apple toolset for software developers. Install Xcode from the App Store and then run command:
    xcode-select --install

  • On Windows 10, there are two ways to get a Stan-compatible C++ toolchain:
    • Use the conda installer for CmdStan, CmdStanPy or CmdStanR or RStan, since these packages all include the required toolchain.
    • Get Rtools which includes a C++17 compiler, GNU Make for windows, and a few Unix utilities.
      CmdStanR users can call the internal function cmdstanr:::install_toolchain.
      CmdStanPy provides both the function cmdstanpy::get_cxx_toolchain and command line script get_cxx_toolchain.

Local CmdStan installations for CmdStanPy, CmdStanR, and Stan.jl

CmdStanPy, CmdStanR, and Stan.jl and require a local CmdStan installation. Both CmdStanPy and CmdStanR provide method install_cmdstan to do this from within Python or R; and CmdStanPy also provides this as a command-line function. See the online documentation:

  • CmdStanPy: install_cmdstan function
  • CmdStanR: install_cmdstan function
  • Stan.jl build instructions

The default installation location is in the user’s home directory and is named .cmdstan (a hidden directory). This directory contains one or more versions of CmdStan.

Both CmdStanPy and CmdStanR provide the following functions:

  • rebuild_cmdstan - rebuild the specified release. On Mac, often required after an Xcode update.
  • cmdstan_path - displays the path to the local install of CmdStan; path name includes version number.
  • set_cmdstan_path - specify which version of CmdStan to use.

Troubleshooting the Install

To help troubleshoot problems that arise when trying to use Stan, we provide the following summary of the chain of events in conditioning a model on data and doing inference:

  • Compile model
    • Stan compiler translates Stan file to C++ file
    • C++ file is compiled to executable program, via GNU Make
  • Run inference algorithm
    • Interfaces run compiled executable program
    • Compiled executable generates per-chain outputs

If the program contains syntax errors, these will be caught and reported by the Stan compiler (program stanc). If the Stan program is successfully translated to C++, then it should compile; error messages from the C++ compiler indicate a problem with the C++ toolchain.

If a model fails to run or appears to run slowly, this is a strong indication that the model is poorly specified given the data. Consult the Stan User’s Guide or search the Stan Forums on Discourse

Common Points of failure; how to address them.

  • Software download failed.
    • workaround: check internet connectivity, disk space, and file permissions
  • C++ components fail to compile
    • Workaround: we highly recommend installing using conda to create a clean environment for Stan and its toolchain.
  • Stan model fails to compile with error message about a ““.(PCH file)()
    • Fix: for CmdStan based systems, rebuild CmdStan.

See CmdStan Guide section Troubleshooting the Installation for further details.

High-level Stan Interfaces

Language Tool Description
R brms Use extended lme4-like formula syntax to specify and fit multivariate and multilevel models in Stan. (Requires CmdStanR and C++ compiler.)
R RStanArm Provides stable, efficient Stan versions of R model-fitting packages. (Stan models are pre-compiled, no C++ compiler needed.)
R Rethinking Accompanies the book and course materials for Statistical Rethinking, 2nd Ed by Richard McElreath. (Requires CmdStanR and C++ compiler.)

Introductory Notebooks,  Vignettes,  and Tutorials

Interface Title
CmdStanPy CmdStanPy “Hello, World!”
Getting Started with Bayesian Statistics using Stan and Python
Multilevel regression modeling with CmdStanPy and plotnine
CmdStanR Getting Started with CmdStanR
StanCon2023/Stan_tutorial.ipynb
Julia Stan.jl Examples
Google Colab Stan Notebooks in the Cloud

For more learning resources, see the Tutorials, Publications and Stan Case Studies pages.

Back to top