How do you install an opam package on all your switches or on different switch then you are on?

I am thinking some thing like this, which doesn’t work.

I am on 4.10.0 globally but have 4.06.1 set locally in vscode for a reason project via settings.json. So I forgot to install ocamllsp when i created the switch. Is it possible to install ocamllsp on 4.06.1 while my current env 4.10.0?

[I] ➜ opam switch
#  switch                      compiler
          description
   4.06.1                      ocaml-base-compiler.4.06.1
          4.06.1
→  4.10.0                      ocaml-base-compiler.4.10.0
          4.10.0
   4.11.1                      ocaml-base-compiler.4.11.1
          4.11.1
   default                     ocaml-base-compiler.4.11.1
          default
   ocaml-base-compiler.4.06.1  ocaml-base-compiler.4.06.1
          ocaml-base-compiler.4.06.1
   ocaml-base-compiler.4.07.0  ocaml-base-compiler.4.07.0
          ocaml-base-compiler.4.07.0
   ocaml-base-compiler.4.08.0  ocaml-base-compiler.4.08.0
          ocaml-base-compiler.4.08.0
   ocaml-base-compiler.4.10.2  ocaml-base-compiler.4.10.2
          ocaml-base-compiler.4.10.2
   reason                      ocaml-base-compiler.4.06.1
          reason

[WARNING] The environment is not in sync with the current
          switch.
          You should run: eval (opam env)
> opam install ocaml-lsp-server reason -s 4.06.1 -y
opam: unknown option `-s'.
Usage: opam install [OPTION]... [PACKAGES]...
Try `opam install --help' or `opam --help' for more information.
> opam install ocaml-lsp-server reason -s 4.06.1 -y

Thank you.

Did you try to use environment variable OPAMSWITCH to install on another switch?

I did not. Let me go look that up.

Hi @idkjs

As @Kakadu mentioned you can use OPAMSWITCH which is the environment variable to work with the --switch command line argument.

Something like:

opam install --switch=4.06.1 -y ocaml-lsp-server reason

should work.

Just as another possible solution that might work for you is opam local switches. Instead of being globally installed in some place like ~/.opam these can be used per project so you will have a _opam directory in that project.

cd ./project
opam switch create . 4.06.1

That distinct advantage here is that opam will automatically choose that switch if you are in that directory so you don’t have to worry too much about selecting the correct switch. This might be useful or it might not :))

4 Likes

Thank you @Kakadu @patricoferris. What I needed.

1 Like

I just want to say that the next release of lsp will not support 4.06. You will need to stick to the current version if you want to use such an old version of OCaml.

How do we install to all switches?
That might be useful sometimes; even as a test to check that some software builds on a set of OCaml-versions (the installed switches).

1 Like

Thanks. Good info. Not really sure about compilers anyway. What is the next version number?

opam install --switch=4.08.1 -y ocaml-lsp-server reason

@UnixJunkie did you figure out how to install on all switches, sir?

With a shell script for loop.

2 Likes

Come on, @UnixJunkie! Easy script to write but you could have just posted it, no?

Thanks, I had not thought about that.

Disclaimer: not a shell script expert, there are probably nicer/cleaner ways of doing it!

This works on my mac:

opam switch list -s | xargs -I % sh -c 'opam install --switch=% -y <pkg-name>'

You can try it with --dry-run to see what it is about to attempt. Hope that helps.

1 Like

much cleaner that what I just came up with though it seems mine handles arrays of packages for some reason:

#!/usr/bin/env sh

opam_switches () {
    switches=$(opam switch list --short)

    for switch in $switches
    do
        opam install "$@" --switch "$switch" -y
    done
}

might want to make the switch variable local in the function: local switches. But then I don’t know if it works on all sh interpreters, like ash. It will work for bash though.

I like both solutions, depending on if it is in a script or just a one liner.

1 Like