Dune

A Dune project is a directory (and its subdirectories) that contain OCaml code you want to compile. The root of a project is the highest directory in its hierarchy. A project might rely on external packages providing additional code that is already compiled. Usually, packages are installed with OPAM, the OCaml Package Manager.

Each directory in your project can contain a file named dune. That file describes to Dune how you want the code in that directory (and subdirectories) to be compiled. (Clarkson 2025, 30)

Basics

Creating a Dune Project Manually

Assuming that you have a hello.ml file in a certain directory:

  1. In this same directory, create a file named dune with the following content:
 (executable
   (name hello))
  1. Also create a file named dune-project with the following content:
  (lang dune 3.4)
  1. To build and run the project:
  dune build hello.exe
  # now you can run the executable with either this
  _build/default/hello.exe
  # or
  dune exec ./hello.exe

Creating a Dune Project Automatically

  dune init project example
  cd example

Running Dune Continuously

  dune build --watch

References:

Clarkson, Michael Ryan. 2025. Ocaml Programming: Correct + Efficient + Beautiful. https://cs3110.github.io/textbook/.

Backlinks: