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.
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:
and on Ubuntu, you can install it using apt.
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
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.
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:
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
To install a new OCaml compiler, run the
switch command with the
After installation, you can switch back and forth between compilers using
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 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
Map among others.
Core supplements Base with additional functionality and new data structures.
This includes making types comparable, and addint data structures like
Queue to Base.
Unless you have a prior need for a minimal installation, it is safe to install
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:
You can now run UTop with the
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
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.
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.