Getting Started with OCaml and Reason

Kevin GathukuKevin Gathuku
3 min read

Recently I've been exploring OCaml, an interesting statically typed functional language that's been around since 1996. I've tried OCaml in the past and always got frustrated by the tools and just gave up. This time round, there seems to have been an effort to improve the developer experience and it shows. The tools are much easier to use, and mostly just work.

In this post, we will go through setting up an OCaml development environment on a Mac. I will also share a starter project, some of the essential packages you will need.

Installing Ocaml

On a Mac, you should be able to install ocaml with homebrew:

brew install ocaml

Real World OCaml has the best installation guide I have found so far, so follow that and you should be good.

At the end of that guide, you should have ocaml and opam installed:

ocaml --version
The OCaml toplevel, version 4.12.0opam --version
2.1.0

Opam is the default package management tool in Ocaml ecosystem.

In addition to following that guide, install dune, the ocaml build tool. We will need it later.

opam install dune

Isolated Development Environments per project with opam

Opam can create isolated OCaml environments per project. Think of this as virtualenv, but for OCaml. By default, when starting a new project, I run the following command in the project folder to create a brand new OCaml environment:

opam switch create . ocaml-base-compiler.4.12.0

This will create an isolated OCaml environment and with OCaml version 4.12.0 installed.

To activate the newly created environment in the current shell, run the following command:

eval $(opam env)

To automate this process, I use direnv to activate the isolated OCaml environment in my shell every time I navigate to the folder. This is done through adding the command above to a .envrc file in the project folder.

Editor setup: VS-Code

For OCaml development on Visual Studio Code, the essential package you need is VSCode OCaml Platform

To make full use of the extension, I've found that I've needed to install the ocamlformat and ocaml-lsp-server packages individually in each of the specific project I'm developing.

Starter Project Template - dune package manager

To ease the process of getting started on a new project, please take a look at this project template that I use whenever I need to start a new project.

Reason Support

In the starter project template, you should find a Reason file in the lib/ directory. Dune supports Reason files by default, so you don't need to do anything extra to write your code in the Reason variant. You can also mix and match Reason and Ocaml files in the project and it should work just fine.

I've found that to enable formatting Reason files in Visual Studio Code, I also have to install the refmt opam package locally in the project-specific environment.

That's all for now.

Enjoy the rest of your day!

0
Subscribe to my newsletter

Read articles from Kevin Gathuku directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kevin Gathuku
Kevin Gathuku

I'm a web-focused developer with a passion for exploring new ideas and hopefully sharing more of them through this blog 😃