Akercode.com

How To Install Node.js and npm on Ubuntu 20.04

This How-To describes how to set up Node.js and npm with Node Version Manager on Ubuntu 20.04.

Node Version Manager (NVM) is a tool that allows programmers to switch between different versions of Node.

Install NVM

Download and install the nvm script:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

From the message below, close and reopen the terminal or run the export command for the changes to take effect.

Output:

=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Check installed version:

nvm --version

Output:

0.35.3

Install Node.js

There are different ways to install the different versions.

LTS is recommended for the most stable release.

Node.js LTS

To install the long term supported version:

nvm install --lts

Node.js latest

To install the latest version of Node.js:

nvm install node

Node.js version x.x.x

To install a specific version of Node.js:

nvm install 12.0.0

To check the current node version:

node --version

Output:

v14.17.3

If you have installed multiple versions you can list them by typing:

nvm ls

Output:

->      v12.0.0
        v14.17.3
        v16.5.0
default -> lts/* (-> v14.17.3)
node -> stable (-> v16.5.0) (default)
stable -> 16.5 (-> v16.5.0) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/fermium (-> v14.17.3)
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.3 (-> N/A)
lts/fermium -> v14.17.3

To change the currently active version type:

nvm use 14.17.3

Output:

Now using node v14.17.3 (npm v6.14.13)