DEV Community

loading...
Cover image for Step-by-Step Guide to Manage OCaml Installations With asdf

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

Sophia Brandt
former tax officer turned programmer
Originally published at rockyourcode.com on ・3 min read

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

Discussion (4)

Collapse
idkjs profile image
Alain

Running asdf opam list-all doesn't seem to work for me. Getting:

➜  ~ asdf opam list-all
zsh: correct 'opam' to '.opam' [nyae]? n
Unknown command: `asdf opam list-all`
version: v0.7.6-6207e42

MANAGE PLUGINS
  asdf plugin add <name> [<git-url>]       Add a plugin from the plugin repo OR, add a Git repo
...
Collapse
sophiabrandt profile image
Sophia Brandt Author

What's the result of asdf plugin-list? It should show that opam is installed.
On my Linux machine, the command for listing all versions of a plugin is asdf plugin list-all, but the documentation says asdf plugin list all (without hyphen).
Perhaps it's different on a Mac?

Collapse
idkjs profile image
Alain

asdf plugin list all works!

➜  tweets-and-users [master*]asdf plugin list all
initializing plugin repository...
Cloning into '/Users/prisc_000/.asdf/repository'...
remote: Enumerating objects: 35, done.
remote: Counting objects: 100% (35/35), done.
remote: Compressing objects: 100% (33/33), done.
remote: Total 1332 (delta 15), reused 17 (delta 2), pack-reused 1297
Receiving objects: 100% (1332/1332), 293.36 KiB | 2.93 MiB/s, done.
Resolving deltas: 100% (553/553), done.
1password                  https://github.com/samtgarson/asdf-1password.git
R                          https://github.com/iroddis/asdf-R.git
adr-tools                  https://gitlab.com/td7x/asdf/adr-tools.git
aks-engine                 https://github.com/robsonpeixoto/asdf-aks-engine.git
alp                        https://github.com/asdf-community/asdf-alp.git
aria2                      https://github.com/asdf-community/asdf-aria2.git
bazel                      https://github.com/rajatvig/asdf-bazel.git
binnacle                   https://github.com/Traackr/asdf-binnacle.git

...
➜  tweets-and-users [master]
Thread Thread
sophiabrandt profile image
Sophia Brandt Author

That's great!
But it looks like the authors implemented a different CLI syntax for Linux and Mac...
I will edit that in my post.