Department
Princeton University

Computer Science 510
Programming Languages
Andrew Appel

Spring 2021


Installing OPAM and OCaml

Install the OCaml package manager OPAM. OPAM is the quickest and easiest way to get all kinds of libraries for OCaml programming. To install on Linux via each distribution's package manager or on Mac via Homebrew, follow these instructions. The binary and source installations are also available at the same page, for manual installation or installation in Cygwin.

Install OCaml. Follow the instructions from OCaml.org. It is likely easiest to use OPAM, however there are also instructions available for independently installing on various flavors of Linux, Mac, and Cygwin.

Text Editors

You can develop Ocaml programs using any text editor. If you already are comfortable with Emacs, Vim, or Eclipse, proceed to the instructions regarding your editor of choice. If you are indifferent, I recommend Emacs with Tuareg mode.

(There is also a tool called Merlin, which is probably overkill for the amount of Ocaml programming you'll do in this course.)

Emacs

To install Emacs on a Mac, follow the instructions here under the heading "Homebrew". To install Emacs on Ubuntu Linux, run the following command from a terminal (for other distributions substitute their appropriate package manager):

$ sudo apt-get install emacs24

Tuareg Mode

Tuareg mode is a special mode for editing OCaml files in Emacs. You'll want to use Tuareg mode whether or not you choose to install Merlin; Tuareg mode provides syntax highlighting, indendation, etc., while Merlin helps with programming logic. Tuareg is now available via OPAM:
$ opam install tuareg

Once the install completes, there are instructions indicating the lines of code you should copy in to your .emacs file. Cut and paste those lines in to your .emacs file, adjusting the placeholder variables to be the correct path, as stated in the instructions given by OPAM.

You can also find the Tuareg software and installation instructions on the Tuareg github. The key files for manual installation are tuareg.el, tuareg_indent.el, ocaml_debug.el, and tuareg-site-file.el. Place these files in an accessible directory, and add the following line to your .emacs configuration file, substituting that directory for $DIR:

(add-to-list 'load-path "$DIR")
(load "tuareg-site-file")

Once you have installed OCaml, Emacs, and Tuareg open any file with a .ml or .mli extension. This should start up Tuareg mode and you should see OCaml key-word, comment color highlighting and other features. To start the OCaml top-level interpreter in Emacs, on Windows at least, make sure the OCaml bin directory is in your path. At this point, you should be able to type C-c C-s in Emacs and the OCaml top-level interpreter will start. Alternatively, you can start OCaml from the Tuareg pull-down menu by choosing "Interactive Mode" and then "Run Caml Toplevel."