TExcelTAD
Home › OCaml › OCaml Get Started

OCaml Get Started

You can run every example of this course in the browser, but sooner or later you will want OCaml on your own machine. This lesson explains the standard tools: opam (the package manager), dune (the build system) and utop (the interactive toplevel).

Installing with opam

opam installs OCaml compilers and libraries. It keeps them in switches: independent environments, each with its own compiler version.

bash
# macOS (Homebrew) or Linux (your distribution's package manager)
brew install opam              # or: sudo apt install opam

opam init                      # one-time setup, creates the default switch
eval $(opam env)               # make the switch's tools visible in this shell
opam install dune utop ocaml-lsp-server
ocaml -version

On Windows, the recommended route is the OCaml installer from ocaml.org or using WSL2 with the Linux instructions.

Tool Purpose
ocaml the basic toplevel (REPL)
utop a friendlier toplevel with history and completion
ocamlc compiler to portable bytecode
ocamlopt compiler to native machine code
dune build system for projects
opam package and compiler manager
ocaml-lsp-server editor support (VS Code "OCaml Platform" extension, Vim, Emacs)

The toplevel: utop

Start utop and type expressions. Each phrase ends with ;;, and the toplevel answers with the value's name, type and value:

ocaml
# 40 + 2;;
- : int = 42
# "Hello" ^ " world";;
- : string = "Hello world"
# let square x = x * x;;
val square : int -> int = <fun>
# square 12;;
- : int = 144
# #quit;;

- means the value has no name. <fun> means the value is a function. The toplevel is the best place to ask "what type does this have?".

Your first compiled program

Save this as hello.ml:

ocaml · try it yourself
let () = print_endline "Hello from a compiled program!"
Hello from a compiled program!

Compile and run it from a terminal:

bash
ocamlfind ocamlopt -package str hello.ml -o hello   # with libraries (optional)
ocamlopt hello.ml -o hello                          # just the standard library
./hello

In a compiled file, ;; is not needed. The file is a list of top-level definitions, and let () = ... runs code for its side effects. The pattern () checks that the expression has type unit, the type of "no interesting value", so you cannot accidentally drop a real result.

A dune project

For anything bigger than one file, use dune. dune init project creates a skeleton:

bash
dune init project shop
cd shop
dune build
dune exec shop
text
shop/
├── dune-project      project-wide settings
├── bin/
│   ├── dune          (executable (public_name shop) (name main) (libraries shop))
│   └── main.ml
├── lib/
│   └── dune          (library (name shop))
└── test/
    └── dune          (test (name test_shop))

Each directory has a small dune file describing what to build there. You will see more in the "Dune Projects and Testing" lesson.

Printing output

Throughout the course we print with a few standard functions:

ocaml · try it yourself
let () =
  print_endline "a line";
  print_string "no newline, ";
  print_string "then a newline\n";
  print_int 42;
  print_newline ();
  Printf.printf "%s is %d years old\n" "Ada" 36
a line
no newline, then a newline
42
Ada is 36 years old

Printf.printf takes a format string with placeholders like %s (string) and %d (int). The compiler checks that the arguments match the placeholders, so Printf.printf "%d" "oops" is a compile error, not a crash.

Tip: Install the OCaml Platform extension in VS Code. Hovering over any name shows its inferred type, which is a huge help while learning.

Test yourself

Which tool manages OCaml compiler versions and libraries?

  1. dune
  2. utop
  3. opam
  4. ocamlopt

In the toplevel, what does val square : int -> int = <fun> tell you?

  1. square is an int
  2. square is a function from int to int
  3. square failed to compile
  4. square returns a string

Why do compiled programs often use let () = ...?

  1. It declares a global variable
  2. It runs code whose result must be unit
  3. It is required to import modules
  4. It defines the main class