Installing and Managing Multiple Node.js Versions in Linux

 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.

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/.nvm"

Next, we’ll load NVM for the current shell session:

$ [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"$ [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

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 --version
0.34.0

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)

Installing Spesific Versions

$ nvm install 6.14.4

Using a Specific Node.js Version

$ nvm use lts
$ nvm use 6.14.4

For using a system-installed version, we’d run:

$ nvm use system

Iklan ada di sini

Komentar