If you are using Windows 10, I suggest to install Ubuntu via the Windows Subsystem for Linux, following the same instructions as for setting up the Scala tool chain. Then follow the instructions for Linux below.
Most Linux distributions as well as homebrew on Mac OS come with precompiled packages for OCaml. There is also a Windows installer. However, I suggest to install OCaml using opam, which is a package manager for OCaml that makes it easy to install many other useful tools for developing OCaml programs.
Make sure that you have XCode installed; see Scala setup)
Then install opam by executing
brew install ocaml
brew install opam
If you are using Ubuntu 18.04 on WSL, it uses an older version of opam by default. To install the latest opam version, first add the latest ppa containing the stable version of opam:
sudo add-apt-repository ppa:avsm/ppa
sudo apt update
After this (or if you are using a newer Ubuntu version), install the latest stable version of opam:
sudo apt install opam
Once you have opam installed, you can install and set up the most recent version of the OCaml language and compiler by first executing the following commands in a terminal:
opam init --disable-sandboxing
then continue with the instructions for all platforms below following the steps after opam init
has been executed.
To install opam execute:
sudo apt install opam
Please read the installation instructions of opam if you have another operating system.
Once you have opam installed, you can install and set up the most recent version of the OCaml language and compiler by executing the following commands in a terminal:
opam init
If you see a warning related to a missing m4
dependency, then
install m4
before you proceed using
brew install m4
respectively
sudo apt install m4
Then you can create a 'switch' for the most recent OCaml release by executing
opam switch create 4.14.0
eval `opam config env`
The installation will take a while since opam will download the
sources of the OCaml compiler and compile it from scratch. Follow
the instructions provided by the output of these commands to set up your
environment variables. Once, the installation has completed, you can
execute ocaml
, which starts an OCaml REPL session:
OCaml version 4.14.0
#
You can also install an alternative OCaml REPL called utop
that provides additional functionality. To install utop
execute:
opam install -y utop
One you have started a REPL session, you can use it to evaluate OCAML
expressions. In the REPL, you need to terminate each expression by a
double semicolon ;;
and then press Enter
:
# 3 + 1 ;;
- : int = 4
# let x = 3 + 1 ;;
val x : int = 4
# #quit ;;
These double semicolons are only needed in the REPL but not in source code files that are processed by the compiler.
In addition to the OCaml compiler and runtime, you also want to install the OCaml library manager ocamlfind, the OCaml build tool Dune, and the OCaml unit testing framework OUnit. You can do this via opam:
opam install -y ocamlfind
opam install -y dune
opam install -y ounit2
These tools provide similar functionality as sbt
and scalatest
do for Scala.
Several IDEs have plugins for OCaml. I suggest to use Merlin which provides IDE support for OCaml in common editors like Emacs and Vim. Merlin can also be integrated into other editors and IDEs via third-party plugins, including Atom, Sublime, and Visual Studio Code. You can install Merlin via opam by executing:
opam install -y merlin
See the installation instructions on Merlin's project website for further details on how to configure various editors.
If you want a modern IDE, Visual Studio Code works well with OCaml in my experience. Follow the installation instructions for your operating system/distribution. For supporting OCaml, you should install the OCaml and Reason IDE extension. You can do this from within Visual Studio Code by selecting
File -> Preferences -> Extensions
Then search for the vscode-reasonml
extension and install it. The
extension builds on top of Merlin which you can install via opam (see
above).