opam — the OCaml Package Manager — is the tool for installing OCaml and any OCaml tools and libraries. This blog post shows us how to set-up opam to install the OCaml compiler and development tools to support your workflow.

Working with Windows

The OCaml tools are typically well supported on Linux, with macOS ports available of most binaries. If you are on a Windows, the best supported environment is the Windows Subsystem for Linux.

Installing opam

opam is available on most Linux and Unix package managers, with a full list available in the opam documentation.

For example, on macOS you can install opam using Homebrew:

$ brew install opam

and on Ubuntu, you can install it using apt.

$ sudo apt-get install curl opam

Setting up your opam installation

After installation, opam creates a .opam directory in $HOME to house the complete OCaml package database of installed tools, including your compiler versions. To initialize this database, run the init command and answer yes to the prompts.

During initialization, pay particular attention to the prompt about setting up your shell. By default, opam will add the opam tools to your PATH using .bash_profile (for bash users). If you use an alternative shell or different bash configuration files, you will need to choose the appropriate file for your needs.

$ opam init

Once complete, the init command will have created the required package database. You can then verify that opam has been added to your path by checking for the OCAML_TOPLEVEL_PATH environment variable. In bash, use the following command after restarting your terminal:

$ printenv OCAML_TOPLEVEL_PATH
/Users/kevinsookocheff/.opam/default/lib/toplevel

Using multiple OCaml compiler versions

Opam allows you to have multiple compiler versions installed, each with their own set of packages. This allows you to set up different OCaml versions for different projects. The switch command allows you to switch the compiler you are currently using. Calling switch with no arguments will list all installed compilers available.

$ opam switch
#   switch   compiler                    description
->  default  ocaml-base-compiler.4.10.0  default

To install a new OCaml compiler, run the switch command with the create sub-command:

$ opam switch create 4.05.0

After installation, you can switch back and forth between compilers using opam switch.

Installing standard libraries

Jane Street has been using OCaml in production since 1995, and during that time they’ve developed key libraries and tools that have been adopted as de-facto standards by the OCaml community.

Base

Base adds additional functionality to OCaml’s standard library. It acts as a replacement for core modules in OCaml and extends those core libraries with modern functionality. Base includes replacements for core primitives such as Array, Char, and Map among others.

Core

Core supplements Base with additional functionality and new data structures. This includes making types comparable, and addint data structures like Stack and Queue to Base.

Unless you have a prior need for a minimal installation, it is safe to install Core using opam.

$ opam install core

utop

UTop stand for Universal Toplevel. UTop provides an interactive interface to OCaml’s toplevel module that improves upon the standard OCaml read-eval-print-loop by providing tab-completion of function and argument names, syntax highlighting, and editor integration through Emacs. Like other OCaml libraries, UTop is installed with opam:

$ opam install utop

You can now run UTop with the utop command:

$ utop

This displays an interactive prompt with a list of available completions at the bottom. Trying typing 1+1;; to see how UTop evaluates code and use CTRL+D to exit. We can initialize UTop throug the .ocamlinit file. Since we have Core installed to take advantage of some of the features developed by Jane Street, we can instruct UTop to load Core every time we start UTop. This syntax is a bit arcane if you don’t know OCaml, but will make more sense as you use the language more. The first two lines import the Core modules, and the last line opens the Core module to load it into the current execution environment.

#require "core.top";;
#require "core.syntax";;

open Core

By following this post, we’ve installed a working OCaml platform including an OCaml compiler and package manager, some key OCaml modules, and an interactive REPL. This should be everything you need to start dipping your toes into the OCaml ecosystem.