Bootstrapping a Project with Dune

Dune is recommended for bootstrapping projects using dune init. If opam or dune are not installed, please see Up and Running with OCaml.

dune init --help

dune init {library,executable,test,project} NAME [PATH] initialize a new dune component of the specified kind, named NAME, with fields determined by the supplied options.

As shown above, dune init accepts a kind, NAME, and optional PATH to scaffold new code. Let's try it out:

dune init project hello ~/src/ocaml-projects

Success: initialized project component named hello

In the above example, we use:

  • "project" as the kind
  • "hello" as the name, and
  • "~/src/ocaml-projects" as the path to generate the content in

The project kind creates a library in ./lib, an executable in ./bin, and links them together in bin/dune. Additionally, the command creates a test executable and an opam file.

tree ~/src/ocaml-projects/hello/

├── bin
│   ├── dune
│   └──
├── hello.opam
├── lib
│   └── dune
└── test
    ├── dune

At this point, you can build the project and run the binary:

cd /home/user/src/ocaml-projects/hello/
dune exec bin/main.exe

Hello, world!

Thus, dune init can rapidly scaffold new projects, with minimal content. It can also be used to add components (kinds) incrementally to existing projects.

Various community projects offer more comprehensive project scaffolding than dune as well. The following projects are not formally supported by the OCaml Platform, but may be of interest to the reader:

Help Improve Our Documentation

All OCaml docs are open source. See something that's wrong or unclear? Submit a pull request.