Skip to main content

Online OCaml Compiler

Write OCaml, press run and read the output in seconds. Nothing to install and nothing to configure.

How it works

Free, no install, works on laptop and phone.

What is an online OCaml compiler?

An online OCaml compiler is a free tool that lets you write OCaml code in your browser and run it to see the output, with nothing to install. On Techsnap you can add input, run a program in seconds and use it on a laptop or a phone.

Updated December 2026

How to run OCaml code online

  1. Step 1

    Write your OCaml code

    Open the editor and type or paste your OCaml program. Syntax colours and indentation help you spot mistakes as you go.

  2. Step 2

    Press Run

    Your code is sent to a secure server that compiles and runs it. Add any input the program needs in the Input box first.

  3. Step 3

    Read the output

    Results and error messages appear below the editor within a few seconds. Fix, edit and run again as often as you like.

Everything you need to code in OCaml

Nothing to install

No OCaml setup, PATH or version problems. It works on a college lab PC or a phone.

Input support

Give your program input the same way a judge or exam platform does, then check the output.

Instant results

Most runs finish in a second or two, so you can try an idea and see what happens right away.

Free to start

Run code without paying. A free Techsnap account gives you more runs and saves your work.

OCaml code examples

Open the editor, paste one of these examples in and run it. Change a value and run it again to see what happens.

Hello World in OCaml

let () = print_endline "Hello, Techsnap!"

Values and types

let name = "Asha"
let age = 21
let marks = 87.5
let passed = true

let () = Printf.printf "%s %d %.1f %b\n" name age marks passed

If and else

let marks = 72

let () =
  if marks >= 90 then print_endline "Grade A"
  else if marks >= 60 then print_endline "Grade B"
  else print_endline "Needs work"

Loops and recursion

let () =
  for i = 1 to 3 do
    Printf.printf "for: %d\n" i
  done

let rec countdown n =
  if n > 0 then begin
    Printf.printf "count: %d\n" n;
    countdown (n - 1)
  end

let () = countdown 3

Functions

let square x = x * x
let add a b = a + b
let add_ten = add 10

let () =
  Printf.printf "%d\n" (square 7);
  Printf.printf "%d\n" (add 5 8);
  Printf.printf "%d\n" (add_ten 5)

Lists and higher-order functions

let nums = [4; 9; 12; 15; 20]

let () =
  List.iter (fun n -> Printf.printf "%d " (n * 2)) nums;
  print_newline ();
  List.iter (fun n -> Printf.printf "%d " n) (List.filter (fun n -> n mod 2 = 0) nums);
  print_newline ();
  Printf.printf "%d\n" (List.fold_left (+) 0 nums)

Records

type student = { name : string; marks : int }

let summary s = Printf.sprintf "%s scored %d" s.name s.marks

let () = print_endline (summary { name = "Ravi"; marks = 78 })

Variants and pattern matching

type shape = Circle of float | Rectangle of float * float

let area = function
  | Circle r -> 3.14159 *. r *. r
  | Rectangle (w, h) -> w *. h

let () =
  Printf.printf "%.2f\n" (area (Circle 2.0));
  Printf.printf "%.2f\n" (area (Rectangle (3.0, 4.0)))

Option instead of null

let safe_divide a b = if b = 0 then None else Some (a / b)

let show = function
  | Some v -> string_of_int v
  | None -> "Cannot divide by zero"

let () =
  print_endline (show (safe_divide 10 2));
  print_endline (show (safe_divide 5 0))

Exceptions

let () =
  try
    let age = int_of_string "twenty" in
    Printf.printf "%d\n" age
  with Failure _ -> print_endline "Caught: not a number"

How to take input in OCaml online

OCaml reads a line of standard input with read_line (). Convert text with int_of_string, and split a line of values with String.split_on_char.

let () =
  let n = int_of_string (read_line ()) in
  let nums =
    String.split_on_char ' ' (read_line ())
    |> List.map int_of_string in
  Printf.printf "count: %d\n" n;
  Printf.printf "sum: %d\n" (List.fold_left (+) 0 nums)

Common OCaml errors and how to fix them

Error: Unbound value x
The name is not defined yet or is misspelled. OCaml reads top to bottom, so define values before using them.
Error: This expression has type int but an expression was expected of type float
OCaml has separate operators for floats. Use +. and *. for floats, and convert with float_of_int.
Error: Syntax error
Check for a missing in after a let, an extra semicolon, or an unclosed bracket.
Warning: this pattern-matching is not exhaustive
Your match does not cover every case. Add the missing case or a wildcard _.

Who uses an online OCaml compiler

Students and learners

Practise OCaml for class assignments and exams without installing anything.

Teachers and mentors

Share a short program in a lesson and let students run and change it on their own devices.

Interview candidates

Rehearse OCaml questions in the same plain editor many online tests use.

Working developers

Check a snippet or an API behaviour in seconds without opening an IDE.

Writers and bloggers

Test code samples so the examples in your tutorials really run.

Teams

Paste a reproducible example into a ticket or chat so everyone sees the same result.

Online compiler vs local OCaml setup

Techsnap online compilerLocal setup
Setup timeNone, open the pageInstall OCaml, set PATH, pick a version
Works on any deviceYes, any modern browserOnly the machine you set up
Best forPractice, quick tests, learningLarge projects and long-running work
Extra librariesStandard library onlyAnything you can install

About OCaml

What is OCaml?

OCaml is a functional language with a strong static type system and fast native compilation. The compiler infers types, so code stays short while catching many errors before it runs.

What is OCaml used for?

Compilers, theorem provers, static analysis, finance systems and teaching. Tools such as Coq are written in OCaml, and it is used heavily at some trading firms.

OCaml for beginners

Learn let bindings, functions, lists and pattern matching. Remember that float operators have a dot (+.), and that lists use semicolons between items.

OCaml vs Haskell

Both are typed functional languages. OCaml allows side effects and mutable values when needed and is more practical for everyday programs; Haskell is purely functional and lazy.

Why use an online OCaml compiler?

Setting up OCaml with opam and a build system is a lot for a first try. An online OCaml compiler lets you try the language at once.

OCaml career opportunities

OCaml jobs are few but specialised, in finance, compilers and tooling. It is also taught in many university courses on programming languages.

Explore more online compilers

Frequently asked questions

Is this online OCaml compiler free?

Yes. Write and run OCaml code in your browser for free, with nothing to install. A free Techsnap account gives you more runs and saves your work.

Why does OCaml have separate operators for floats?

OCaml never converts between int and float automatically, so + works on ints and +. works on floats. This avoids hidden type conversions.

How do I take input in OCaml online?

Use the Input box and read lines with read_line (), as shown on this page.

Can I run OCaml on my phone?

Yes. The editor, input and output work in a mobile browser, so you can practise without a laptop.

Ready to write OCaml code?

Open the editor and run your first program in under a minute.