Setup for using Stan with Julia

I’m busy preparing a poster about Stan.jl for JuliaCon 2018. Getting set up is pretty simple, although there are some minor details that I thought I’d document.

Julia and Stan logos.

Installing CmdStan

The Stan.jl uses the CmdStan command line client for interacting with Stan. This is what I did to install CmdStan on my Ubuntu 18.04 system.

  1. Download the source distribution.
    wget -P /tmp/ https://github.com/stan-dev/cmdstan/releases/download/v2.17.1/cmdstan-2.17.1.tar.gz
    
  2. Unpack it under /opt/.
    cd /opt
    sudo tar -zxvf /tmp/cmdstan-2.17.1.tar.gz
    cd cmdstan-2.17.1
    
  3. Build.
    sudo make build
    

Since the distribution was unpacked under /opt/ it was read only for non-root users. As a result I had a minor issue actually running my first Stan model because in the process Stan tried to create the precompiled header model_header.hpp.gch under /opt/ which, of course, resulted in a permission denied error. Quick fix for this: apply the changes from this pull request and rebuild. Sorted!

Upon success you can clean up the download.

rm /tmp/cmdstan-2.17.1.tar.gz

Julia Initialisation

You can tell Julia where to find stanc in a couple of ways. You can either set an environment variable (this should ideally be appended to the end of ~/.bashrc so that it is applied to each session).

export CMDSTAN_HOME=/opt/cmdstan-2.17.1/

Alternatively, if you don’t want to clutter up your shell namespace you can define it in your personal Julia initialisation file, ~/.juliarc.jl:

const CmdStanDir = "/opt/cmdstan-2.17.1/";

I prefer the second approach.

Install the Package

Now fire up Julia then install and load the Stan.jl package.

Pkg.add("Stan")
using Stan

Find links to pertinent documentation in the package repository.