Installing multiple versions of Node.js has become a necessity due to the fact that many libraries and apps depend on specific versions of Node.js. Unfortunately, many require a newer version of Node.js than the one used by your app, or in some cases an older one. This means that you may need to run one app with Node version 12.13.0 for example, while another might have to be run with Node version 20 or higher. This problem is easily solved using the Node Version Manager (NVM).
How To Install Node Version Manager On Linux
In order to run multiple versions of Node.js on your Linux machine, install Node Version Manager using the following commands. Change ‘v0.39.5’ to your preferred version (or the latest version of NVM available):
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh
The output of that command shows changes that will be made if that shell script is executed. Please read it and proceed to execute the following commands if you are ok with the proposed changes.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash source ~/.bashrc
How To Install Multiple Versions Of Node.js
In order to run alternate versions of Node.js, you have to install them first. This can be done with the ‘nvm install [version]’ command.
List the versions of Node.js you can install with the following command:
nvm ls-remote
It will return a long list of Node versions you can install. Scroll through and copy the version number you need, then run the nvm install [version] command as shown in the example below:
nvm install v20.2.0
If you’d like to install v12.13.0 as well, feel free. Install as many versions as you need!
nvm install v12.13.0
How To Run Multiple Versions Of Node.js
The Node Version Manager can be used instead of the ‘node’ command to run your app with the version of Node.js it needs. It can also be used to install alternate versions of Node.js. You can have several versions of Node.js installed on your machine if you use NVM and simply pick which one you want to run each app with.
For example: You can run your app like this:
nvm run 12.13.0 app.js
The output would look something like this:
Running node v12.13.0 (npm v6.12.0)
What if there’s another app that requires Node version 20.2.0? You don’t have to delete 12.13.0, just run version 20.2.0 like this:
nvm run 20.2.0 app2.js
How To Change The Default Version Of Node.js
If you want to change the default version of Node.js instead of typing which version you want to run each time, you can set it using the ‘nvm use‘ command, but first type ‘node -v‘ to see which version is currently the default.
The following example shows how to change the default version of Node.js to v12.13.0, so that it will be used whenever you run the ‘nodejs‘ command:
nvm use v12.13.0
Run ‘node -v’ afterwards to confirm that it has switched to your preferred version.
Note that whenever you install a new version of Node.js using NVM, it will set the last version you installed as the default. That means that you’ll need to switch back to your preferred default with the ‘nvm use’ command shown above.
How To Uninstall A Specific Version Of Node.js
You can use NVM to uninstall a specific version of Node.js using the ‘nvm uninstall [version]’ command, for example:
nvm uninstall v10.24.1
The output from that command should look something like this:
Uninstalled node v10.24.1