This tutorial helps you to install and Use multiple Node.js versions on a single Linux machine. In this tutorial, we use the Node Version Manager (NVM) tool for the installation of Node.js.
NVM Installation
NVM install and Manage Node.js version in the user’s home directory. So you don’t need any root-level privileges. Simply login as a non-root user and follow tutorial.
The NVM team provides a Bash script for the installation of NVM on the Linux system. You can simply execute this script on your machine to install NVM.
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
The installer makes an entry to the user’s default profile file. You need to reload the user’s environment using one of these commands. It will set the required environment variables to use nvm on the system.
source ~/.profile ## Debian based systems source ~/.bashrc ## CentOS/RHEL systems
Search Available Node.js Version’s
At this point, you have installed nvm on your system for the current user. Now find out the available version of Node.js to install. Use ls-remote
option to list versions.
nvm ls-remote
You will see a long list of available Node.js versions. You can install any of the listed Node.js versions.
... ... v12.12.0 v12.13.0 (LTS: Erbium) -> v12.13.1 (Latest LTS: Erbium) v13.0.0 v13.0.1 v13.1.0 v13.2.0
Install Multiple Node.js Versions
Now install the node.js version you need to use for running node.js application. Below command will install node.js v12.13.1 the LTS release on your system.
nvm install v12.13.1
You can have also installed the latest version of Node.js.
nvm install v13.2.0
Repeat the above command with the different-2 node.js versions to install multiple versions of node.js on your system. For this tutorial, I have installed both of the above Node.js version and one more old LTS version below:
nvm install v10.17.0
Now, I have installed 3 Node.js versions on my system.
Set Node.js Default Version
As you have installed multiple node.js versions, You can select the specific version of node.js as default version used by system and load in the environment. Use below command to list currently installed version and default set version.
nvm list -> v10.17.0 v12.13.1 v13.2.0 system default -> v12 (-> v12.13.1) node -> stable (-> v13.2.0) (default) stable -> 13.2 (-> v13.2.0) (default) iojs -> N/A (default) unstable -> N/A (default) lts/* -> lts/erbium (-> v12.13.1) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.16.2 (-> N/A) lts/dubnium -> v10.17.0 lts/erbium -> v12.13.1
You can see that version 10.17.0 is set as the default Node.js version. Now, change the default Node.js version to 12.13.1.
nvm use v12.13.1
Now verify the currently active version of Node.js. Using the same command you can switch to any Node.js version any time as per your requirements.
node --version v12.13.1
But still, there is a question. How can we run two or more applications with different Node.js versions at the same time? Not to worry, read the next step:
Runing Application with Specific Version
You want to run multiple Node.js applications on your system and want to run each with a specific version of node.js. NVM provides you an option to do this. You can use any installed node.js version for running a Node.js application. For example:
nvm run v10.17.0 app.js
The app.js will run with Node.js version 10.17.0, It doesn’t matter what is the default version is set on your system.
Remove Unused Node.js Version
This command will provide a list of installed versions of node.js on your system.
nvm list
Now remove any version installed on your system using the following command. The below command will remove Node.js version 10.16.3 from your system.
nvm uninstall v10.17.0
Conclusion
This tutorial helps you with the installation of multiple Node.js versions on the Linux machine. Follow our next tutorial to deploy Node.js application with PM2. The PM2 helps you to keep any Node.js application run. In case the application goes down for any reason PM2 will start it automatically.