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. use- opam switch create 4.06 ocaml-base-compiler.4.06.0to create a new switch named- 4.06. Just- opam switch create 4.06.0also still works in most cases.
- opam repository(or- opam remote): repositories are still configured globally, but are now selected for each switch. So by default- opam repository addwill only affect the current switch. You can change the defaults with- --set-defaults, and choose the repositories at switch creation time with- opam switch create --repositories REPOS
- opam listand- opam showhave been largely reworked to offer more options
- options 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 corresponding- opam configsubcommands
- new 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 the- remove,- upgrade,- reinstalland- showcommands, and specifying the path to a specific- opamfile 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 prefix
- sandboxing: on Linux and MacOS, all package commands will now be sandboxed by default. The - bubblewraptool is now required to this end on Linux, and the- sandbox_execcommand is used on MacOS. See- faq 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 the- ocamlpackage with- depends: [ "ocaml" {>= "4.00.1"} ]. The- ocaml-versionvariable is no longer defined.
- extra dependencies needed for test and documentation must now be flagged with resp. - with-testand- with-doc. Fields- build-test:and- build-doc:are deprecated in favour of filters on- build:instructions, and there is a new- run-test:field
- the format of the - depexts:field is changed
- the host system is now polled through the - arch,- os,- os-distributionand- os-versionvariables.- osmay now take the value- macosinstead of- darwin.
- 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 than- https://github.com/owner/pkg.git. The latter will still be understood as- gitand rewritten to the former if used from the command-line.
- Any specified - patches:must now apply with- patch -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. Specify- flags: light-uninstallif you do need- remove:instructions, but these are not required to be run from the source tree of the package.
- the - descrfile is now preferably replaced by- synopsis:and- description:fields in the- opamfile (and strings can be put between- """to avoid escaping issues)
- the - urlfile can be replaced by a section of the form- url { src: "URL" checksum: "CKSUM" }. With the above, this allows single-file package definitions
- checksums 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
 
          


