Online OCaml Compiler
Compile and run OCaml online with OCaml 5.5. Write your program, press Run, and ocamlopt compiles it to native code and starts it, with type errors shown by line if the build fails. Programs can read input with read_line as they run, and the standard library is ready to use.
How to run OCaml code online
- This page opens with OCaml selected, so you can start typing straight away.
- Write your code in the editor, or open files from your computer: single files, or a whole project as a .zip.
- Press Run, or Ctrl+Enter. The output appears in the panel beside the editor, and when the program asks for input you type it there and press Enter.
- Share a copy with a link, or save your files to your computer.
OCaml 5.5 on Online IDE
- Your program is compiled to native code with ocamlopt from OCaml 5.5, then run.
- read_line () reads a line of input, and a prompt from print_string shows first, as read_line flushes the output before it waits.
- int_of_string_opt turns text into a number without raising an exception, so unexpected input cannot crash the program.
- The standard library is available, but opam packages such as Core or Lwt are not.
Example: a OCaml program that reads input
let () =
print_string "Enter a whole number: ";
match int_of_string_opt (String.trim (read_line ())) with
| Some n when n > 0 ->
let divisors = List.filter (fun d -> n mod d = 0) (List.init n (fun i -> i + 1)) in
Printf.printf "Divisors of %d: %s\n" n (String.concat ", " (List.map string_of_int divisors))
| _ -> print_endline "Please enter a whole number above zero."
Features of this tool
- Simple and fastA clean, lightweight editor that opens instantly.
- Powered by AceThe Ace code editor, with syntax highlighting, autocompletion and bracket matching.
- Interactive programsType input into the terminal while your program runs.
- Several filesWork in tabs, open files or a .zip from your computer, and save the project back to disk.
- Made for learningHelpful for beginners to learn and practice programs.
- Light and dark themesPlus 39 editor themes, a choice of coding fonts, and Vim, Emacs or VS Code keys.
- Keep your outputCopy or download the program output in one click.
- Share by linkSave your code in the cloud and open it anywhere.
Frequently asked questions
Which OCaml compiler does Online IDE use?
ocamlopt from OCaml 5.5, which compiles your program to native code for Linux.
Can an OCaml program read input?
Yes. read_line () reads a line; type it into the output panel while the program waits and press Enter. At the end of the input it raises End_of_file.
Can I use opam packages or dune?
No. Only the standard library is installed, and the file you run is compiled on its own with ocamlopt.
Can my program span several files?
No. The file you run is compiled on its own, so keep the program in it, with modules defined inside the file where you need them.
Contact us
For any feedback, queries or issues, please feel free to contact us or write to . We value your input and are committed to providing the best possible experience for our users.