Skip to main content

Get started with OCaml on macOS

Prerequisites

Installation

To install OCaml, you can use brew or Nix to install the opam package. opam is a package manager for OCaml. It will allow you to install OCaml and other packages.

brew install opam

Initialization

To initialize Opam, you can use the following command:

opam init
eval $(opam env)

Install additional packages

Many packages are required to work with OCaml. Here is a list of packages to install:

opam install dune utop merlin ocp-indent ocaml-lsp-server tuareg
  • utop: An interactive terminal for OCaml, useful for quickly testing functions.
  • merlin, ocp-indent, ocaml-lsp-server: Tools to be able to use OCaml with your favorite IDE (VSCode, Vim, Emacs, ...).
  • dune: A tool to compile your OCaml projects, necessary for the AFIT project
  • tuareg: An Emacs mode for editing OCaml code

In addition, if you like unit tests, you can install the alcotest package:

opam install alcotest

Execute OCaml code

It's easy, just run the ocaml command in your terminal, or utop if you have installed the utop package:

❯ ocaml
OCaml version 4.13.1
# let x = 42;;
val x : int = 42
# x * 2;;
- : int = 84

Configure your IDE

You can use your favorite IDE to work with OCaml. The IDE recommended for OCaml by Epita is Emacs, but you can use any IDE that supports OCaml.

Emacs

There is a big advantage of using the tuareg mode for Emacs: you can copy-paste the content of a buffer into an ocaml instance directly from Emacs.

You can configure OCaml with the following .emacs file (which is located in your home folder /Users/<your name>/.emacs on Mac). If you already have an .emacs file, you can simply add the lines at the end of your file.

.emacs
(require 'package)
(setq package-archives '(("gnu" . "https://elpa.gnu.org/packages/")
("melpa" . "https://melpa.org/packages/")))
(package-initialize)

; Suggest installing the tuareg mode if needed
(unless (require 'tuareg nil 'noerror)
(if (y-or-n-p "Il manque le mode tuareg, appuyer sur 'y' pour l'installer automatiquement")
(progn
(package-refresh-contents)
(package-install 'tuareg))))
; Suggest installing ocp-indent if needed
(unless (require 'ocp-indent nil 'noerror)
(if (y-or-n-p "Il manque ocp-indent, appuyer sur 'y' pour l'installer automatiquement")
(progn
(package-refresh-contents)
(package-install 'ocp-indent))))

When you open a .ml or .mli file, Emacs should automatically use the tuareg mode to color the code, and add a Tuareg menu in the menu bar.

The shortcuts are as follows:

  • C-c C-e : Execute the current line
  • C-c C-r : Execute the selected region
  • C-c C-k : Kill the current buffer

VSCode

We recommend using the OCaml Platform extension. It allows you to use OCaml with VSCode, and to have full support for .ml and .mli files (syntax highlighting, autocompletion, ...).

Manage multiple OCaml environments

If you need to manage multiple OCaml environments, you can use opam switch to create virtual environments.

For example, to create a virtual environment ocaml-4.13.1 with OCaml 4.13.1, you can use the following command:

opam switch create ocaml-4.13.1 4.13.1
eval $(opam env)

You can then use opam switch to switch between different virtual environments.

❯ opam switch
# switch compiler description
-> 4.13.1 ocaml-base-compiler.4.13.1 4.13.1
default ocaml.4.14.0 default
info

When changing virtual environment, you may need to reinstall the additional packages you had installed previously.

Sources