Update OCaml and OPAM along with packages

As a beginner of OCaml, it felt a bit unwieldy to perform an upgrade of my current environment (OCaml + OPAM + packages). I knew there was not a single command that does all the tasks like conda for Python. Luckily, it was not so difficult with OPAM’s import/export function [1]. I upgraded my OCaml environment with all installed packages retained following these steps without much trouble.

  1. Export the list of currently installed packages:

    opam switch export opam-pkg-list.txt
    
  2. Update OCaml & OPAM with the system package manager (e.g., Homebrew on macOS or apt-get on a major Linux distribution).

    Now the system OCaml should be upgraded to the latest version. If the updated version is not set as the system OCaml, use opam switch <version> to switch to it.

  3. Import the list of installed packages. Pay attention to the warnings and error messages because some packages may not be installed automatically and therefore need intervention.

    opam switch import opam-pkg-list.txt
    
  4. Whatever package that cannot be automatically imported in the previous step needs to be manually installed with OPAM. Deal one error message at a time. Once those packages that give error messages are manually installed, run opam switch import again to import the rest of uninstalled packages.