Install OCaml Compiler
Learn how to install the OCaml compiler and the REPL utop on your local machine as well as OCaml's compilation process.
We'll cover the following
OCaml compiler
The recommended way to install the OCaml compiler on a local machine is to use the package manager OPAM.
First, let’s use the following script to install OPAM:
$ sh <(curl -sL https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh)
The script checks the architecture of the computer and downloads and installs the suitable pre-compiled binary to /usr/local/bin
by default.
Verify that OPAM has been installed successfully by typing in the terminal:
$ which opam
/usr/local/bin/opam
Next, follow the [instruction for installing OCaml via OPAM](https://ocaml.org/docs/install.html to install the latest OCaml compiler version. At the time of this writing, the latest OCaml compiler version is 4.12.0.
$ opam init
$ eval `opam env`
$ opam switch create 4.12.0
$ eval `opam env`
After this, install the following tools:
ocamlc
: The OCaml bytecode compiler that compiles OCaml source files to bytecode files.ocamlopt
: The OCaml native compiler that compiles OCaml source files to native code object files and links them to produce standalone executables.ocamlrun
: The bytecode interpreter that executes bytecode files produced byocamlc
.
Note: Verify that the OCaml bytecode compiler has been installed successfully before continuing.
$ which ocamlc
/Users/quang/.opam/4.12.0/bin/ocamlc
REPL utop
A great way to learn functional programming with OCaml on a local machine is using the read–eval–print loop (REPL) utop
. Use the package manager OPAM
mentioned previously to install utop
.
$ opam install utop
You can start utop
by entering it in the shell.
$ utop
You should see something like the following screenshot:
Get hands-on with 1400+ tech skills courses.