LinuxCommandLibrary

opam

Manage OCaml packages and dependencies

TLDR

Initialize opam for first use

$ opam init
copy

Search for packages
$ opam search [query]
copy

Install a package and all of its dependencies
$ opam install [package]
copy

Display detailed information about a package
$ opam show [package]
copy

List all installed packages
$ opam list
copy

Update the local package database
$ opam update
copy

Upgrade all installed packages
$ opam upgrade
copy

Display help
$ opam help
copy

SYNOPSIS

opam [global-options] command [command-options] [arguments]

Common commands include:
opam init
opam switch {list | create | set | remove} [switch-name | ocaml-version]
opam install package-name...
opam update
opam upgrade
opam remove package-name...
opam pin {add | remove | list} package-name [source-path]
opam exec -- command [args...]
opam config {env | exec}

PARAMETERS

--help, -h
    Displays help information for opam or a specific command.

--version
    Shows the opam version number.

--root=dir
    Specifies the opam root directory (default: ~/.opam).

--color=mode
    Controls color output (auto, always, never).

--dry-run
    Simulates the command's actions without making actual changes.

--json
    Outputs information in JSON format for commands that support it.

--yes, -y
    Automatically answers yes to all prompts.

--no-strict
    Disables strict sanity checks during operations.

--verbose, -v
    Increases the verbosity of output messages.

DESCRIPTION

opam is the official source-based package manager for OCaml. It enables users to install, upgrade, and manage multiple OCaml compilers and their associated libraries in isolated environments called switches. opam resolves package dependencies automatically, fetches sources, compiles, and installs software from official repositories and custom sources. This isolation prevents conflicts between different OCaml projects and ensures reproducibility. It provides powerful functionalities for managing development workflows, including pinning specific versions of packages, setting up development environments (via opam env), and extending its capabilities through plugins. opam is fundamental for modern OCaml development, offering a robust and flexible way to manage the entire OCaml toolchain and its ecosystem.

CAVEATS

Disk Usage: opam can consume significant disk space, especially with multiple switches and compiled packages, as each switch often duplicates compilers and libraries.
Initial Setup: Requires opam init to be run once to set up the opam root directory and integrate with your shell.
Shell Integration: Proper shell integration (e.g., via eval $(opam env)) is crucial for opam to function correctly by adjusting environment variables (like PATH and OCAML_TOPLEVEL_PATH). Without it, OCaml tools installed by opam might not be found.
Complexity: While powerful, opam's advanced features (e.g., custom repositories, intricate switch management) can have a learning curve for new users.

SWITCHES

opam's core concept for managing isolated OCaml development environments. Each switch contains a specific OCaml compiler version and its own set of installed packages, preventing conflicts between different projects that might require different compiler versions or library dependencies. Users can easily create, list, and switch between these environments using the opam switch command.

REPOSITORIES

opam retrieves packages from repositories, which are collections of package definitions (e.g., version constraints, build instructions). The main repository is opam/opam-repository, hosted on GitHub, containing thousands of OCaml packages. Users can add custom repositories for private packages or experimental versions.

ENVIRONMENT CONFIGURATION

After opam init, opam requires shell integration (e.g., via eval $(opam env)) to correctly set environment variables. This ensures that the shell's PATH points to the executables within the current opam switch, and other variables like OCAML_TOPLEVEL_PATH are configured for interactive OCaml sessions. This setup is crucial for opam-managed tools to be accessible.

HISTORY

opam was initially developed by OCamlPro and first released around 2012. It quickly became the de-facto package manager for the OCaml ecosystem, addressing the long-standing need for a robust and reproducible way to manage OCaml compilers and libraries. Its introduction significantly streamlined OCaml development, making it easier for developers to manage dependencies, switch between compiler versions, and share reproducible project environments. The project later transitioned to community maintenance under the OCaml Foundation, with active development continuing to enhance its features and stability. It is a cornerstone of modern OCaml tooling, underpinning many projects and development workflows.

SEE ALSO

ocaml(1), git(1), make(1), sh(1)

Copied to clipboard