When working with NodeJS, sometimes there will be version specific issues. Even if there are none you might want to experiment with certain node versions. And manually doing so will take your precious time. For that reason, here is a brief tutorial on how you can use nvm to install and manage multiple NodeJS version in your Linux distro.
What you must already know ?
Before we can dig in with the process. You must be familiar with the command line interface commonly known as shell. I am on Ubuntu so I will be using the bash shell to perform all of the commands.
If you are not comfortable with the shell this might be a little tricky at first. But trust me it’s really very easy if you follow along.
Also installation requires curl being installed in your device. Open the terminal (
t ) and type in the
curl command. If you don’t see the same output as below. Install it with
sudo apt update && sudo apt install curl.
Or if you don’t want to use
wget instead you can do so. Verify you have
wget installed by typing the command in terminal.
If you see something else you can install it using
sudo apt update && sudo apt install wget.
Installing the nvm-sh
Once you have verified the existence of the
curl binary in your system. We can proceed with the installation.
Open your terminal and run the following command.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.36.0/install.sh | bash
Or if you prefer
wget run the command below
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.36.0/install.sh | bash
But make sure the v0.36.0 is the latest version from GitHub repository, here.
After successful installation the output should look something like
But we are not done yet. We need to add the path to our
$PATH variable. It is already added let us just double check it and make sure.
If you don’t see something like this when you run nvm
--version. Then read below.
Also Read : Learn Interesting Uses of ‘ls’ command in LinuxTECHENUM
Exporting the NVM_DIR to PATH variable
If you look closely to the output of the installation command. You can see that it tells us to add some values to
How do we do that ?
Open the the file
~/.bashrc with your favorite text editor. Let me use
nano for the sake of this blog post.
Add the following snippet at the end of the file if it doesn’t exist already
We are not done yet. We need to reflect the changes in our terminal without closing it.
Run the command below to refresh the environment variables.
We are all set now let us confirm it actually worked by tying
nvm --version. This command should output the version.
Installing a specific NodeJS version
Let us install the latest LTS version of NodeJS, which at the time of writing this post is 12.19.0.
You can install node using nvm install <version>. See image for example and context
Once you have installed the desired version. You can install any other version as much as you like following the same command.
Lastly let’s look at some useful commands for nvm-sh.
Toggling NodeJS versions using nvm-sh
You are now able to install any NodeJS version that is available.
To change to specific installation version you can use
nvm use <version> # where version is any version that you installed
You can also switch to the latest installed LTS version using the following command
nvm use --lts
Uninstalling NodeJS using the nvm-sh
Another important command you must remember is for un-installation.
You can simply run the command below to uninstall the NodeJS.
nvm uninstall <version> # replace version with your desired version
Reinstall packages for current NodeJS version
Finally you can use the command below to reinstall the packages for specific version.
If after switching the version name you run into some issues you might want to try this.
nvm reinstall-packages <version> # replace version with your specific version
That’s it for this post. I have run across the problem of not being able to manage multiple NodeJS. Which was solved by nvm-sh.
So here I have shared on how to use nvm to install and manage multiple NodeJS version.
Also Read : 5 Productivity Tips and Tricks for UbuntuTECHENUM