Sophia Brandt
Posted on January 16, 2020
Use
asdf
to manage opam (and OCaml)
asdf is a command-line tool which allows you to install multiple versions of a programming language.
With asdf you have absolute control over which language version gets installed on your system.
You can also switch between different versions. That's useful if you work with several projects that might use different versions.
I wrote about asdf a while ago. In this post, I will go over the steps on how to manage OCaml via asdf.
For me, it's the most convenient way to exactly control which version of OCaml runs on my computer.
Install asdf
- Prerequisites: git
git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.7.6
Then you have to add asdf to your shell.
If you use bash:
echo -e '\n. $HOME/.asdf/asdf.sh' >> ~/.bashrc
echo -e '\n. $HOME/.asdf/completions/asdf.bash' >> ~/.bashrc
If you use fish:
echo 'source ~/.asdf/asdf.fish' >> ~/.config/fish/config.fish
mkdir -p ~/.config/fish/completions; and cp ~/.asdf/completions/asdf.fish ~/.config/fish/completions
If you use zsh:
echo -e '\n. $HOME/.asdf/asdf.sh' >> ~/.zshrc
echo -e '\n. $HOME/.asdf/completions/asdf.bash' >> ~/.zshrc
Check the asdf documentation for trouble-shooting and further information.
You can also use Homebrew on MacOS to install asdf.
Install opam
Now you can use asdf to install opam, the sanctioned package manager for OCaml.
Run this command in your terminal:
asdf plugin-add opam https://github.com/asdf-community/asdf-opam.git
The command adds the opam plugin to asdf. Now you can list all available versions of opam:
Linux:
asdf opam list-all
MacOs:
asdf opam list all
Currently, the lastest version is 2.0.6
, so we'll install that one.
asdf install opam 2.0.6
Set this version as the global version:
asdf global opam 2.0.6
See:
opam --version
> 2.0.6
Don't install the asdf plugin for OCaml!
You can install and manage OCaml with opam.
Install OCaml
Now, that we have opam installed, let's use it to set up OCaml:
In your terminal:
# environment setup
opam init
eval `opam env`
# install given version of the compiler
opam switch create 4.09.0
eval `opam env`
# check you got what you want
which ocaml
ocaml -version
If you want to install a different version of OCaml, you can use the opam switch commands:
opam switch list-available
This command shows the different OCaml versions.
Install a different version and switch to it, for example:
opam switch create 4.08.0
eval `opam env`
Now you have a working installation.
Next Steps
Install some packages, and integrate OCaml into your text editor.
If you use NeoVim, you can check out my article Setup OCaml With NeoVim.
Further Reading
Posted on January 16, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.