Copy from: https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-20-04
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
Copy
Review the script and make sure you are comfortable with the changes it is making. When you are satisfied, run the command again with | bash
appended at the end. The URL you use will change depending on the latest version of nvm, but as of right now, 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
Copy
This will install the nvm
script to your user account. To use it, you must first source your .bashrc
file:
Copy
Now, you can ask NVM which versions of Node are available:
Copy
Output. . .
v18.0.0
v18.1.0
v18.2.0
v18.3.0
v18.4.0
v18.5.0
v18.6.0
v18.7.0
v18.8.0
v18.9.0
v18.9.1
v18.10.0
v18.11.0
v18.12.0 (LTS: Hydrogen)
v18.12.1 (LTS: Hydrogen)
v18.13.0 (Latest LTS: Hydrogen)
v19.0.0
v19.0.1
v19.1.0
v19.2.0
v19.3.0
v19.4.0
It’s a very long list. You can install a version of Node by writing in any of the release versions listed. For instance, to get version v14.10.0, you can run:
Copy
You can view the different versions you have installed by listing them:
Output-> v14.10.0
v14.21.2
default -> v14.10.0
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v14.21.2) (default)
stable -> 14.21 (-> v14.21.2) (default)
. . .
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.
Note: if you also have a version of Node.js installed through apt
, you may receive a system
entry here. You can always activate the system-installed version of Node using nvm use system
.
Additionally, there are aliases for the various long-term support (or LTS) releases of Node:
Outputlts/* -> lts/hydrogen (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.21.2
lts/gallium -> v16.19.0 (-> N/A)
lts/hydrogen -> v18.13.0 (-> N/A)
You can install a release based on these aliases as well. For instance, to install the latest long-term support version, hydrogen
, run the following:
Copy
OutputDownloading and installing node v18.13.0...
. . .
Now using node v18.13.0 (npm v8.19.3)
You can switch between installed versions with nvm use
:
Copy
OutputNow using node v14.10.0 (npm v6.14.8)
```
You can verify that the install was successful using the same technique from the other sections:
```command
node -v
The correct version of Node is installed on your machine as expected. A compatible version of npm
is also available.