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
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 :))
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).
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.