DEV Community

Cover image for OCaml in 5 Minutes: From Zero to 'Hello'
david2am
david2am

Posted on

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

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
Enter fullscreen mode Exit fullscreen mode

For Linux

sudo apt-get install opam
Enter fullscreen mode Exit fullscreen mode

Initialize OCaml's global configuration:

opam init -y
Enter fullscreen mode Exit fullscreen mode

Load Opam's Environment:

eval $(opam env)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Activate the Switch

Run at your project directory:

eval $(opam env)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Install Dev Tools for the Switch

Run:

opam install ocaml-lsp-server odoc ocamlformat
Enter fullscreen mode Exit fullscreen mode

Configure Git

Dune projects do not include a .gitignore file by default. Create it manually:

# .gitignore
_opam/
_build/
Enter fullscreen mode Exit fullscreen mode

Initialize Git

Run:

git init
Enter fullscreen mode Exit fullscreen mode

Run the Project

Compile and execute your project:

dune build
dune exec my_project
Enter fullscreen mode Exit fullscreen mode

Alternatively, use watch mode to accomplish both commands in one:

dune exec -w my_project
Enter fullscreen mode Exit fullscreen mode

And voilà you will see this hello world message in your terminal:

Hello, World!
Enter fullscreen mode Exit fullscreen mode

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!"
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)