Linux: Manage Multiple Versions of Node.js with the NVM Manager


Node.js is a crucial development tool for cloud native development. Because Node.js is very resource-efficient and high performing, it’s perfectly suited for apps and services that need to function at a larger scale. With Node.js, you can develop SaaS applications, such as those used by Netflix, Uber, and Linkedin.

You probably already know what Node.js is. For those of you who do not, it’s an open source server software that runs on almost every platform. An asynchronous, event-driven JavaScript runtime, Node.js is ideal for building scalable network apps and services. Node.js can be used to create very basic applications (such as the always fun “Hello, World” app) to very complex applications.

A sample Node.js web server that serves up the Hello World app might look like this:

However, during your career with Node.js, there’s one issue you might eventually run into… that of having to work with more than one version of Node. Why is this an issue? Sometimes you might need to build an app that works with an older version of Node.js and other times you might need the new features found in more recent iterations.

That can be a challenge because your operating system might not like the idea of installing more than one version of the language.

Fortunately, there’s a way around that. If your development platform of choice is Linux, you can always turn to the Node Version Manager (aka NVM). With NVM on board, you can easily switch between multiple versions of Node.js on a per-project basis, making certain you have all your bases covered.

Let me show you how to install NVM on Linux. I’ll demonstrate on Ubuntu Server 22.04, which means all you’ll need is a running instance of that distribution (or any Debian-based distro) and a user with sudo privileges. You don’t even need Node.js installed, as you can take care of that task once you have NVM installed.

With that said, let’s get to the steps.

Update/Upgrade

Before we install NVM, let’s first make sure our operating system (and the included software) is up to date.

Log in to your Linux instance (which can be on bare metal or a cloud-hosted service) and update apt with the command:

Once apt is updated, run the upgrade with:

Should the kernel be upgraded in the process, you’ll need to reboot the server so the changes take effect.

Install NVM

Now, let’s install NVM. To that, you’ll first need to make sure to install two dependencies with the command:

With the dependencies installed, download and run the installer script with the command:

Once the installation is complete, log out and log back in again so everything required is added to your .bashrc file. After logging back in, activate the settings with:

You can now verify the installation with the command:

You should see something like this:

How to Use NVM

Now that NVM is installed, it’s time to use it. The first thing you’ll want to do is install the most recent version of Node.js with the command:

When the installation completes, you should see something like this in the output:

You are now using version 18.10.0 of Node.js.

Let’s install a different version. How about the LTS (Long Term Support) version of Node.js? That’s done with the command:

Let’s say you have a project that requires a version of Node.js that is not the latest version. First, you can find out which versions of Node.js are currently installed with the command:

You should see something like this in the output:

To view a list of all available versions, issue the command:

The output of that command will list a considerable number of versions. Let’s say you have a project that requires version 17.9.1 of Node.js. To use that version, issue the command:

Once installed, you can switch to it with the command:

The output of the above command should be:

And there you go, you can now easily install and switch between different versions of Node.js on your Linux machine. For any developer that requires different versions of the language, the NVM tool will be absolutely invaluable for your cloud native development process.

Group Created with Sketch.



Source link

Share

Leave a Reply

Your email address will not be published. Required fields are marked *