Hello World in OCaml

Our first program will print the classic “hello world” message. Here’s the full source code.

let () =
  print_endline "hello world"

To run the program, save the code in a file named hello_world.ml and use the ocaml command to execute it.

$ ocaml hello_world.ml
hello world

Sometimes we’ll want to compile our OCaml programs into executables. We can do this using ocamlc.

$ ocamlc -o hello_world hello_world.ml
$ ls
hello_world  hello_world.cmi  hello_world.cmo  hello_world.ml

We can then execute the compiled binary directly.

$ ./hello_world
hello world

Now that we can run and build basic OCaml programs, let’s learn more about the language.