When dealing with many projects we always come across a project whose version is different from the one you are using and it is often necessary to install another version or upload a Docker container, which I did for a long time, in node I used nvm, and I always wanted something similar for the other languages and so I present you the asdf.
But what is this asdf?
asdf is a version manager, let’s say you are using a project with PHP 5.0 but only version 7.4 is installed on your machine, how to do it? What if I still have to run projects on both versions? asdf comes to solve this problem, it allows the installation of both versions in a simple way and even makes it possible to run both at the same time.
Installing asdf
Currently asdf runs only on Linux and Mac, on Windows we can use it with WSL2.
I could write all the steps here but the official website already has a very detailed documentation with all the steps for each operating system and terminal, just click here to access.
Managing versions
asdf can manage versions in two scopes, global, where it changes system-wide and local, where it changes only the directory, remembering that the local scope takes precedence over the global one.
Installing plugins
The plugins are basically the languages, so if you want to install nodejs we have to install the nodejs plugin, some plugins may have some dependency so it is always good to check the plugin repository, for example PHP
depends on the re2c
package .
asdf has a variety of plugins which you can see here.
In this example we will install the nodejs
plugin, in your terminal type:
asdf plugin add nodejs
But calm down, if you were in a hurry and tried to type node
in your terminal you will find that it still doesn’t work, because the plugin doesn’t install the language itself, it basically gives asdf the ability to manage nodejs.
Installing the languages
Now with the nodejs
plugin installed we can then actually install the language.
It is possible to install a specific version of node simply by typing its version, let’s say we want to install version 17.3.0
just type:
asdf install nodejs 17.3.0
Or, to install the latest version:
asdf install nodejs latest
And then, let’s ensure that the latest version is used:
global asdf nodejs latest
With that we have node up and running.
Local Scope
In the example above we installed the latest version of node for the entire system, but let’s say that now we need to install node 14.1.0 for a specific project, as we already have the node plugin installed, we just need to install the necessary version:
asdf install nodejs 14.1.0
And then we go to the project folder and run:
local asdf nodejs 14.1.0
We can see the difference by running the node -v
command inside the folder and outside:
Note that it creates a .tools-versions
file in the directory and if we look we see that it has saved nodejs 14.1.0
inside the file.
Installing multiple languages at once
To make my life easier I usually use the for command, which despite being a simple command solves many things easily.
We create a .txt containing each language that we are going to install, one per line, remembering that it is always good to check the dependencies of each plugin, the ones I will use in this example, to make it easier, will be installed without problem.
Let’s create the asdf-plugins.txt
file with:
cat <<EOT >> asdf-plugins.txt
nodejs
ruby
rust
EOT
Feel free to change languages, remembering that here is a list of all plugins.
And we install with:
for plugin in $(cat asdf-plugins.txt)
of
asdf plugin add $plugin;
asdf install $plugin latest;
global asdf $plugin latest;
done;
This for
will basically go through each line of the asdf-plugins.txt
file and install and configure each language in its latest version, simple isn’t it?
Well, with that you can now test in several languages and test and automate according to your needs.