OCaml By Examples

opam

console

Install **opam**, the version management and package manager of OCaml. You can also consult the [official opam webpage](https://opam.ocaml.org/doc/Install.html).
$ sh <(curl -sL https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh)
In all of these examples, you will need to **replace** the standard library of OCaml with the well-accepted standard library replacement of Janestreet: **Core**. To consult the documentation of any packages (including Core), refer to the [ocaml docs](https://docs.ocaml.pro/). You can also use [odig](https://erratique.ch/software/odig) to refer to the doc of your installed opam packages.
$ opam install core
Sometimes, things won't work if you don't set some PATHs via this command.
$ eval $(opam env)
The command `opam switch` allows you to manage different versions of OCaml. This is called a **switch** and the installed binaries and libraries are stored in the `~/.opam` directory.
$ opam switch
#  switch   compiler                    description
→  default  ocaml-base-compiler.4.12.0  default
Best practice is to create a **local** switch for your project: a version of OCaml and a set of libraries that will only be used when in a specific folder. When a local switch is used, relevant binaries and libraries are stored under a `_opam` folder in your project.
$ opam switch list-available
...
$ opam switch create . 4.11.0
...
$ opam switch
#  switch           compiler                    description
→  /ocamlbyexample  ocaml-base-compiler.4.11.0  /ocamlbyexample
   default          ocaml-base-compiler.4.12.0  default
next: hello world