Newsletter Subscribe
Enter your email address below and subscribe to our newsletter
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:
I will also be including the removal method for every installation listed so you can have a complete solution.
Table of Contents
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
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:
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
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.
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
As you can see, it gave me node.js version 20.12.2 (while writing this article).
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
While writing, it gave me node.js version 22.0 (a nightly build).
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
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:
curl -fsSL https://deb.nodesource.com/setup_21.x | sudo -E bash - &&\
sudo apt-get install -y nodejs
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - &&\
sudo apt-get install -y nodejs
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - &&\
sudo apt-get install -y nodejs
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
There you go!
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
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
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:
Next, you can list the available version of node.js using the following command:
nvm list-remote
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
You can also install multiple versions using the same command. I installed version 21 right after installing the version 18:
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
Pretty neat. Isn’t it?
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>
Next, uninstall node.js that you installed using NVM using the following:
nvm uninstall <version>
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
Make sure to save changes before quitting the nano editor. Yes, that’s all it takes to remove NVM from Ubuntu.
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.