Skip to content

Lesson 7 of 9 · 23m

opam, Dune, and Native Builds That Stay Reproducible

Set up OCaml tooling correctly, read Dune files, pin dependencies, and understand what the native compiler is actually producing.

Most frustration attributed to OCaml is actually toolchain friction: the wrong switch, a missing system dependency, an unpinned library, or a Dune stanza copied without understanding it. The good news is that modern OCaml tooling is coherent once you learn the vocabulary. opam manages compiler versions and packages; Dune builds projects; the compiler produces native or bytecode artifacts.

opam is environment management, not just package install

Create a switch for a project instead of polluting one global compiler installation:

opam update
opam switch create ocamlapp 5.2.0
eval $(opam env --switch=ocamlapp)
opam install dune ocaml-lsp-server ocamlformat

A switch is a compiler plus package universe. If a teammate is on a different compiler, you are not yet reproducible. Record the intended version in documentation or lock files rather than relying on 'whatever opam installed last year.'

A minimal Dune project

A small tree has bin/main.ml, lib/ledger.ml, lib/ledger.mli, and dune-project. The project file contains (lang dune 3.16). Executable and library stanzas then declare the build units:

(executable
 (name main)
 (public_name ledgerctl)
 (libraries ledger))

(library
 (name ledger)
 (modules ledger))

Dune derives a lot from file layout, but do not mistake concise configuration for simplicity. name, public_name, modules, and library dependency resolution each have precise semantics.

Build, run, and test like a practitioner

dune build
dune exec ./bin/main.exe
dune runtest
dune build @fmt --auto-promote

.exe naming can confuse people on Linux. Dune uses it as a conventional executable artifact name even when the resulting binary is a native Linux executable. Do not 'fix' that unless you understand how references throughout the build change.

Lock files matter

Generate and commit dependency lock information when supported by your workflow:

opam switch export opam-switch.export

For application deployment, a lock file is often more useful than a loose *.opam constraint set. Constraints say what may solve; locks say what actually built. Reproducibility is about the second thing.

Native compilation has real operational consequences

ocamlopt produces native binaries through architecture-specific backends. Static or mostly static deployment may be possible depending on libraries and C dependencies, but assumptions vary. System packages such as libev, OpenSSL headers, or pkg-config are often required before opam installation succeeds. Read the first missing external dependency carefully instead of retrying blindly.

Field notes

  • Always run eval $(opam env) in a shell where builds mysteriously use the wrong compiler or packages.
  • _build is generated output. Do not edit it, commit it, or debug there first.
  • dune clean is safe and often the fastest way to eliminate stale artifact confusion.
  • Editor integration depends on the correct switch. If the language server reports nonsense, check the environment before blaming the code.

Was this lesson helpful?

Sign in to rate