A Simple Installing of CmdStanPy
With CmdStanPy, Python users can enjoy Bayesian analysis using Stan. CmdStanPy can be installed in a simple way as shown bellow.
CmdStanPy can be installed on Ubuntu by Miniconda. Windows users can install CmdStanPy on Ubuntu, which is installed on Windows by WSL (Windows Subsystem for Linux).
First, preparation for Windows users is explained.
Then, installation on Linux is explained.
A simple example of using CmdStanPy is shown.
A more practical example is shown.
It is easy to install CmdStanPy on Ubuntu. So, when we use CmdStanPy, first install Ubuntu on Windows, then install CmdStanPy on Ubuntu on Windows.
Ubuntu can be installed on Windows by Windows Subsystem for Linux (WSL).
Start Windows Powershell, by selecting the item Windows Powershell on the start menu (Figure A1).

Figure A1 Start menu
A list of Linuxes, which can be installed by a simple command is shown by the command “wsl -–list --online”(Figure A2).

Figure A2
When you install Ubuntu, run the command “wsl -–install Ubuntu”(Figure A3).

Figure A3
At the end of installation, password is required to be set.

Figure A4
After setting password, quit the Ubuntu to update.
Execute “exit” command twice (Figure A5).

Figure A5
To run the Ubuntu, select the icon Ubuntu in the start menu.

Figure A6 Start menu.
To update and upgrade, run a command “sudo apt update && sudo apt upgrade” (Figure A7)
.

Figure A7
After updating and upgrading, execute command “exit” (Figure A8). After restarting, updated and upgraded system becomes effective.

Figure A8
Installing CmdStanPy on Linux (e.g., Ubuntu, Fedora etc).
CmdStanPy requires C++ compiler. A simple method of installing C++ compiler is to install g++.
g++ can be installed by command “sudo apt install g++” (Figure B1).

Figure B1
CmdStanPy can be installed easily by conda command of Miniconda.
The file, by which Miniconda can be installed, is available on the following website
https://www.anaconda.com/docs/getting-started/miniconda/main
In case of Ubuntu on WSL, files can be downloaded by Browsers of Windows.
Figure B2 shows that the downloaded file Miniconda3-latest-Linux86_64.sh is stored in Folder “C:Download”, which is denoted as “/mnt/c/Download” in Linux. The directory C: of Windows is represented by lower case c. Directories of Windows are under the directory /mnt of Linux.

Figure B2
Go to the directory where the file to install Miniconda is put, then execute the file (Figure B3).

Figure B3
At the end of installation, choice about initialization is asked.
I recommend to choose “yes” (Figure B4).

Figure B4
After the installation, shut down Ubuntu (Figure B5).
![]()
Figure B5
Choosing Ubuntu on start menu, start Ubuntu (Figure B6).

Figure B6
When the terminal window of Ubuntu opens, “(base)” is shown at the head of the prompt (Figure B7).
This means that you are in the virtual environment of Miniconda called “base”. In a virtual environment, you can use usual commands of Linux.

Figure B7
First, update Miniconda. We must execute two types of update for the “base” environment.
Execute the command “conda update -n base conda” (Figure B8).

Figure B8
Then, execute the command “conda update --all”. (Figure B9)

Figure B9
After these updatings, create a virtual environment with CmdStanPy installed.
Execute the following command(Figure B10)
(base) yasuh@pcyo:~$
conda create -n stan -c conda-forge
cmdstanpy
![]()
Figure B10
After creating, activate the created environment “stan”(Figure B11). The name “stan” is given by parameter “-n stan”. If you want “another_name”, use parameter “-n another_name”. In this case, activation command is “conda activate another_name”

Figure B11
When the environment “stan” is activated, “(stan)” is displayed at the head of prompts (Figure B11).
To update the environment, execute command “conda update --all”.
After updating, shutdown the Linux (Figure B12).
![]()
Figure B12
The scripts in Figures C1 and C2 have been developed with the editor of Python IDLE for Windows.
The file of script in Figure 1C is named model1.stan and written in Stan.

Figure C1
The file of script in Figure C2 is named main1.py and written in Python.

Figure C2
The script files are in the same folder C:\samples.

Figure C3
To execute the files, run Ubuntu and change the current directory to /mnt/c/samples, which means C:\samples of Windows (Figure C4).

Figure C4
Run command “python main1.py”.
The script is run as shown in Figure C5.

Figure C5
When you run the script, which was successfully executed in another environment, RuntimeError might be invoked (Figure C6).

Figure C6
In that case, add parameter “force_compile=True” to function CmdStanModel (Figure C7).

Figure C7
Run the script main1.py in Figure C7, the Stan script is rebuilt, and the error would disappear.
The Python script in Figure D1 displays trace plot of MCMC sampling

Figure D1
The script uses modules matplotlib and arviz.

Figure D2
When you install arviz 0.22.0, conflict error would be shown (Figure D3).

Figure D3
To install the adequate arviz, Python, which is installed, must be down graded.
Run the command “conda install python=3.13” (Figure D4). By this command, the installed Python is replaced by python 3.13, that is adequate in the environment.

Figure D4
Install arviz 0.22.0 by command “conda install arviz==0.22.0”(Figure D6).
![]()
Figure D5
Execute command “python main2.py” (Figure D6).
![]()
Figure D6
Trace plot is shown.

Figure D7