๐ Setup: your local forge
Phases 1โ2 mostly run in the browser. But real projects โ the canvas starter, the snake port, domains, the capstone โ need OCaml on your machine. Twenty minutes, once.
The toolchain, in one table
| Tool | What it is | Game analogy |
|---|---|---|
opam | OCaml's package & compiler manager | the item shop |
ocaml 5.5.0 | the compiler + toplevel | the game engine itself |
dune | the build system | the crafting bench |
utop | a deluxe toplevel for your terminal | the training arena |
js_of_ocaml | compiles OCaml bytecode to JavaScript | the portal to the browser world |
1 ยท Install opam
Linux / macOS โ official installer script (or your package
manager: apt install opam, dnf install opam,
brew install opam; make sure you get opam โฅ 2.1):
Windows โ two good options: WSL2 with Ubuntu, then follow the Linux steps inside it (recommended โ it matches every terminal block on this site), or native opam โฅ 2.2 from ocaml.org/install.
2 ยท Create the OCaml 5.5 switch and install the tools
Verify the loot:
(Later versions are fine.)
3 ยท Editor
VS Code with the OCaml Platform extension gives inline types, errors and formatting. Install the extension, then:
4 ยท The canvas starter (your first portal)
Download canvas-starter.zip โ a minimal dune project that compiles OCaml to JavaScript and animates a square on an HTML canvas. It's the skeleton every local project in this course grows from.
Open http://localhost:8000 โ a bouncing square you steer with the arrow keys. The project layout:
canvas-starter/
โโโ dune-project # project manifest (one line of config)
โโโ bin/
โ โโโ dune # "build main.ml to JavaScript"
โ โโโ main.ml # ALL the game logic โ yours to edit
โโโ web/
โโโ dune # copies the compiled JS next to the page
โโโ index.html # the page: canvas + HUD (Maks's territory)
โโโ style.css # the look (also Maks's territory)
Edit bin/main.ml, run dune build again,
refresh the browser. That's the whole loop. (Try dune build -w
for auto-rebuild on save.)
5 ยท Hosting this site (localhost or homelab)
The site is pure static files in site/. Simplest:
For the homelab, a tiny nginx container (Dockerfile included in the repo โ nginx also gzips the 11 MB engine down to ~3 MB on the wire):
Then any device on your network can play at
http://<server-ip>:8043.
6 ยท Rebuilding the in-page engine (optional)
The engine at site/assets/toplevel/toplevel.js is an
OCaml 5.5 toplevel compiled with js_of_ocaml (source in
playground-src/). It ships prebuilt; rebuild only if you
upgrade OCaml or hack on it:
Troubleshooting
command not found: duneโ runeval $(opam env)(or open a fresh terminal).opam switch create 5.5.0says unknown compiler โopam updatefirst.- Port already in use โ change
8000/8043to anything free. - Engine pill says "unavailable" โ the site was copied without
site/assets/toplevel/toplevel.js; rebuild it (section 6) or re-copy. - The page froze after Run โ an infinite loop in the code (it happens to everyone); reload the tab โ exercise code is autosaved.