# StanSample.jl can't find cmdstan?

**URL:** <https://discourse.julialang.org/t/stansample-jl-cant-find-cmdstan/75451>\
**Category:** New to Julia\
**Tags:** stan\
**Created:** [January 30, 2022, 5:35am UTC](https://discourse.julialang.org/t/stansample-jl-cant-find-cmdstan/75451 "2022-01-30T05:35:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeetoomHeida](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peetoomheida/32/23865_2.png) [@PeetoomHeida](https://discourse.julialang.org/u/PeetoomHeida)\
**Post date:** [January 30, 2022, 5:35am UTC](https://discourse.julialang.org/t/stansample-jl-cant-find-cmdstan/75451/1 "2022-01-30T05:35:11Z")

</div>

I am having trouble getting a Stan model to run through Julia. I have installed cmdstan following [these instructions](https://mc-stan.org/docs/2_28/cmdstan-guide/cmdstan-installation.html). cmdstan is now installed in `/home/isaac/.conda/envs/stan-env/bin/cmdstan`. I have added `export CMDSTAN=/home/isaac/.conda/envs/stan-env/bin/cmdstan` to `~/.bashrc` and have also added `ENV["CMDSTAN"]="/home/isaac/.conda/envs/stan-env/bin/cmdstan"` to `/home/isaac/packages/julias/julia-1.7/etc/julia/startup.jl` following [these instructions](https://stanjulia.github.io/Stan.jl/latest/INSTALLATION/). I am currently running the example model provided [here](https://stanjulia.github.io/Stan.jl/latest/WALKTHROUGH/). When I attempt

```julia
sm = SampleModel("bernoulli", model);

```

I get the following stack trace:

```julia
julia> sm = SampleModel("bernoulli", model);

/tmp/jl_auygYa/bernoulli.stan updated.
ERROR: IOError: could not spawn `make -f /home/isaac/.conda/envs/stan-env/bin/cmdstan/makefile -C /home/isaac/.conda/envs/stan-env/bin/cmdstan /tmp/jl_auygYa/bernoulli`: no such file or directory (ENOENT)
Stacktrace:
  [1] _spawn_primitive(file::String, cmd::Cmd, stdio::Vector{Any})
    @ Base ./process.jl:100
  [2] #690
    @ ./process.jl:113 [inlined]
  [3] setup_stdios(f::Base.var"#690#691"{Cmd}, stdios::Vector{Any})
    @ Base ./process.jl:197
  [4] _spawn
    @ ./process.jl:112 [inlined]
  [5] _spawn(::Base.CmdRedirect, ::Vector{Any})
    @ Base ./process.jl:140
  [6] _spawn
    @ ./process.jl:107 [inlined]
  [7] success(cmd::Base.CmdRedirect)
    @ Base ./process.jl:502
  [8] (::StanSample.var"#25#26"{IOBuffer, String, String})()
    @ StanSample ~/.julia/packages/StanSample/Ka9pQ/src/stanmodel/SampleModel.jl:102
  [9] cd(f::StanSample.var"#25#26"{IOBuffer, String, String}, dir::String)
    @ Base.Filesystem ./file.jl:110
 [10] SampleModel(name::String, model::String, tmpdir::String)
    @ StanSample ~/.julia/packages/StanSample/Ka9pQ/src/stanmodel/SampleModel.jl:101
 [11] SampleModel(name::String, model::String)
    @ StanSample ~/.julia/packages/StanSample/Ka9pQ/src/stanmodel/SampleModel.jl:92
 [12] top-level scope
    @ REPL[3]:1

```

I am not good at understanding stack traces, and so am unsure where to go from here. I have a hunch that I have somehow misunderstood how to add cmdstan to the environment.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [January 30, 2022, 10:27am UTC](https://discourse.julialang.org/t/stansample-jl-cant-find-cmdstan/75451/2 "2022-01-30T10:27:37Z")

</div>

> [@PeetoomHeida](#):
>
> `ENV["CMDSTAN"]="/home/isaac/.conda/envs/stan-env/bin/cmdstan"`

I often have trouble setting up the path in startup.jl and .bashrc. Lately, I have been adding the path directly to my script. Do you get the same error when you add `ENV["CMDSTAN"]="/home/isaac/.conda/envs/stan-env/bin/cmdstan" ` to your script?

---

<div class="post-metadata">

**Author:** ![PeetoomHeida](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peetoomheida/32/23865_2.png) [@PeetoomHeida](https://discourse.julialang.org/u/PeetoomHeida)\
**Post date:** [January 30, 2022, 5:31pm UTC](https://discourse.julialang.org/t/stansample-jl-cant-find-cmdstan/75451/3 "2022-01-30T17:31:31Z")

</div>

After adding `ENV["CMDSTAN"]="/home/isaac/.conda/envs/stan-env/bin/cmdstan"` as the first line of the script I still get the same error.

These are the contents of the path above. Maybe I am pointing to the wrong directory somehow?

```julia
> ls -al /home/isaac/.conda/envs/stan-env/bin/cmdstan
total 72
drwxr-sr-x 1 isaac isaac 404 Jan 29 18:44 .
drwxr-sr-x 1 isaac isaac 3996 Jan 29 18:44 ..
drwxr-sr-x 1 isaac isaac 138 Jan 29 18:44 bin
-rwxrwxr-x 1 isaac isaac 3090 Jan 29 18:44 build_env_setup.sh
-rwxrwxr-x 2 isaac isaac 2932 Nov 23 06:23 .clang-format
-rwxrwxr-x 2 isaac isaac 1429 Nov 23 06:23 conda_build.sh
drwxr-sr-x 1 isaac isaac 18 Jan 29 18:44 examples
drwxr-sr-x 1 isaac isaac 152 Jan 29 18:44 .github
-rwxrwxr-x 2 isaac isaac 499 Nov 23 06:23 .gitignore
-rwxrwxr-x 2 isaac isaac 79 Nov 23 06:23 .gitmodules
-rwxrwxr-x 2 isaac isaac 377 Nov 23 06:23 install-tbb.bat
-rwxrwxr-x 2 isaac isaac 10808 Nov 23 06:23 Jenkinsfile
drwxr-sr-x 1 isaac isaac 52 Jan 29 18:44 lib
-rwxrwxr-x 2 isaac isaac 1480 Nov 23 06:23 LICENSE
drwxr-sr-x 1 isaac isaac 84 Jan 29 18:44 make
-rwxrwxr-x 2 isaac isaac 12017 Nov 23 06:23 makefile
-rwxrwxr-x 2 isaac isaac 1505 Nov 23 06:23 metadata_conda_debug.yaml
-rwxrwxr-x 2 isaac isaac 3199 Nov 23 06:23 README.md
-rwxrwxr-x 1 isaac isaac 6397 Jan 29 18:44 runCmdStanTests.py
drwxr-sr-x 1 isaac isaac 14 Jan 29 18:44 src
drwxr-sr-x 1 isaac isaac 250 Jan 29 18:44 stan
-rwxrwxr-x 2 isaac isaac 580 Nov 23 06:23 test-all.sh
```

---

<div class="post-metadata">

**Author:** ![PeetoomHeida](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peetoomheida/32/23865_2.png) [@PeetoomHeida](https://discourse.julialang.org/u/PeetoomHeida)\
**Post date:** [January 30, 2022, 7:32pm UTC](https://discourse.julialang.org/t/stansample-jl-cant-find-cmdstan/75451/4 "2022-01-30T19:32:01Z")

</div>

So I think I have figured out the short term solution, though I am unsure how it will affect working directories and Julia environments.

I run

```julia
> conda activate stan-env
> julia
julia> using StanSample, DataFrames
julia> model = "
data { 
 int<lower=0> N; 
 int<lower=0,upper=1> y[N];
} 
parameters {
 real<lower=0,upper=1> theta;
} 
model {
 theta ~ beta(1,1);
   y ~ bernoulli(theta);
}
";
julia> sm = SampleModel("bernoulli", model);
julia> data = Dict("N" => 10, "y" => [0, 1, 0, 1, 0, 0, 0, 0, 0, 1]);
julia> rc = stan_sample(sm; data, num_threads=1);
julia> if success(rc)
 df = read_samples(sm, :dataframe);
 df |> display
end

```

I don’t run into any errors this way.

---

<div class="post-metadata">

**Author:** ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)\
**Post date:** [January 30, 2022, 7:54pm UTC](https://discourse.julialang.org/t/stansample-jl-cant-find-cmdstan/75451/5 "2022-01-30T19:54:11Z")

</div>

Out of curiosity: why not Turing? It saves having to deal with a C++, R, Conda stack and instead works with just Julia (at least on Julia 1.6. 1.7 not yet)

---

<div class="post-metadata">

**Author:** ![PeetoomHeida](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peetoomheida/32/23865_2.png) [@PeetoomHeida](https://discourse.julialang.org/u/PeetoomHeida)\
**Post date:** [January 30, 2022, 11:35pm UTC](https://discourse.julialang.org/t/stansample-jl-cant-find-cmdstan/75451/6 "2022-01-30T23:35:23Z")

</div>

A couple of reasons. 1) I had never heard of Turing before. 2) This is for a class, where the instruction is in R using RstanARM. Stan.jl seemed to be the closest, though it looks like Turing will be good for what I need. It certainly seems more straightforward to work with.

---

<div class="post-metadata">

**Author:** ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)\
**Post date:** [January 30, 2022, 11:57pm UTC](https://discourse.julialang.org/t/stansample-jl-cant-find-cmdstan/75451/7 "2022-01-30T23:57:16Z")

</div>

Turing should be fine for most things yes, but depending on your class can take some time to get used to. My 2cents are to use Pluto and ask here on Discourse with a turing tag if problems come up. Good luck with the assignment
