asdf - the final(?) version manager
Tags: tooling
Published: 2021-04-26 12:43:21

## Introduction

Have you ever had the need to run multiple different versions of Nodejs? -Of course, silly! We have [nvm](https://github.com/nvm-sh/nvm) for that. 

How about python, then? -Yeah, we use [pyenv](https://github.com/pyenv/pyenv). 

Java? -Yup. [SDKman](https://sdkman.io/). 

Ruby? -[rvm](https://rvm.io/).

Tmux? -Um...

Vim? -But why?!

If you are (or pretending to be) a polyglot software developer, you'll need a whole lot of versioning managers and a whole lot of different commands and syntax to remember. 

Wouldn't it be nice if there was one tool that could handle everything? There is - [asdf](https://github.com/asdf-vm/asdf)!

## Installation

`asdf` is available on both macOS and Linux. The examples in this article will use macOS.

### Dependencies

`asdf` requires you to have `corutils`, `curl` and `git` installed. We can install them using Homebrew:

    brew install coreutils curl git

We'll install `asdf` itself with Homebrew too:

    brew install asdf

Next we need to add `asdf.sh` to our `.zshrc`:

    echo -e "\n. $(brew --prefix asdf)/asdf.sh" >> ${ZDOTDIR:-~}/.zshrc
(if you're using [oh-my-zsh](https://ohmyz.sh/) there is a [plugin for asdf](https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/asdf))

Now we're ready to start using it!

## Getting started with `asdf`

These are the basic principles for using `asdf`. Be sure to check out the [official documentation](https://asdf-vm.com/) as well.

### Adding plugins and versions

`asdf` has a [long list](https://asdf-vm.com/#/plugins-all) of plugins for different languages and tools.

To add python we simply execute:

    asdf plugin add python

`asdf` is now python-aware, and we can install multiple versions of it. 

To get the latest one (3.9.4 when I wrote this):

    asdf install python latest

To get a specific version:

    asdf install python 3.8.7

### Setting versions

Now that we have a couple of versions of Python installed we should set the default global version:

    asdf global python 3.9.4

We can also set the global version to the operating systems version of python (not managed by asdf):

    asdf global python system

To set a version to be used in the current directory (and any sub-directories) we run:

    asdf local python 3.8.7

We can even set a specific version for the current shell:

    asdf shell python 3.8.7

Finding out what versions are active is as simple as running:

```
asdf current
```

### The `.tool-versions` file

To keep track of what versions to use, `asdf` creates a `.tool-versions` file. For global settings this will be located at `$HOME/.tool-versions` and for local versions it'll be put in your current directory.

This is just a text file describing what versions to use and it can look something like this:

    nodejs 16.0.0
    python 3.9.4

It is also possible to have several versions defined:

    python 3.9.4 3.8.7 system

## Coming from other version managers

"I already use `nvm` and `pyenv`, and have a million `.nvmrc`, `.node-version` and `.python-version` files sprinkled through-out my system!", you might say. `asdf` can take advantage of these files if we tell it to.

We need to create a file in your home directory called `.asdfrc` with the following content:

```
legacy_version_file = yes
```

This will enable support for reading version files used by other version managers.

## Conclusion

That is pretty much all there is to it. It is _super_ handy to just `cd` into a directory and have `asdf` automatically switch to the correct version of the tools needed for that particular project.

It also integrates very nicely with [oh-my-zsh](https://ohmyz.sh/) and [powerline10k](https://github.com/romkatv/powerlevel10k).
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4v5543oxp2hpyz31jed.png)

---

_Jonathan Walter is a Media Consultant at [Eyevinn Tecnology](https://www.eyevinn.se/), the European leading independent consultancy firm specializing in video technology and media distribution._

_Photo by <a href="https://unsplash.com/@scottwebb?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Scott Webb</a> on <a href="https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>_