The Margin Calculated Economics

OCaml on Windows: The easy way

There have been a number of ways to run OCaml on Windows in the past, and there is an extensive list here. However, they all have downsides, whether that involves depending on someone else to update installer binaries, or having to deal with installing and managing Cygwin just to run OCaml.

Fortunately, with the release of Windows Subsystem for Linux (WSL), it’s possible to use the standard Ubuntu OCaml/OPAM easily from within windows, and integrate it with a windows GUI code editor. This guide assumes that you already have WSL set up - if not come back after setting it up by following this guide.

Given how fundamental modules such as Core by Jane Street are to doing any real work with OCaml we’re going to install the OPAM package manager and Core along with just the compiler. The end result is exactly the set up you need to begin the excellent Real World OCaml guide.

Firstly, we need to download and install ocaml and opam we need to add a personal package archive (ppa) to apt-get because the official Ubuntu repos have occasional issues. The code below adds this private repo to apt-get.

sudo add-apt-repository ppa:avsm/ppa
sudo apt-get update

Then just use apt-get to install ocaml, opam and m4 (a macro processor also required for the setup to work properly)

sudo apt-get install ocaml opam m4

The next step is to set up opam. Start with opam init, which will ask you to give it permission to update your .ocamlinit and .profile files. You can simply answer yes to both to put the changes through.

This lets you run the basic REPL loop with the ocaml command. However, a couple of extra bits and pieces are needed to get to the start of the Real World OCaml guide. We install these extras through opam with the command:

opam install core utop

Normally we’d now add the environmental variables to run the environment with the command:

eval 'opam config env'

Which should add the variables you need as explained here and here. However, when I tried it on WSL it didn’t work, I think as a side effect of the difference between the way windows and linux handles new lines. You can see this for yourself by running the command and then printenv to see all of the environmental variables - none of the variables have been set.

Instead if we use this command:

eval "$(opam config env)"

Then all of the commands are passed to ‘eval’ as a block, and so the whole thing evaluates cleanly, and if you run printenv you should see all of the required environmental variables.

The only thing that remains is to set up OCaml with the custom utop by editing the .ocamlinit file, which will be in your home directory.

Side Note: If you’re new to bash you might be confused that it doesn’t appear if you run ls in your home directory. This is because of the . in front of the filename which hides it by default. If you run ls -a you will be able to see it.

Open this file with your favourite text editor, e.g. nano .ocamlint, and then add these lines. This is some OCaml code, which will execute on initialisation, i.e. every time you start up the OCaml environment.

#use "topfind";;
#require "";;
#require "core.syntax";;
#open Core.Std;;

Everything is now doing what it’s supposed to do - if you run ocaml you will get utop with the Core module loaded and be ready to start working through the excellent Real World OCaml guide.

However, there is one last wrinkle. Setting environmental variables this way is temporary - if you exit WSL and then restart it with bash and then rerun printenv you will see that you have lost the environmental variables, and the ocaml command will now fail - unable to find the path for topfind because the environmental variables have gone.

To fix this we can add the command eval "$(opam config env)" to the bottom of your .bashrc file in your home folder, which is run by bash every time you log in. This ensures that it runs automatically, so that you can just login and run ocaml to have a fully functional ocaml environment set up. If you switch compiler through the opam switch command, you will need to either re-run the command or restart the shell with exit and then bash to push the changes to the environment variables and update the version used by OCaml.

You’re now ready to start Real World OCaml - enjoy! This gives you the REPL loop under bash, which is good for learning. However, for more serious programs you need to write an .ml file - the next part (which you can find here) covers building using the bash OCaml environment from a GUI code editor in windows, such as Visual Studio Code.