Enter your email address below and subscribe to our newsletter

How to Install Node.js and npm on Ubuntu 22.04

Learn how to install node.js and npm on Ubuntu with multiple methods

Share your love

If you are into web development and moved to Ubuntu recently, you may wonder how to install node.js and npm on Ubuntu. Apart from web development, node.js is also a crucial dependency for other Linux packages.

So in this tutorial, I will walk you through 4 different ways you can install node.js on Ubuntu:

  1. Using the default package manager (apt): It is the easiest method and recommended to beginners, but it will get you a little older version for stability reasons.
  2. Using snap: it is the easiest way to get the most recent version of node.js without any additional configuration.
  3. Using NodeSource repository: A slightly complex method but lets you choose from various available versions, including the most recent one.
  4. Using NVM (Node Version Manager): It lets you keep multiple versions of node.js and does not require sudo privileges, which is a plus when dealing with multiple users.

I will also be including the removal method for every installation listed so you can have a complete solution.

Method 1: Use apt to install Node.js and npm

By far this is the easiest method to install node.js and npm on Ubuntu 22.04.

But why? Well, all it takes is 2 commands and you’re done. There’s a catch! Ubuntu 22.04 is an LTS release, so every package included in its repository should be stable as it will be mostly used on production servers.

See also: How to Boot into Rescue/Emergency Mode in Ubuntu 20.04 LTS

So it requires thorough testing, and the included packages are a bit behind the latest release. If that’s not an issue, then you can follow this method without any problem.

To install any package using apt, it is important to update the repository index using the given command:

sudo apt update

Once you are done updating the system (no need to perform a system upgrade), you can use the following command to install npm and node.js on Ubuntu:

sudo apt install nodejs npm
install nodejs and npm in Ubuntu using apt

To verify the installation, you can check the installed version of npm and node.js by executing the given commands one by one:

node --version
npm --version

The output of the above two commands should look like this:

Check the installed version of npm and node.js in Ubuntu

How to uninstall

If you followed the above instructions to install node.js and npm through apt, and now you want to uninstall the package, then it can be removed using the apt remove command in the following manner:

sudo apt remove nodejs npm

But if you intend to remove binary packages and configuration along with the package itself then, you use the apt purge instead of apt remove in the following manner:

sudo apt purge nodejs npm

Method 2: Use snaps to install Node.js

Ubuntu comes pre-configured with snaps. Another good thing is you can choose between the stable and the bleeding-edge release while installing node.js using Snaps.

I will share commands for both the stable and the edge release of the node.js so you can choose what suits the best to your workflow.

For stable release:

In my opinion, for most users, getting a stable release will get the job done as it is thoroughly tested and the possibilities of crash are minimal. To install the stable release of node.js using snaps, use the given command:

sudo snap install node --classic
Install node.js using snaps stable channel

As you can see, it gave me node.js version 20.12.2 (while writing this article).

For the edge release:

I won’t recommend getting an edge release for the general audience as it might break in the middle of nowhere, and you have to start from scratch.

To install the edge release of the node.js, use the following command:

sudo snap install node --channel=latest/edge --classic
Install edge release of node.js in ubuntu

While writing, it gave me node.js version 22.0 (a nightly build).

How to uninstall

If you installed node.js using snaps, it can be removed using a single command. And yes, the command remains the same even if you installed the stable or edge release:

sudo snap remove node
Remove snap version of node.js from ubuntu

Method 3: Use the NodeSource repository to install Node.js

If you want to get the most recent version of node.js, then you can refer to this method, where I will be showing you how you can use the official NodeSource repository for installation purposes.

The best part of using this method is it provides a one-step installation of your preferred node.js version!

While writing, node versions 18x, 20x, and 21x are supported, and I highly recommend only going with those 3 versions or else you’ll get the Node.js script deprecation notice error.

In simple terms, I will get you commands to all of the 3 maintained versions so you can choose between them and save yourself from the error!

But before that, you’d need to install curl as it serves as a key requisite, and here’s what you need to execute to install curl on Ubuntu:

sudo apt update && sudo apt install curl

Once done, you can use any of these 4 commands to get a specific version of node.js:

  • For node.js v21.x:
curl -fsSL https://deb.nodesource.com/setup_21.x | sudo -E bash - &&\
sudo apt-get install -y nodejs
  • For node.js v20.x:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - &&\
sudo apt-get install -y nodejs
  • For node.js (v20.x) LTS (Long Term Support):
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - &&\
sudo apt-get install -y nodejs
  • For node.js v18.x:
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - &&\
sudo apt-get install -y nodejs

I went for the node.js v21.x so when I checked the installed version, it gave me this output:

node --version 
Check the installed version of node.js after installing the latest version

There you go!

How to uninstall

If you used the NodeSource repository to install node.js, then you’d need to remove the GPG keys and the repository itself for the complete removal. Sounds complex? Let me help.

First, remove the packages using the given command:

sudo apt purge nodejs && sudo apt autoremove -y

Next, remove the NodeSource repository:

sudo rm -r /etc/apt/sources.list.d/nodesource.list

Finally, remove the GPG keys from the /usr/share directory using the following:

sudo rm -r /usr/share/keyrings/nodesource.gpg

Method 4: Use Node Version Manager to install Node.js

If you’re a developer and have to use multiple versions of node.js for the dependency reasons, then this is the perfect method for you. The only down-side is it requires a little more effort, but lets you use node.js without elevated privileges (sudo).

The first step is to install curl in Ubuntu using the given command:

sudo apt update && sudo apt install curl -y

Next, install the Node Version Manager using the following curl command (will take care from cloning to installation, all at once):

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Install Node Version Manager to install node.js in Ubuntu

Once you are done with the installation, restart the terminal and check the status of NVM using the given command:

command -v nvm

It should return nvm as an output as shown here:

Check the status of NVM

Next, you can list the available version of node.js using the following command:

nvm list-remote
List available versions of node.js

Note: It will list every version, including deprecated ones. So, I’d highly recommend only going with the currently supported versions of node.js of Ubuntu. While writing, version 18x, 20x, and 21x are supported.

Once you choose the version, enter the version number in the given command, and it will be installed shortly:

nvm install <version-number>

Let’s say I want to install node.js version 18, so I’ll be using the following command:

nvm install 18
Install a specific version of node.js using NVM in ubuntu

You can also install multiple versions using the same command. I installed version 21 right after installing the version 18:

Install multiple version of node.js in Ubuntu

But the question is: How you switch between them? It uses the most recently installed version but having multiple versions at once you may want to switch between back and forth.

To switch between the multiple versions, you can enter the version number to switch within the following command:

nvm use <version-number>

I wanted to use the node.js version 18.20.2 so I used the following command:

nvm use 18.20.2
Switch between multiple versions of node.js in Ubuntu using NVM

Pretty neat. Isn’t it?

How to uninstall

If you wish to uninstall the NVM and installed versions of node.js, then here’s how you do it.

First, deactivate the currently active version of node.js using the following command:

nvm deactivate <version>
Deactivate the active version of node.js

Next, uninstall node.js that you installed using NVM using the following:

nvm uninstall <version>
Uninstall node.js using NVM

To remove the NPM, you need to remove the NPM directory which can be done using through the given command:

rm -rf "$NVM_DIR"

The final step is to remove a line from the bashrc file which is responsible for loading the node.js. For that purpose, first, open the bashrc file using the given command:

nano ~/.bashrc

Now, look for the following line in the file (mostly found at the end of the file) and remove it:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[[ -r $NVM_DIR/bash_completion ]] && \. $NVM_DIR/bash_completion
remove node.js and NVM from ubuntu completely

Make sure to save changes before quitting the nano editor. Yes, that’s all it takes to remove NVM from Ubuntu.

Conclusion

In this tutorial, I explained how to install node.js and npm on Ubuntu 22.04 using multiple methods, including their removal steps.

If you were to ask me, I’d still go with the first method, utilizing apt for installing node.js and npm. Being a little behind on the latest release is acceptable as long as there’s no threat of instability. But that’s me.

Share your love
Kabir
Kabir

A tech journalist whose life revolves around networks.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!