This content originally appeared on DEV Community and was authored by david2am
Tired of the OCaml setup rabbit hole?
In 5 minutes, you’ll have a fully working OCaml project.
Just copy, paste, run.
Index
-
Global Setup
- Install OCaml
- Install Platform Tools
-
Create a New Project
- Create a new project
- Create a Switch
- Configure Git
- Run the Project
- Next Steps
- References
🟣 Global Setup
Install OCaml
Start by installing Opam, the OCaml package manager, which is similar to npm in JavaScript. It manages packages and compiler versions.
For macOS
brew install opam
For Linux
sudo apt-get install opam
Initialize OCaml's global configuration:
opam init -y
Load Opam's Environment:
eval $(opam env)
Consider adding
eval $(opam env)command to your.bashrcor.zshrcfile to automate this process.
Install Platform Tools
Install tools to assist you:
opam install ocaml-lsp-server odoc ocamlformat utop
-
ocaml-lsp-server: editor integrations (VS Code, Neovim, etc.) -
odoc: documentation generator -
ocamlformat: automatic code formatter -
utop: improved OCaml REPL
🟣 Create a New Project
Dune is OCaml's default build system, it helps you create and manage projects.
Create a new project
Dune is already installed by the platform tools so just run:
dune init proj my_project
cd my_project
Your project structure will look like this:
my_project/
├── lib/
│ └── dune
├── bin/
│ ├── main.ml
│ └── dune
├── test/
│ └── test_my_project.ml
├── dune-project
└── my_project.opam
-
lib/: contains your modules (.mlfiles). -
bin/: contains yourmain.mlalias your runnable app file. -
test/: contains your tests. -
dune-project: it's the equivalent topackage.jsonin JavaScript orrequirements.txtin Python. -
bin/main.ml: the application’s entry point.
Most of your work will happen in the
lib/folder, and themain.mlfile is the application's entry point.
Create a Switch
Switches in OCaml are similar to Python's virtual environments. They isolate compilers and package versions from another projects or global configurations.
Create a switch with a compiler version (5.3.0 in this example):
opam switch create . 5.3.0 --deps-only
This command creates and stores switch artifacts in the _opam/ folder. The --deps-only flag ensures that only dependencies are installed, and not the current project been taken as another dependency.
If you want to know the available compiler versions run this command and pick one:
opam switch list-available
Activate the Switch
Run at your project directory:
eval $(opam env)
Enable Automatic Switch Detection
You only need to run this command once, it enables automatic switch detection when moving from one OCaml project to another:
opam init --enable-shell-hook
Install Dev Tools for the Switch
Run:
opam install ocaml-lsp-server odoc ocamlformat
Configure Git
Dune projects do not include a .gitignore file by default. Create it manually:
# .gitignore
_opam/
_build/
Initialize Git
Run:
git init
Run the Project
Compile and execute your project:
dune build
dune exec my_project
Alternatively, use watch mode to accomplish both commands in one:
dune exec -w my_project
And voilà you will see this hello world message in your terminal:
Hello, World!
If you wonder where this code lives go to the lib/ folder and open the main.ml file and you will see it:
(* lib/mail.ml *)
let () = print_endline "Hello, World!"
Congratulations! You have created your first OCaml/Dune project!
Happy coding with OCaml! 🚀
Next Steps
- If you want to dig into OCaml I wrote Basic OCaml and its second part Practical OCaml
- Explore the standard library in the OCaml Manual
- Try Real World OCaml
- Browse different OCaml packages on OCaml Packages
Did this help?
- Give it a ❤️
- Follow for Part 2: Ocaml/Dune Modules and Libraries
- Share with one friend learning systems programming
Have a question? Comment below — I reply to all!
References
This content originally appeared on DEV Community and was authored by david2am
david2am | Sciencx (2025-11-14T16:32:34+00:00) OCaml in 5 Minutes: From Zero to ‘Hello’. Retrieved from https://www.scien.cx/2025/11/14/ocaml-in-5-minutes-from-zero-to-hello/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.