Step-by-Step Guide to Manage OCaml Installations With asdf

sophiabrandt

Sophia Brandt

Posted on January 16, 2020

Step-by-Step Guide to Manage OCaml Installations With asdf

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

If you use zsh:

echo -e '\n. $HOME/.asdf/asdf.sh' >> ~/.zshrc
echo -e '\n. $HOME/.asdf/completions/asdf.bash' >> ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The command adds the opam plugin to asdf. Now you can list all available versions of opam:

Linux:

asdf opam list-all
Enter fullscreen mode Exit fullscreen mode

MacOs:

asdf opam list all
Enter fullscreen mode Exit fullscreen mode

Currently, the lastest version is 2.0.6, so we'll install that one.

asdf install opam 2.0.6
Enter fullscreen mode Exit fullscreen mode

Set this version as the global version:

asdf global opam 2.0.6
Enter fullscreen mode Exit fullscreen mode

See:

opam --version
> 2.0.6
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

If you want to install a different version of OCaml, you can use the opam switch commands:

opam switch list-available
Enter fullscreen mode Exit fullscreen mode

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`
Enter fullscreen mode Exit fullscreen mode

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

💖 💪 🙅 🚩
sophiabrandt
Sophia Brandt

Posted on January 16, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related