Package management Last updated Mar 18, 2025
The standard package manager for OCaml is opam.
Instruction for configuring an opam switch is beyond the scope of this guide, but here are some common useful commands:
-
$ opam switch show
- prints the name of the current switch -
$ opam switch
- shows all installed switches -
$ opam list
- shows all packages installed in the current switch -
$ opam pin list
- shows all "pinned" packages -
$ opam config list
- lists all general configuration settings -
$ opam config list ounit2
- lists all configuration settings for packageounit2
-
$ opam var
- same asopam config list
-
$ opam var bin
- print the value of thebin
var of the current switch -
$ opam var --package <pkg>
- prints all variables for package<pkg>
as<pkg>:<var>
-
$ opam var <pkg>:<var>
- prints value of variable for package, e.g.-
$ opam var ounit2:version
- prints version string of packageounit2
-
"opam var --package …" may be very, very slow. |
OPAM switch names commonly match the version string of the installed
compiler, but this is not required; you can name your switches however
you wish. In that case, here are some useful commands, using --switch
<s>
to pick out a switch:
-
$ opam list --switch myswitch
- list packages installed for switchmyswitch
-
$ opam config list --switch myswitch
- prints variables for switchmyswitch
. -
$ opam var --package ocaml --switch myswitch
- prints variables for theocaml
package of switchmyswitch
-
$ opam var ocaml:version --switch myswitch
- printsversion
variable of theocaml
package of switchmyswitch
-
$ opam var ocaml:depends --switch myswitch
- printsdepends
variable (i.e. compiler build options) of theocaml
package of switchmyswitch
-
-
$ opam var --package ocaml-base-compiler --switch myswitch
- prints variables for theocaml-base-compiler
package of switchmyswitch
Compiler versions are treated as (pseudo) packages. When you run
$ opam switch you will see that the "compiler" column lists strings of
form <pkgname>.<varname> ; for example, ocaml-base-compiler.4.12.0 .
If you have installed a compiler with +options the string will look
like ocaml-variants.4.14.0+options . Use the <pkgname> part to obtain information about the compiler, e.g. $ opam var --package ocaml-base-compiler:version --switch myswitch .
|
The ocaml-base-compiler variable is unreliable! Don’t use it unless it is listed in the output of $ opam config list --switch myswitch .
|
-
If you use
emacs
, you probably want to installmerlin
.
-
Locally installed (system) libraries. Some OPAM packages depend on locally installed resources. For example, package
bignum
depends on packagezarith
, which depends on a local installation oflibgmp
(usually in/usr/local
).