Full installation instructions for CmdStan are in the CmdStan User’s Guide; here we focus specifically on the most straightforward way of installing on Windows 10.
Install conda
Using the conda package management system is the recommended way of installing CmdStan on Windows. It allows one to install a pre-built version of CmdStan along with all required dependencies. Assuming that you do not already have conda installed, follow these steps:
- Point your browser at the Miniconda Home Page.
- Scroll down to “Latest Miniconda Installer Links”.
- Click on “Miniconda3 Windows 64-bit” (Name) for Windows (Platform) to download the installer.
- Run the downloaded installer, Miniconda3-latest-Windows-x86_64.exe.
- When the installer asks, it is recommended to choose “Install for: Just Me” and, if at all possible, “Register Miniconda3 as my default Python 3.9”.
- You will run the conda commands from within an Anaconda Prompt (Miniconda3) window, which you will now find in your Windows Start menu.
Here are some additional conda resources that you most likely will not need:
Install CmdStan
Make sure you have an Anaconda Prompt window open (as described at the end of the previous section) and issue all of the following commands within that window.
- Download CmdStan and all required dependencies in a new conda environment called “stan”. Answer “y” to the “Proceed” prompt. This will take a while.
conda create -n stan -c conda-forge cmdstan
- Activate the stan environment.
conda activate stan
- See where your new stan environment is located; there should be a star after its name in the listing.
conda info -e
- Take note of the path to the CmdStan installation directory; it will be nested within the stan environment directory.
echo %CMDSTAN%
Verify CmdStan installation
The following is just a quick check to verify that CmdStan is properly installed:
- Change to the CmdStan installation directory.
cd /d "%CMDSTAN%"
- Make an example Stan model. Note the forward slashes; this is important.
mingw32-make examples/bernoulli/bernoulli.exe
- Now run the model on the data in the accompanying .data.json file. This should be very fast.
examples\bernoulli\bernoulli.exe sample data ^ file=examples/bernoulli/bernoulli.data.json
The output will look something like this: - See a summary of the parameter estimates. The mean of theta, +/- one standard deviation, should include 0.2.
bin\stansummary.exe output.csv
The output will look something like this:
Using CmdStan outside of Anaconda Prompt
The Anaconda Prompt is convenient because, once you activate the stan environment (conda activate stan
), the CMDSTAN environment variable is set to the “work” directory for CmdStan (from which you can make models and run them) and your PATH is modified to include the directories containing the Stan compiler and all supporting tools.
But maybe you want to run CmdStan from PowerShell, or the normal Windows command prompt, or from a script. Then here’s what you need to do.
- If you haven’t already done so, open Anaconda Prompt and issue the command
conda activate stan
- Get the values of the environment variables CMDSTAN and CONDA_PREFIX and save them away somewhere.
echo %CMDSTAN%
echo %CONDA_PREFIX% - From within PowerShell, the Windows Command Prompt, or a script, you can then set up your environment as follows:
set CMDSTAN=cmdstan_value
set TMP=conda_prefix_value
set PATH=%TMP%;%TMP%\Library\mingw-w64\bin;%TMP%\Library\usr\bin;%TMP%\Library\bin;%TMP%\Scripts;%TMP%\bin;%PATH%;.
Optional: Uninstalling Miniconda3
This is a bit of a delicate proposition because the %CONDA_PREFIX% directory containing CmdStan and all its supporting tools is located within your Miniconda3 installation. So this is what you need to do first:
- Choose a new location for your CmdStan installation, somewhere outside of the Miniconda3 installation; let’s call this stanenv.
- Open the Windows Command Prompt and issue the following command:
move conda_prefix_value stanenv
Now you can do the actual uninstall:
- Let condainst be the path to the root of the Miniconda3 installation.
- Issue the following command:
condainst\Uninstall-Miniconda3
- An uninstaller will pop up; click through the steps.
Note that when setting up your environment to use CmdStan as described in step 3 of Using CmdStan outside of an Anaconda Prompt, you will now need to replace conda_prefix_value with stanenv and modify cmdstan_value accordingly.