Working with Multiple NodeJS Versions in Linux

Introduction

Node.js is an open-source and asynchronous event-driven JavaScript runtime used to build scalable network applications. It is cross-platform and can be installed on various platforms such as, Windows, Linux, Unix, Mac OS X, etc. It uses an event-driven, non-blocking I/O model that makes it simple, lightweight and efficient.

In some cases, you may need to install multiple Node.js versions in your system. In that case, you can use Node Version Manager (NVM) to manage and switch between different Node versions with ease. NVM provides a command-line interface that allows you to install different versions, set a default version, switch between them and much more.

In this post, we will show you how to use NVM to manage multiple Node.js versions on Linux.

Requirements

  • A server running Linux operating system.

  • A root password is set up on your server.

Install NVM on Linux

In this section, we will show you how to install NVM on Debian and RPM based distributions.

For Debian and Ubuntu operating systems, follow the below steps to install NVM:

First, install the required dependencies using the following command:

apt-get install gnupg2 curl -y

Next, install the NVM by running the following script:

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash

Once the installation is complete, you should get the following output:

% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 14984 100 14984 0 0 108k 0 --:--:-- --:--:-- --:--:-- 107k => Downloading nvm as script to '/root/.nvm' => Appending nvm source string to /root/.bashrc => Appending bash_completion source string to /root/.bashrc => Close and reopen your terminal to start using nvm or run the following to use it now: export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion

Next, activate the NVM system path using the following command:

source ~/.bashrc

Now, verify the NVM installation using the following command:

nvm --version

Sample output:

0.38.0

For RHEL and CentOS operating systems, follow the below steps to install NVM:

First, install the required dependencies using the following command:

dnf install curl

Next, install the NVM by running the following script:

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash

Next, activate the NVM system path using the following command:

source ~/.bashrc

Now, verify the NVM installation using the following command:

nvm --version

Sample output:

0.38.0

You can see NVM help information using the following command:

nvm --help

Install Multiple Versions of Node.js

NVM provides an easier way to install a specific Node.js version with ease.

To install the latest version of Node.js, run the following command:

nvm install node

Sample output:

Downloading and installing node v16.9.1... Downloading https://nodejs.org/dist/v16.9.1/node-v16.9.1-linux-x64.tar.xz... ################################################################################################################# 100.0% Computing checksum with sha256sum Checksums matched! Now using node v16.9.1 (npm v7.21.1) Creating default alias: default -> node (-> v16.9.1)

To install the latest stable version of Node.js, run the following command:

nvm install --lts

Sample output:

Installing latest LTS version. Downloading and installing node v14.17.6... Downloading https://nodejs.org/dist/v14.17.6/node-v14.17.6-linux-x64.tar.xz... ############################################################################################################################ 100.0% Computing checksum with sha256sum Checksums matched! Now using node v14.17.6 (npm v6.14.15)

To install the specific Node.js version like v14.15.4, run the following command:

nvm install v14.15.4

Sample output:

Downloading and installing node v14.15.4... Downloading https://nodejs.org/dist/v14.15.4/node-v14.15.4-linux-x64.tar.xz... ############################################################################################################################# 100.0% Computing checksum with sha256sum Checksums matched! Now using node v14.15.4 (npm v6.14.10)

Switch Between Node.js Versions

To verify the active Node.js version, run the following command:

node --version

Sample output:

v14.15.4

If you want to switch to the latest Node.js version, run the following command:

nvm use node

Sample output:

Now using node v16.9.1 (npm v7.21.1)

To switch to the latest LTS version, run the following command:

nvm use --lts

Sample output:

Now using node v14.17.6 (npm v6.14.15)

If you want to run a command directly for an installed version without switching the node variable, run:

nvm run default --version

Sample output:

Running node v16.9.1 (npm v7.21.1) v16.9.1

List Node.js Versions

If you want to list all installed Node.js versions, run the following command:

nvm ls

Sample output:

v14.15.4 -> v14.17.6 v16.9.1 default -> node (-> v16.9.1) iojs -> N/A (default) unstable -> N/A (default) node -> stable (-> v16.9.1) (default) stable -> 16.9 (-> v16.9.1) (default)

To display the current Node.js version, run the following command:

nvm current

Sample output:

v14.17.6

To list all available Node.js versions, run the following command:

nvm ls-remote

You should get the following output:

-> v14.17.6 (Latest LTS: Fermium) v15.0.0 v15.0.1 v15.1.0 v15.2.0 v15.2.1 v15.3.0 v15.4.0 v15.5.0 v15.5.1 v15.6.0 v15.7.0 v15.8.0 v15.9.0 v15.10.0 v15.11.0 v15.12.0 v15.13.0 v15.14.0 v16.0.0 v16.1.0 v16.2.0 v16.3.0 v16.4.0 v16.4.1 v16.4.2 v16.5.0 v16.6.0 v16.6.1 v16.6.2 v16.7.0 v16.8.0 v16.9.0 v16.9.1

Run Application with Specific Version

If you have multiple applications on your system and want to run each application with a specific version of node.js then you can use the option to use a node.js version for any application.

For example, to run app.js with Node.js version v14.15.4, run the following command:

nvm run v14.15.4 app.js

You can also use exec to run a command on a sub-shell:

nvm exec 14.15.4 node --version

Sample output:

Running node v14.15.4 (npm v6.14.10) v14.15.4

To find the path of the Node.js executable of a specific version, run the following command:

nvm which 14.15.4

Sample output:

/root/.nvm/versions/node/v14.15.4/bin/node

Remove Node.js Version

First, list all install Node.js versions using the following command:

nvm list

Next, remove any unused version from your system using the following command:

nvm uninstall 14.15.4

Sample output:

Uninstalled node v14.15.4

Conclusion

In the above guide, you learned how to use NVM to install and manage multiple Node.js versions. I hope you can now run your application with any Node.js versions.


Was this page helpful?

Thank you for helping us improve!