Tarides Logo
Windows logo background, Ubuntu and OCaml logos overlap.

How to Setup OCaml on Windows with WSL

Pizie Dust

Software Engineer

Posted on Wed, 08 May 2024

The stable opam 2.2 and a fully Windows compatible ecosystem of OCaml libraries and tools are getting closer every month. That's extremely exciting! With opam 2.2, Windows users will be able to use OCaml directly and natively without extra set-up or workarounds. Everyone is excited about this future, so we often forget people who want to use OCaml on Windows now and without set-up problems.

In this guide, we demonstrate how to set up OCaml on Windows using WSL2. With WSL2, you can write OCaml programs on your Windows computer while utilising all the benefits of working in a Linux environment.

Note that as an alternative to WSL2, it's already possible to install OCaml natively on Windows. If you wish to install OCaml directly on your PC, follow this guide on how to install OCaml natively on Windows using Diskuv or you can use opam 2.2. We recommend native Windows in the following circumstances:

  • For people who want to have Windows commands available (e.g., dir) instead of Unix commands (e.g., ls). Although it's a tougher set-up process.
  • For people who want to build OCaml native Windows binaries, e.g., to distribute OCaml apps on Windows
  • For people who are really into Windows from a perspective of technical curiosity
  • For advanced programmers who are happy to try native Windows support from the beginning

For anyone else, like people who just want to try OCaml on their Windows machine, OCaml on Windows via WSL2 is a great solution, so we'll dive right in.

Prerequisite

Procedure

Update Packages

  • Open your Ubuntu terminal (e.g., by searching "Ubuntu" in the Start menu).
  • Run the following command to update the package list and upgrade all packages:
$ sudo apt update && sudo apt upgrade

Install Required Packages

  • Run the following command to install packages required to succesfully install OCaml:
$ sudo apt install gcc build-essential curl unzip bubblewrap

Download and Install opam

Opam is OCaml's package manager. It makes it easier to install additional libraries and tools relevant to various OCaml projects. It is similar to package managers like pip for Python or npm for JavaScript. To download and install opam, run the following command:

$ bash -c "sh <(curl -fsSL https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh)"

The error below (or similar) is due to DNS issues: If you don't experience this error, you can skip directly to the Initialise opam section

curl: (6) Could not resolve host: raw.githubusercontent

To resolve:

  • Use the nano editor to edit the resolv.conf file by running:
$ sudo nano /etc/resolv.conf
  • Change the address 127.0.0.1 to 8.8.8.8. The final file should look like this:
nameserver 8.8.8.8
  • Now, we will use the nano editor to edit the wsl.conf file by running:
$ sudo nano /etc/wsl.conf
  • Add this entry to the file:
generateResolvConf = false
  • The final file should look like this:
[boot]
systemd=true
generateResolvConf = false
  • At this point, we can re-run our script to install opam:
$ bash -c "sh <(curl -fsSL https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh)"

Our script should now run without any issues. If you are prompted for questions, such as where to install it, just press enter to accept the default locations and values.

Initialise opam

Now that we have installed opam, the next step is to initalise it. This step creates a new opam switch, which acts as an isolated environment for your OCaml development. We can do this by running the command:

$ opam init

When prompted, press y to modify the profile file so we can easily run opam commands. If you didn't press y, we can activate the switch by running:

$ eval $(opam env)

Setup A Development Environment

Now that we have OCaml setup. The next thing is to setup a development environment by installing packages that make programming in OCaml a much nicer experience. We can do this by using the opam install <package-name> command. Below are the basic recommended packages and tools for OCaml development:

  • ocaml-lsp-server provides an LSP implementation for OCaml giving us a nice experience with editors and IDEs.
  • odoc is a documentation tool that generates human-readable documentation from OCaml code, including comments, types, and function signatures.
  • ocamlformat formats OCaml code according to a defined style guide, ensuring consistent formatting across different code files, which improves readability and maintainability.
  • utop provides an interactive environment for OCaml development where you can directly type in OCaml expressions and see their results immediately. It's perfect for experimenting and testing.

We can easily install all these packages at once and set them up by using the OCaml Platform Installer. Alternatively, we can manually by type the command below, then follow the installation and set-up instructions:

$ opam install ocaml-lsp-server odoc ocamlformat utop

The most supported editors include:

  • VSCode: VSCode also has a WSL version.
  • Vim
  • Emacs

Based on your preference, you can install any of the editors and begin programming in OCaml. Follow this helpful guide on how to setup your editor: Setting up your editor for OCaml development.

We have succesfully setup OCaml on our Windows PC using WSL. Now you can work on OCaml projects just like you would on a Linux computer.

Open-Source Development

Tarides champions open-source development. We create and maintain key features of the OCaml language in collaboration with the OCaml community. To learn more about how you can support our open-source work, discover our page on GitHub.

Explore Commercial Opportunities

We are always happy to discuss commercial opportunities around OCaml. We provide core services, including training, tailor-made tools, and secure solutions. Tarides can help your teams realise their vision