Step-by-Step Guide to Manage OCaml Installations With asdf
Tags: devtools, ocaml
Published: 2020-01-16 14:19:23

---
title: Step-by-Step Guide to Manage OCaml Installations With asdf
published: true
date: 2020-01-16 00:00:00 UTC
tags: devtools, ocaml
series: 'How to Learn OCaml'
cover_image: https://images.unsplash.com/photo-1558660334-f4520ac120bb?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=420&ixid=eyJhcHBfaWQiOjF9&ixlib=rb-1.2.1&q=80&w=1000
canonical_url: https://www.rockyourcode.com/step-by-step-guide-to-manage-ocaml-installations-with-asdf/
---


> Use `asdf` to manage opam (and OCaml)

**[asdf][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][rockyourcode]. 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](https://git-scm.com/)

```bash
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:

```bash
echo -e '\n. $HOME/.asdf/asdf.sh' >> ~/.bashrc
echo -e '\n. $HOME/.asdf/completions/asdf.bash' >> ~/.bashrc
```

If you use [fish](https://fishshell.com/):

```bash
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](https://www.zsh.org/):

```bash
echo -e '\n. $HOME/.asdf/asdf.sh' >> ~/.zshrc
echo -e '\n. $HOME/.asdf/completions/asdf.bash' >> ~/.zshrc
```

Check the [asdf documentation](https://asdf-vm.com/#/core-manage-asdf-vm?id=install-asdf-vm) 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][opam]**, the sanctioned package manager for OCaml.

Run this command in your terminal:

```bash
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:

```bash
asdf opam list-all
```

MacOs:

```bash
asdf opam list all
```

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

```bash
asdf install opam 2.0.6
```

Set this version as the global version:

```bash
asdf global opam 2.0.6
```

See:

```bash
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][installocaml]:

In your terminal:

```bash
# 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](https://opam.ocaml.org/doc/Usage.html#opam-switch):

```bash
opam switch list-available
```

This command shows the different OCaml versions.

Install a different version and switch to it, for example:

```bash
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](https://neovim.io/), you can check out my article **[Setup OCaml With NeoVim][setupneovim]**.

### Further Reading

- [asdf][asdf]
- [opam][opam]
- [Ocaml](https://ocaml.org/)
- [Use asdf to Manage Multiple Versions of Languages][rockyourcode]
- [Setup OCaml With NeoVim][setupneovim]

[asdf]: https://asdf-vm.com/#/
[opam]: https://opam.ocaml.org/
[rockyourcode]: https://www.rockyourcode.com/use-asdf-to-manage-multiple-versions-of-languages/
[installocaml]: https://ocaml.org/docs/install.html
[setupneovim]: https://www.rockyourcode.com/setup-ocaml-with-neovim/