Opam: system compiler vs. equivalent version

The questions below are not very deep or important, but I thought I’d use my mild confusion as the basis of the first post here.

I installed opam (using Homebrew on OS X) and it installed ocaml 4.04.1. I then installed a number of packages. Then I ran opam switch 4.04.0 and installed the same packages for that compiler. I wasn’t sure how to switch back to the original compiler, and I like seeing my choices listed explicitly under .opam, anyway, so I ran opam switch 4.04.1 and then installed the packages again. I believe that I now have all of my chosen packages compiled for 4.04.1 both under .opam/4.04.1 and .opam/system. (I’m guessing this about system, since .opam/system/bin/ocaml -version displays reports version 4.04.1.)

I don’t need to have the same packages in two places. Is there a reason to use the second installation of 4.04.1 rather than the original one labeled “system”, or vice versa? Can I remove the packages I installed at the beginning of this process by switching to system and then removing all of the packages? Or would it make more sense to simply remove the second 4.04.1 installation using opam switch remove 4.04.1?

Not quite - assuming a normal opam init, opam set-up a switch designed to use your system compiler (it was probably considerably faster to set-up than the subsequent opam switch commands you ran?).

You shouldn’t be needing to look at anything under .opam - opam list will show you what’s you installed in any given switch. It should be the case that .opam/system/bin/ocaml is a script installed as a result of installing the ocamlfind package (if it’s a binary, you have something strange going on with a system switch) - but again, you shouldn’t really need to be poking in the .opam directory.

There can be occasional issues with using the system compiler - and indeed opam 1.x is not able to detect if you upgrade your system compiler very well (opam 2.0 improves this and will rebuild all the packages) but mostly you should be fine using it and, yes, you could just remove the 4.04.1 switch, as you say.

Some of your confusion comes from the system switch, which is a special-case. It exists so that you can use a pre-installed compiler (e.g. via Homebrew), but OPAM must deal with the fact that you might upgrade it externally (e.g. brew update). So, if you do an opam switch 4.04.1, you are compiling and installing a new version of OCaml 4.04.1, even if your system switch is currently also pointing to OCaml 4.04.1.

You can verify what is actually active on your system by:

$ opam switch
--     -- 4.04.0  Official 4.04.0 release
4.04.1  C 4.04.1  Official 4.04.1 release
system  I system  System compiler (4.04.0)
# 237 more patched or experimental compilers, use '--all' to show

The “C” beside 4.04.1 indicates it is my currently active switch, and the “I” indicates an available but inactive switch.

For your needs, the system compiler is probably sufficient, and you would avoid another locally compiled version. To switch to it just do:

$ opam switch system
$ eval `opam config env`
$ opam switch remove 4.04.1

Thanks dra27, avsm. Both answers were very helpful.