DEV Community

loading...
Cover image for asdf - the final(?) version manager
Eyevinn Video Dev-Team Blog

asdf - the final(?) version manager

Jonathan Walter
・3 min read

Introduction

Have you ever had the need to run multiple different versions of Nodejs? -Of course, silly! We have nvm for that.

How about python, then? -Yeah, we use pyenv.

Java? -Yup. SDKman.

Ruby? -rvm.

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!

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

We'll install asdf itself with Homebrew too:

brew install asdf
Enter fullscreen mode Exit fullscreen mode

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

echo -e "\n. $(brew --prefix asdf)/asdf.sh" >> ${ZDOTDIR:-~}/.zshrc
Enter fullscreen mode Exit fullscreen mode

(if you're using oh-my-zsh there is a plugin for 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 as well.

Adding plugins and versions

asdf has a long list of plugins for different languages and tools.

To add python we simply execute:

asdf plugin add python
Enter fullscreen mode Exit fullscreen mode

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

To get a specific version:

asdf install python 3.8.7
Enter fullscreen mode Exit fullscreen mode

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

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

asdf global python system
Enter fullscreen mode Exit fullscreen mode

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

asdf local python 3.8.7
Enter fullscreen mode Exit fullscreen mode

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

asdf shell python 3.8.7
Enter fullscreen mode Exit fullscreen mode

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

asdf current
Enter fullscreen mode Exit fullscreen mode

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

It is also possible to have several versions defined:

python 3.9.4 3.8.7 system
Enter fullscreen mode Exit fullscreen mode

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

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 and powerline10k.
Alt Text


Jonathan Walter is a Media Consultant at Eyevinn Tecnology, the European leading independent consultancy firm specializing in video technology and media distribution.

Photo by Scott Webb on Unsplash

Discussion (0)