How To Install Node.
js on Ubuntu
three different ways of getting Node.js installed on an Ubuntu
using apt to install the nodejs package from Ubuntu’s default software repository
using apt with an alternate PPA software repository to install specific versions of
the nodejs package
installing nvm, the Node Version Manager, and using it to install and manage multiple versions
of Node.js
Option 1 — Installing Node.js with Apt from the Default Repositories
you can use the apt package manager
$sudo apt update
Then install Node.js:
sudo apt install nodejs
Check that the install was successful by querying node for its version number:
node –v
In most cases, you’ll also want to also install npm, the Node.js package manager. You can do
this by installing the npm package with apt:
sudo apt install npm
Option 2 — Installing Node.js with Apt Using a NodeSource PPA
First, install the PPA to get access to its packages. From your home directory, use curl to
retrieve the installation script for your preferred version, making sure to replace 16.x with your
preferred version string (if different):
cd ~
curl -sL https://deb.nodesource.com/setup_16.x -o /tmp/nodesource_setup.sh
Inspect the contents of the downloaded script with nano or your preferred text editor:
nano /tmp/nodesource_setup.sh
When you are satisfied that the script is safe to run, exit your editor. Then run the script
with sudo:
sudo bash /tmp/nodesource_setup.sh
The PPA will be added to your configuration and your local package cache will be updated
automatically. You can now install the Node.js package in the same way you did in the previous
section:
sudo apt install nodejs
Verify that you’ve installed the new version by running node with the -v version flag:
node –v
The NodeSource nodejs package contains both the node binary and npm, so you don’t need to
install npm separately.
At this point, you have successfully installed Node.js and npm using apt and the NodeSource
PPA.
Option 3 — Installing Node Using the Node Version Manager
Another way of installing Node.js that is particularly flexible is to use nvm, the Node Version
Manager.
This piece of software allows you to install and maintain many different independent versions of
Node.js, and their associated Node packages, at the same time.
To install NVM on your Ubuntu 20.04 machine, visit the project’s GitHub page. Copy
the curl command from the README file that displays on the main page. This will get you the
most recent version of the installation script.
Before piping the command through to bash, it is always a good idea to audit the script to make
sure it isn’t doing anything you don’t agree with. You can do that by removing the |
bash segment at the end of the curl command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh
the script can be downloaded and executed with the following:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh |
bash
This will install the nvm script to your user account. To use it, you must first source
your .bashrc file:
source ~/.bashrc
Now, you can ask NVM which versions of Node are available:
nvm list-remote
You can install a version of Node
nvm install v14.10.0
You can view the different versions you have installed by listing them:
nvm list
This shows the currently active version on the first line (-> v14.10.0), followed by some
named aliases and the versions that those aliases point to.
Removing Node.js
You can uninstall Node.js using apt or nvm, depending on how it was installed. To remove the
version from the system repositories, use apt remove:
sudo apt remove nodejs
By default, apt remove retains any local configuration files that were created since installation.
If you don’t want to save the configuration files for later use, use apt purge:
sudo apt purge nodejs
To uninstall a version of Node.js that you installed using nvm, first determine whether it is the
current active version:
nvm current
If the version you are targeting is not the current active version, you can run
nvm uninstall node_version
If the version you would like to remove is the current active version, you first need to
deactivate nvm to enable your changes:
nvm deactivate