Building an OCaml Project with Dependencies in a Conda Environment Using Dune
This workflow shows how to create and build an OCaml project with dependencies inside a Conda environment using Dune.
Key Benefits
- No Opam needed:
- Dune handles dependency resolution and build steps.
- No use of a global
~/.opam. - All dependencies and builds stay inside your project directory.
- Portable: Easy to reproduce and use in shell scripts, CI, or Docker.
Make sure you have a Conda environment with OCaml and Dune. To build OCaml and Dune in a Conda environment, follow the instructions here.
Steps
Create a New OCaml Project
Use dune to initialize your project:
1 | |
This sets up:
1 | |
Write Your Code
Executable Code (bin/main.ml)
1 | |
Edit your bin/dune file to include used libraries:
1 | |
Declare Project Dependencies
Edit dune-project:
1 | |
Lock Dependencies
Run:
1 | |
- This will generate a lock directory.
- Dune will resolve, fetch, and build compatible versions for your declared dependencies.
Build the Project
1 | |
This will download, build, and install all your locked dependencies and then use those to build your project. This means that the first time building it will take longer than usual, as the dependencies need to be built first. Subsequent builds where all dependencies have been built before will be fast.
Run the Executable
1 | |
or
1 | |
You should see:
1 | |
Building an OCaml Project with Dependencies in a Conda Environment Using Dune
https://jifengwu2k.github.io/2026/01/17/Building-an-OCaml-Project-with-Dependencies-in-a-Conda-Environment-Using-Dune/