Honing my craft

Crafting Interpreters in OCaml - Setting up your environment

May 15, 2019

If you want to just clone a repo and move on, check out this sample

The easiest way to get an environment set up is to just follow the Real World OCaml guide. I’m going to enumerate the steps here in less detail. Since I’m running on macOS, this guide will be tailored to that. Check out the RWO guide for more options.

  1. Get OPAM installed. Opam is the OCaml package manager and is available on Homebrew. If you have Homebrew installed, it’s as easy as brew install opam

  2. This should have installed OCaml for you. To check, run ocaml -version and make sure you have a response.

  3. Next up, we need to configure OPAM. Run opam init and walk through the questions. I answered yes to everything.

  4. Now we need some packages. We’re going to start by installing just Core and utop. Core is a replacement to the OCaml standard library. It has some different APIs for labeled arguments, more functionality in every module, and has a consistent interface for Container structures. To be honest, we could just install Base instead of Core, where Base supplies those features, but RWO uses Core so let’s stick with it. utop is a toplevel (like the Ruby REPL irb) that is really easy to work with for testing code on the fly. For the purpose of this tutorial, I’m actually not going to use Base or Core so you can omit it.

  5. Let’s also install dune for our build tooling, with opam install dune.

  6. open up ~/.ocamlinit and add the following to get all of Core set up in the toplevel

#use "topfind";;
  1. Configure your editor. I use Visual Studio Code and really like the ReasonML tool Reason Language Server

And that’s a minimal install! If you check out the repo I linked above, you can run tests with dune runtest and play with some code.

Nikhil Thomas

I work and live in Brooklyn, NY building software.