Search code examples
shellocamlzshopam

Is there a way to activate the default opam switch on shell startup?


It's very tedious to have to activate the default switch every time I want to start using ocaml with eval $(opam env --switch=default --set-switch). I was given an option when I first initialized opam to update my .zshrc with a line to do this on startup but I must have missed it because the initialization continued on without my input. I tried manually adding the line to my .zshrc but the switch that's activated is not my default switch, which uses ocaml v5.0.0. Instead, it activates another switch I created with v4.14.0.

For context, the line I added to my runtime config was

[[ ! -r /Users/USERNAME/.opam/opam-init/init.zsh ]] || source /Users/USERNAME/.opam/opam-init/init.zsh  > /dev/null 2> /dev/null

Maybe the answer to my question is that it's not recommended to activate a switch by default?


Solution

  • It is generally recommended to activate a switch by default if you always want to use a specific version of a tool or language. In your case, it seems that the line you added to your .zshrc is not activating the default switch, but rather a different switch you created with version 4.14.0 of OCaml.

    To activate the default switch every time you start a new terminal session, you can add the following line to your .zshrc file:

    eval $(opam env --switch=default --set-switch)
    

    This will activate the default switch and set the environment variables for that switch. Make sure to remove any other lines that activate a different switch.

    After adding this line to your .zshrc file, run the following command to source your updated .zshrc file:

    source ~/.zshrc
    

    This should activate the default switch every time you start a new terminal session.