OCaml in 5 Minutes: From Zero to ‘Hello’

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


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 .bashrc or .zshrc file 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 (.ml files).
  • bin/: contains your main.ml alias your runnable app file.
  • test/: contains your tests.
  • dune-project: it's the equivalent to package.json in JavaScript or requirements.txt in Python.
  • bin/main.ml: the application’s entry point.

Most of your work will happen in the lib/ folder, and the main.ml file 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

Did this help?

Have a question? Comment below — I reply to all!

References


This content originally appeared on DEV Community and was authored by david2am


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » OCaml in 5 Minutes: From Zero to ‘Hello’." david2am | Sciencx - Friday November 14, 2025, https://www.scien.cx/2025/11/14/ocaml-in-5-minutes-from-zero-to-hello/
HARVARD
david2am | Sciencx Friday November 14, 2025 » OCaml in 5 Minutes: From Zero to ‘Hello’., viewed ,<https://www.scien.cx/2025/11/14/ocaml-in-5-minutes-from-zero-to-hello/>
VANCOUVER
david2am | Sciencx - » OCaml in 5 Minutes: From Zero to ‘Hello’. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/11/14/ocaml-in-5-minutes-from-zero-to-hello/
CHICAGO
" » OCaml in 5 Minutes: From Zero to ‘Hello’." david2am | Sciencx - Accessed . https://www.scien.cx/2025/11/14/ocaml-in-5-minutes-from-zero-to-hello/
IEEE
" » OCaml in 5 Minutes: From Zero to ‘Hello’." david2am | Sciencx [Online]. Available: https://www.scien.cx/2025/11/14/ocaml-in-5-minutes-from-zero-to-hello/. [Accessed: ]
rf:citation
» OCaml in 5 Minutes: From Zero to ‘Hello’ | david2am | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.