Quick upgrade guide from opam 1.2 to opam 2.0
This guide is not a complete list of changes, but it highlights changes that users of opam 1.2 should know about, as well as some important new features.
Command-line
What you need to be aware of
Some commands have changed syntax:
opam switch:createmust be specified to create a new switch. You should then specify either--emptyor a base compiler package, e.g. useopam switch create 4.06 ocaml-base-compiler.4.06.0to create a new switch named4.06. Justopam switch create 4.06.0also still works in most cases.opam repository(oropam remote): repositories are still configured globally, but are now selected for each switch. So by defaultopam repository addwill only affect the current switch. You can change the defaults with--set-defaults, and choose the repositories at switch creation time withopam switch create --repositories REPOSopam listandopam showhave been largely reworked to offer more optionsoptions to build tests and documentation are now respectively
--with-testand--with-doc. They only apply to packages listed on the command-line
The opam-admin tool, for repository operations, is now built into opam, use
the opam admin command instead.
Opam now comes with a solver built-in,
which means that installing aspcud alongside opam is no longer required.
Pinning to a version-controlled directory will now only use what is committed by
default (opam 1.2 had a "mixed mode" where it would use the current versions of
files, but only the version-controlled ones. This was good most of the time, but
could become puzzling e.g. when opam didn't see an added source file). The
option
--working-dir can
be used to temporarily make opam fetch uncommitted changes (see also
--inplace-build), and --assume-built
to run only installation instructions, assuming that build has been done locally.
Upon pinning, opam 2.0 will also select the current branch by default if
unspecified, so that later running git checkout BRANCH in the source tree
doesn't affect the pinned package.
New features
new command aliases:
opam var,opam exec,opam envcan be used in place of the correspondingopam configsubcommandsnew command:
opam cleanto clear caches, logs, etc.local switches: use e.g.
opam switch create .to create a switch in the current directory. The switch contents will be in a_opamdirectory, which can be safely removed once done. The switch path can then be used as a handle to refer to the switch. Additionally, the above command will install any packages which definitions are found in the selected directory into the local switch.automatic pinning: use e.g.
opam install .to pin and install any packages found in the current directory.opam install . --deps-onlycan also be used to prepare a switch for working with a source tree. This extension also concerns theremove,upgrade,reinstallandshowcommands, and specifying the path to a specificopamfile is also supported.archive caching: opam now uses a much better caching mechanism, both locally and on the opam-repository. In particular, upstream repositories being down should no longer prevent package installation (even for older or removed packages). Git repositories are also cached.
better error mitigation, messages and recovery.
opam install --restorecan be used to retry after a failed operation.a plugin, e.g.
opam-depext, will now be available from all switches once installed in one.opam install --destdircan be used to copy build artefacts of given packages to an external prefixsandboxing: on Linux and MacOS, all package commands will now be sandboxed by default. The
bubblewraptool is now required to this end on Linux, and thesandbox_execcommand is used on MacOS. Seefaq entryfor more details.
File formats
What you need to be aware of
Repositories and migration
Repositories for 1.2 and 2.0 have a different format, but everything should be transparent unless you publish packages:
- The main repository is now in format 2.0. This means the
masterbranch, and the contents ofhttps://opam.ocaml.org/packages.- There is a 1.2 branch that is served at
opam.ocaml.org/1.2.2.When publishing packages, remember that:
packages in 2.0 format must be published to the
2.0.0branch — e.g. using the new opam-publish.2.0. They will only be available to opam 2.0 users.packages in 1.2 format are no more accepted, expect for relevant fixes. In that case they must be published to
1.2branch.
opam-publish.2.0.0has a fully revamped interface, and many new features, like filing a single PR for multiple packages. It files pull-request in 2.0 format only to master branch of the repository. The new version ofdune-release.1.0.1handles the new format.It is also advised to keep in-source opam files in 1.2 format until that date, so as not to break uses of
opam pin add --dev-repoby opam 1.2 users.The small
opam-package-upgradeplugin can be used to upgrade single 1.2 opam files to 2.0 format. You can also use API to upgrade you opam files, usingOpamFormatUpgrade.opam_file, available in packageopam-state.More advice for package maintainers and custom repositories in this blog post.
compiler definition files: these no longer exist, as compilers have been replaced by normal package definitions (that should have
flags: compiler)the base syntax of
opamfiles didn't change, but:compilers now being packages, e.g. the field
available: [ ocaml-version >= "4.00.1" ]is now encoded as a dependency towards theocamlpackage withdepends: [ "ocaml" {>= "4.00.1"} ]. Theocaml-versionvariable is no longer defined.extra dependencies needed for test and documentation must now be flagged with resp.
with-testandwith-doc. Fieldsbuild-test:andbuild-doc:are deprecated in favour of filters onbuild:instructions, and there is a newrun-test:fieldthe format of the
depexts:field is changedthe host system is now polled through the
arch,os,os-distributionandos-versionvariables.osmay now take the valuemacosinstead ofdarwin.depopts: [ "foo" >= "v0" ]now means that the optional dependency is only active for the corresponding versions, there is no implicit conflict with"foo" < "v0"
URLs must now be non-ambiguous in files, e.g. you must use
git+https://github.com/owner/pkg.gitrather thanhttps://github.com/owner/pkg.git. The latter will still be understood asgitand rewritten to the former if used from the command-line.Any specified
patches:must now apply withpatch -p1and use unified, rather than context, diffs.opam switch export/importformat has been changed (but files in the 1.2 format can still be read).the conversion from the 1.2 format is done internally and automatic, both for repositories and when pinning. Be careful, however, not to submit 2.0 format files if they are to be used by opam 1.2.
New features
opam files have been extended in a lot of ways:
more expressive dependencies
new fields:
opam now tracks installed files, so the
remove:field can now in general be omitted. Specifyflags: light-uninstallif you do needremove:instructions, but these are not required to be run from the source tree of the package.the
descrfile is now preferably replaced bysynopsis:anddescription:fields in theopamfile (and strings can be put between"""to avoid escaping issues)the
urlfile can be replaced by a section of the formurl { src: "URL" checksum: "CKSUM" }. With the above, this allows single-file package definitionschecksums now accept SHA256 and SHA512 besides MD5. Use the strings
"md5=HEX","sha256=HEX","sha512=HEX".
For more details on the new opam, see:
- the manual
opam COMMAND --help- the changelog
- the blog for announcements of some of the new features
- the tracker


