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
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 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
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 and powerline10k.
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
Top comments (0)