In this article, we’ll use a tool called Node Version Manager. It’s a shell script that is specifically designed to assist us in automating the installation and management of multiple Node.js versions. We’ll begin by installing NVM, and then we’ll cover the installation of different Node.js versions.
Afterward, we’ll take a look at using a specific Node.js version and setting a default Node.js version for a new shell.
2. Node Version Manager
Node Version Manager is an open-source shell utility we can use to manage multiple Node.js versions on a single machine.
Of course, we can install multiple Node.js versions without the use of any utility whatsoever. However, it’s a tedious task to install and maintain these versions. Not only that, but it can also get messy over time due to dependency conflicts.
That’s why NVM exists. NVM reduces this overhead by automating this whole process.
2.1. NVM Installation
By default, NVM is not available in the package repositories. Therefore, we’ll need to manually download the installation script and execute it:
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
After the installation, the setup will print out the directory for NVM installation. At this point, we can either restart our terminal or export the NVM directory:
$ export NVM_DIR="$HOME/.local/share/nvm"
Next, we’ll load NVM for the current shell session:
$ [ -s "$NVM_DIR" ] && \. "$NVM_DIR/nvm.sh"
We should know that if we’re using a shell other than bash, we’ll need to put these two commands in our shell profile. So, we wouldn’t need to repeat this process whenever we open a new terminal instance.
Now, let’s verify that NVM is successfully installed:
$ nvm --help 0.34.0
2.2. Installing the Latest Node.js Version
Now that we have NVM installed, we’ll simply install the latest version of Node.js:
$ nvm install node Downloading and installing node v18.3.0... ... Now using node v18.3.0 (npm v8.11.0) Creating default alias: default -> node (-> v18.3.0)
As we can see, this is going to be our default Node.js version. Let’s test it out:
$ node -v v18.3.0
2.3. Installing Multiple Versions
Similarly, we can install as many different versions of Node.js as we like. We simply pass the version to the install sub-command:
$ nvm install 6.14.4
Additionally, we can install the latest LTS version without specifying the exact version of it:
$ nvm install --lts
We can also list the installed versions using the ls sub-command:
$ nvm ls -> v6.14.4 v18.3.0 default -> node (-> v18.3.0) node -> stable (-> v18.3.0) (default) stable -> 18.3 (-> v18.3.0) (default) iojs -> N/A (default) unstable -> N/A (default)
In the same way, we can also list the versions available for installation:
$ nvm ls-remote
2.4. Using a Specific Node.js Version
As we saw earlier, NVM will set the first installed version as the default. We can change the default version using the use sub-command:
$ nvm use lts
$ nvm use 6.14.4
For using a system-installed version, we’d run:
$ nvm use system
2.5. Removing a Node.js Version
We can use the uninstall sub-command to uninstall a specific Node.js version. For instance, if we want to remove the latest LTS version, we’ll pass —lts:
$ nvm uninstall --lts
2.6. Default Node.js Version
We can also use a default Node.js version:
$ nvm alias default node default -> node (-> v18.3.0)
When we set a default Node.js version, we can use that version in new shell instances.
2.7. Migrating Global Packages
Whenever we switch to a different version, the global packages we’ve installed through npm will remain intact. However, some of these packages might not work with our current version.
For that reason, we’ll need to install Node.js alongside the global packages:
$ nvm install node --reinstall-packages-from=node
The command will first install Node.js and then re-install the npm packages from the prior version of Node.js.
In the same way, we can also specify which version of Node.js to install the packages from:
$ nvm install lts --reinstall-packages-from=16
In this article, we discussed how we can install and manage multiple versions of Node.js on a single Linux machine. We covered the use of the Node Version Manager utility to install different Node.js versions.
Apart from that, we also covered the different operations such as removing, using, and migrating global npm packages to the current version from a previous version.