# Can't make cmdstan 2.18 in Windows

**URL:** <https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088>\
**Category:** CmdStan\
**Created:** [August 5, 2018, 6:33pm UTC](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088 "2018-08-05T18:33:24Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [August 7, 2018, 10:25pm UTC](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088/21 "2018-08-07T22:25:34Z")

</div>

Yes, I can do it tomorrow (european time).

The compiler is MingW-w64 (gcc 5.3?)

There was a `find.exe` with windows. Before I hacked cmder to use unix style find, but I found `m2-findutils` which probably means everything can be installed with conda.

Steps with conda:

1. Download and install miniconda3 x64 (I recommend python3)
2. Open anaconda prompt (or cmd if miniconda is found in PATH)
3. Create conda environment

```
    #optional step - creates "isolated" environment
    conda create -n cmdstan python=3.7
    # activate env - this is needed everytime you 
    # open commandline, unless you skip creating the conda env
    conda activate cmdstan
    conda install make m2w64-toolchain m2-findutils -c msys2

```

1. cd to cmdstan folder and use `gnumake` as make. There is also `mingw32-make` which came with the toolchain, it works too.

```
    gnumake build -j4

```

If needed, edit `makefile` and add `CC=gcc` and `CXX=g++` (or/and `CPP=g++`). These might be needed if msvc was installed on your computer, not sure if this is true.

---

<div class="post-metadata">

**Author:** ![Cyianor](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cyianor/32/1727_2.png) [@Cyianor](https://discourse.mc-stan.org/u/Cyianor)\
**Post date:** [August 9, 2018, 11:53am UTC](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088/22 "2018-08-09T11:53:35Z")

</div>

I can confirm that this works. However, I had to do everything in the Anaconda prompt since a lot of paths (e.g. to mingw binaries) are not set otherwise, even if the “normal” Anaconda paths are set.

Also, `gnumake` does not seem to be installed through the packages. Using `mingw32-make`, as you noted @ahartikainen, works perfectly fine.

In the Wiki it should maybe also be noted that even the model has to be build from the same prompt, to ensure the same build environment.

EDIT: Using `mingw32-make` and the anaconda prompt, the only change to the makefiles I had to make was to add `CXXFLAGS += -DSTAN_THREADS`. Btw, @wds15, does this change have to be made for each sub-repository or is it enough to make it in `stan-math`?

---

<div class="post-metadata">

**Author:** ![Cyianor](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cyianor/32/1727_2.png) [@Cyianor](https://discourse.mc-stan.org/u/Cyianor)\
**Post date:** [August 9, 2018, 11:54am UTC](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088/23 "2018-08-09T11:54:41Z")

</div>

> [@ahartikainen](#):
>
> MSVC can’t compile Stan source. I assume it is the Sundials that are failing.

That seems a bit weird. I recall compiling sundials through MSVC at some point. It might however be more complicated than simply exchanging `cc` for `cl`.

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [August 9, 2018, 12:08pm UTC](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088/24 "2018-08-09T12:08:55Z")

</div>

In normal cmd you need to activate the environment, even if you use the base

```
conda env list

# activate your base environment
conda activate base

```

There are

```
# msys2
mingw32-make 

# anaconda channel
make

# conda-forge channel
gnumake

```

Did the find.exe throw spme garbage before compilation (about wrong cygwin stuff)?

This is due to “incompatibility” between `m2` and `m2w64` packages.

---

<div class="post-metadata">

**Author:** ![wds15](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wds15/32/908_2.png) [@wds15](https://discourse.mc-stan.org/u/wds15)\
**Post date:** [August 9, 2018, 12:56pm UTC](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088/25 "2018-08-09T12:56:41Z")

</div>

> [@Cyianor](#):
>
> Btw, @wds15, does this change have to be made for each sub-repository or is it enough to make it in `stan-math` ?

this change only needs to made to whatever repository you use. So if you run a threaded test in `stan-math`, then do it there. In case you run a threaded cmdstan model, then you only need to edit `cmdstan/make/local` (but an entry for the nested stan\_math won’t hurt nor be useful).

---

<div class="post-metadata">

**Author:** ![Cyianor](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cyianor/32/1727_2.png) [@Cyianor](https://discourse.mc-stan.org/u/Cyianor)\
**Post date:** [August 9, 2018, 12:56pm UTC](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088/26 "2018-08-09T12:56:43Z")

</div>

> [@ahartikainen](#):
>
> In normal cmd you need to activate the environment, even if you use the base

My bad. I had an old version of Anaconda installed. I uninstalled it, installed the newest miniconda and now that part works great. In `cmd` in any case, not in PowerShell, what I usually use. But I see that is a general `conda` [issue](https://github.com/conda/conda/issues/626).

If I install everything as you describe above (so not installing `gnumake` for now) I get some weird error messages when I try to use `make`

```no-highlight
1 [main] make 12488 find_fast_cwd: WARNING: Couldn't compute FAST_CWD pointer. Please report this problem to
the public mailing list cygwin@cygwin.com

```

and then a bunch of complains about not finding `/bin/sh`. Afterwards it starts compiling though. This makes it necessary to use `mingw32-make`. I wonder why you install `make` at all, in the instructions above. You maybe meant to write `gnumake`?

EDIT: Sometimes I should try stuff on the command line first before writing. So installing the package `make` from the channel `conda-forge` gives you `gnumake` and that works fine as well. So the difference is maybe that in your miniconda you prefer `conda-forge` over the standard base channel and that is why get `gnumake`?

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [August 9, 2018, 1:07pm UTC](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088/27 "2018-08-09T13:07:17Z")

</div>

Yes, using conda-forge one gets usually more up-to-date packages on windows. It is a python thing.

The error are coming from the find.exe stuff (or atleast I think they are coming from it). Even so, find.exe works.

Maybe instructions should only use mingw32-make.

Btw, on windows one can use also find.exe found in cmder tools, if one uses that, but in that case one must touch PATH.

---

<div class="post-metadata">

**Author:** ![Cyianor](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cyianor/32/1727_2.png) [@Cyianor](https://discourse.mc-stan.org/u/Cyianor)\
**Post date:** [August 9, 2018, 1:08pm UTC](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088/28 "2018-08-09T13:08:14Z")

</div>

> [@wds15](#):
>
> this change only needs to made to whatever repository you use. So if you run a threaded test in `stan-math` , then do it there. In case you run a threaded cmdstan model, then you only need to edit `cmdstan/make/local` (but an entry for the nested stan\_math won’t hurt nor be useful).

Good to know! That actually seems to break it again though… During my last test I had only added `CXXFLAGS += -DSTAN_THREADS` in the `stan-math` sub-module while compiling from the `cmdstan` repository. Now that I set the flag in `make/local` directly in the `cmdstan` main folder, I get the old behaviour. I compiled cmdstan like @ahartikainen describes, I compiled your example model from above, @wds15, but then it only prints the cmdstan config and exits before sampling.

Did you set the respective compile flags, @ahartikainen?

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [August 9, 2018, 1:16pm UTC](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088/29 "2018-08-09T13:16:39Z")

</div>

No, apparently I did not read the instructions.

I edited the makefile and added the `CXXFLAGS += -DSTAN_THREADS` there.

I need to try local. Did you see that flag added to command when you compiled your model make/local. Are files without a extensions even a thing?

---

<div class="post-metadata">

**Author:** ![Cyianor](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cyianor/32/1727_2.png) [@Cyianor](https://discourse.mc-stan.org/u/Cyianor)\
**Post date:** [August 9, 2018, 1:21pm UTC](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088/30 "2018-08-09T13:21:22Z")

</div>

The cmdstan Makefile starts like this

```
# CmdStan users: if you need to customize make options,
# you should add variables to a new file called
# make/local (no file extension)

```

As far as I can see it the Stan Makefile reads in a file called `local` in the folder `make` if it exists. There you can simply add your own modifications. Files without an extension are a big thing once you leave the Windows world ;-)

When you clone a fresh copy of cmdstan the file `make/local` isn’t there. So I created it and added exactly one line to it

```
CXXFLAGS += -DSTAN_THREADS

```

This way it works great on Ubuntu.

On Windows the compilation still seems to work, but not the sampling.

@ahartikainen I really like your way to set up a mingw toolchain though. It’s convenient and fast.

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [August 9, 2018, 1:30pm UTC](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088/31 "2018-08-09T13:30:31Z")

</div>

Yes, I meant if they are a thing on Windows.

So

1. `make build` works?

2. With `make/local` compiles

3. You can not compile from the `model.exe`

4. Is the bernoulli model working with your current setup?

5. If you edit Makefile can you sample your model?

And have you cleaned old cpp/exe files before new compilations? (Is there a make command that forces all the steps?)

ps. Are you using the common \ / quirks found on windows? E.g running exe needs \ but commands need /

---

<div class="post-metadata">

**Author:** ![Cyianor](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cyianor/32/1727_2.png) [@Cyianor](https://discourse.mc-stan.org/u/Cyianor)\
**Post date:** [August 9, 2018, 1:58pm UTC](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088/32 "2018-08-09T13:58:02Z")

</div>

> [@ahartikainen](#):
>
> Yes, I meant if they are a thing on Windows.

I guess. `Makefile` is also one of those.

Yes to 1 and 2. I can compile @wds15’s sample model without a problem. The Bernoulli example compiles and samples as expected.

I cleaned everything with `gnumake clean-all` . I tried to abide to the Windows \ / quirks. Like `gnumake ../model-path/model.exe` to build the model. I know that `\` makes problems here.

Well… this is black magic, isn’t it? Now it samples :-D I could swear I didn’t do anything different. It is also quite a bit slower as before, which might be expected with the added overhead. So maybe this toolchain really is the way to go. Thanks @ahartikainen

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [August 9, 2018, 2:00pm UTC](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088/33 "2018-08-09T14:00:14Z")

</div>

> [@Cyianor](#):
>
> this is black magic, isn’t it?

just Windows.

Great that it works!

---

<div class="post-metadata">

**Author:** ![Cyianor](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cyianor/32/1727_2.png) [@Cyianor](https://discourse.mc-stan.org/u/Cyianor)\
**Post date:** [August 9, 2018, 2:20pm UTC](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088/34 "2018-08-09T14:20:58Z")

</div>

Well… this isn’t over yet :-D Motivated by the success with @wds15’s test model, I wanted to compile and sample from one of my own models involving ODEs. I did a proper cleanup of old executables and headers and the model builds fine. However, it exits before sampling.

I am quite sure that the model is correct as it compiles and samples with threading support on Ubuntu and a cluster running CentOS.

If I find time soon I’ll try to break it down to a smaller model to isolate the issue a bit. (Or to see that it works. Then the problem is somewhere in the size of my model maybe, we’ll see)

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [August 9, 2018, 10:33pm UTC](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088/35 "2018-08-09T22:33:40Z")

</div>

I added the instructions to wiki.

I still need to test if there are some problems on powershell, but cmd should work.

I skipped the anaconda prompt and cmder (optional).

Also we need to test that this really works for all the models.

---

<div class="post-metadata">

**Author:** ![Ole\_Petter\_Hansen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ole_petter_hansen/32/2958_2.png) [@Ole\_Petter\_Hansen](https://discourse.mc-stan.org/u/Ole_Petter_Hansen)\
**Post date:** [August 10, 2018, 12:39pm UTC](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088/36 "2018-08-10T12:39:28Z")

</div>

I’m, trying to do the steps in the guide - but how do I set `STAN_NUM_THREADS`?

Found a description of make/local in the cmdstan-guide, but can’t find anything on `STAN_NUM_THREADS`.

---

<div class="post-metadata">

**Author:** ![Cyianor](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cyianor/32/1727_2.png) [@Cyianor](https://discourse.mc-stan.org/u/Cyianor)\
**Post date:** [August 10, 2018, 12:52pm UTC](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088/37 "2018-08-10T12:52:11Z")

</div>

> [@Ole\_Petter\_Hansen](#):
>
> but how do I set `STAN_NUM_THREADS` ?

It’s an environment variable that you set directly before you start sampling from your model. If you work in `cmd` you would write

```no-highlight
set STAN_NUM_THREADS=-1

```

if you want Stan to figure out the best number of threads or

```no-highlight
set STAN_NUM_THREADS=4

```

where you are setting an upper limit yourself (even though Stan might end up using less than four threads).

In Powershell you would put the variable into `Env`, i.e.

```no-highlight
$Env:STAN_NUM_THREADS=-1

```

Let us know how it goes. As you can read above, it is still a bit troublesome whether or not within-chain-parallelisation works for all models.

---

<div class="post-metadata">

**Author:** ![Cyianor](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cyianor/32/1727_2.png) [@Cyianor](https://discourse.mc-stan.org/u/Cyianor)\
**Post date:** [August 10, 2018, 12:55pm UTC](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088/38 "2018-08-10T12:55:52Z")

</div>

> [@ahartikainen](#):
>
> I still need to test if there are some problems on powershell

Powershell and Conda environments don’t really play well together. This has been an [issue](https://github.com/conda/conda/issues/626) since 2014 but never been properly solved by the conda team.

---

<div class="post-metadata">

**Author:** ![Ole\_Petter\_Hansen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ole_petter_hansen/32/2958_2.png) [@Ole\_Petter\_Hansen](https://discourse.mc-stan.org/u/Ole_Petter_Hansen)\
**Post date:** [August 10, 2018, 12:57pm UTC](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088/39 "2018-08-10T12:57:00Z")

</div>

Thank you, @Cyianor. Looks like I get the same result as you - i.e. it just exits when STAN\_NUM\_THREADS is greater than 1.

Edit: I used the model and data above, and have created the make/local file with `CXXFLAGS += -DSTAN_THREADS` in the cmdstan-folder. Apart from that followed the instructions [here](https://github.com/stan-dev/math/wiki/Threading-Support).

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [August 10, 2018, 2:30pm UTC](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088/40 "2018-08-10T14:30:33Z")

</div>

Are there any exceptions?

Can you add `-fexceptions` to make/local

Is there possibility to run with pbr?

[Previous page](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088.md?page=1)

[Next page](https://discourse.mc-stan.org/t/cant-make-cmdstan-2-18-in-windows/5088.md?page=3)
