Node Js Installation
Node Js Installation
js on Ubuntu
three different ways of getting Node.js installed on an Ubuntu
installing nvm, the Node Version Manager, and using it to install and manage multiple versions
of Node.js
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:
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 ~
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:
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:
Verify that you’ve installed the new version by running node with the -v version flag:
node –v
At this point, you have successfully installed Node.js and npm using apt and the NodeSource
PPA.
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:
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 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:
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:
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
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