VSCode, conda environments, and Stan.jl

I have spent the last couple of days trying to get Stan.jl to run with VSCode. Stan.jl requires cmdstan, which (on my platform, Windows) is most easily installed via conda. However, the conda environments seem to be giving me trouble.

I see two ways I could install cmdstan via conda:

  1. using Conda.jl
  2. using a separate-from-Julia conda installation

When I try (1), cmdstan appears to install just fine (I see all the proper binaries), but Stan programs won’t compile (I get an error that mingw32-make" cannot be found, although the CMDSTAN environment variable appears to point to the correct directory).

When I try (2), I can run Stan programs, but only when I start julia within the proper conda environment – and I have no idea about how to tell VSCode to start julia in a particular conda environment easily.

Is anyone using Stan and VSCode on Windows? Any tips on getting this sort of setup working?

I’m not sure VSCode is part of the equation, but you are right. Installing Stan on Windows is needlessly difficult. What I found to work is to set the CMDSTAN_HOME environment variable at the top of my script:

ENV[“CMDSTAN_HOME”] = “path_to_cmdstan”

On my system this was located in “Anaconda3/envs/stan-evn/Library/bin/cmdstan”

Thanks. That’s on Windows?

My problem appears to be the tooling (mingw32, etc.). Conda installs everything properly, but unless I am running from the Anaconda/miniconda3 prompt, the StanSample call can’t “see” mingw32-make, etc. And I’m not sure how to run julia using the miniconda3 prompt from within VSCode (or if that’s even necessary).

Do you have the required toolchain (g++, etc.) set up separately from conda (e.g., from RTools or something)? How are you running your julia code?

I mostly use Linux, but when I had to install Stan on a Windows machine, I used conda: 1 CmdStan Installation | CmdStan User’s Guide

I installed StanSample.jl and added ENV["CMDSTAN_HOME"] = “path_to_cmdstan” to the top of my script. A working example of a script can be found here: https://github.com/StanJulia/StanSample.jl/blob/master/example/bernoulli.jl

That seemed to be the easiest approach. RTools didn’t work for me.

Well, after futzing with it for waaaaay too long, I got a kludge-y sort of method working.

But mostly for my own notes, when I invariably try this again in a year or so, here’s what I’ve tried, and what worked/didn’t work:

  1. Install cmdstan via Conda.jl. cmdstan installs, but julia (for example, the julia terminal in VSCode) can’t “see” the required tooling (e.g., mingw32-make, g++). The tooling is there, installed within the conda environment – it’s just not on the path, so StanSample doesn’t know how to call it. At least, I assume that’s what’s happening.

  2. Install via R, C++ tooling via RTools. This is the way I always used to do it, and is usually quite reliable. This time failed for some reason. RTools installed, the proper directories were added to the path, cmdstan will attempt to compile a stan program, but errors out, mentioning something about missing libraries. But I do want to be able to run from R eventually, so I will have to revisit later on.

  3. Install cmdstan via conda. Works, but requires that I: (a) start a miniconda prompt, (b) navigate to my Julia project directory, (c) activate the proper conda environment, (d) launch Julia, (e) launch VSCode, (f) tell the Julia extension to use the external REPL (which requires searching for the command, copy-pasting a sample command to link the external terminal to VSCode), and (g) use VSCode normally, with Stan code going to the external terminal.

Once the session is set up, (3) works fine. I especially like being able to just alt-tab back and forth between the editor and the terminal; it works just like Tinn-R used to!! Which I love, but it’s very very annoying to set it up every time.

This was a lot easier under linux, but I have to use Windows for work, and it’d be best if I can figure out how to get it working there (and I always used to run it under Windows, anyway). But this is honestly a bit excruciating. :frowning:

That’s strange that you had to do all of that, but I’m glad it eventually worked. Did you not have success setting CMDSTAN_HOME in your script?

I certainly tried to, from the very beginning – I don’t know if I used the correct directory, but it seemed quite straightforward.

On my system, the cmdstan directory installed in my specific conda environment is located at C:\Users\brats\miniconda3\envs\stan-env\Library\bin\cmdstan. The required C++ tooling is installed by conda at C:\Users\brats\miniconda3\envs\stan-env\Library\mingw-w64\bin.

Per the various instructions out there, I’ve tried setting both ENV[“CMDSTAN”] and ENV[“CMDSTAN_HOME”] to the …\bin\cmdstan directory mentioned above (I see both ENV variables mentioned in different places, but it’s not clear which is the “correct” one).

I’ve tried setting them in startup.jl, or in the script itself, but I only get this error when trying to execute a script:

IOError: could not spawn mingw32-make -f 'C:\Users\brats\miniconda3\envs\stan-env\Library\bin\cmdstan/makefile' -C 'C:\Users\brats\miniconda3\envs\stan-env\Library\bin\cmdstan' 'c:\Users\brats\OneDrive\Julia\Stan\tmp\bernoulli.exe': no such file or directory (ENOENT)

Basically, it can’t find mingw32-make or g++, unless I am running things through the miniconda prompt. Which I think is expected, but makes it difficult to use julia from VSCode’s built-in terminal.