How to Install Node.js and npm on Ubuntu 20.04

install node.js and npm on ubuntu 20.04

Node.js is an open-source and cross-platform JavaScript runtime environment built on Chrome’s V8 JavaScript engine. It is designed for non-blocking, event-driven servers and primarily used for traditional web sites and back-end API services. It allows you to build network applications quickly. It makes development more consistent by leveraging JavaScript on both the front and backend. npm is the Node.js registry and package manager. It is used to publish, discover, install, and develop node programs.

In this tutorial, we will show you several ways to install Node.js and npm on Ubuntu 20.04 server.

Prerequisites

  • An Ubuntu 20.04 VPS (we’ll be using our SSD 2 VPS plan)
  • Access to the root user account (or access to an admin account with root privileges)

Log in to the Server & Update the Server OS Packages

First, log in to your Ubuntu 20.04 server via SSH as the root user:

ssh root@IP_Address -p Port_number

You will need to replace ‘IP_Address‘ and ‘Port_number‘ with your server’s respective IP address and SSH port number. Additionally, replace ‘root’ with the username of the admin account if necessary.

Before starting, you have to make sure that all Ubuntu OS packages installed on the server are up to date. You can do this by running the following commands:

apt-get update -y
apt-get upgrade -y

Install Node.js and npm from Ubuntu Repository

The simple and easiest way to install Node.js and npm is to install them from the Ubuntu default repository. However, it does not contains the latest Node.js version. At the time of writing this tutorial, the latest Node.js version available in the Ubuntu 20.04 is 10.19.0.

First, update the system packages by running the following command:

apt-get update -y

Once all the packages are updated, install the Node.js and npm with the following command:

apt-get install nodejs npm -y

Once both packages are installed, verify the Node.js version using the following command:

node -v

You should get the following output:

v10.19.0

You can also check the npm version using the following command:

npm -v

You should get the following output:

6.14.4

Install Node.js and npm from NodeSource

NodeSource maintains an APT repository and contains multiple Node.js versions. It allows you to install the specific version of Node.js in your system.

In this section, we will install Node.js v14.x from the NodeSource.

First, install the curl with the following command:

apt-get install curl -y

Next, download and run the Node.js installation script by running the following command:

curl -sL https://deb.nodesource.com/setup_14.x | bash -

This will add the GPG key and Node.js repository to the APT.

Next, install the Node.js version 14.x by running the following command:

apt-get install nodejs -y

Once installed, verify the installed version of Node.js with the following command:

node -v

You should get the following output:

v14.15.1

You can also verify the npm version with the following command:

npm -v

Yu should get the following output:

6.14.8

Install Node.js and npm with NVM

NVM also know as “Node Version Manager” is a script that allows you to manage multiple version of Node.js.

First, you will need to download and install NVM in your system. You can download and run the script manually with the following command:

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

Once installed, you should get the following output:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 13527  100 13527    0     0   528k      0 --:--:-- --:--:-- --:--:--  528k
=> Downloading nvm as script to '/root/.nvm'

=> Appending nvm source string to /root/.bashrc
=> Appending bash_completion source string to /root/.bashrc
=> 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

Next, you will need to close and reopen the terminal to add the path to nvm script to the current shell session.

Next, verify the NVM version with the following command:

nvm --version

You should get the following output:

0.37.2

You can list all Node.js version that can be installed with NVM using the following command:

nvm list-remote

This command will show you a long list of all Node.js versions.

Next, install the latest stable version of Node.js with the following command:

nvm install node

You should get the following output:

Downloading and installing node v15.3.0...
Downloading https://nodejs.org/dist/v15.3.0/node-v15.3.0-linux-x64.tar.xz...
######################################################################################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v15.3.0 (npm v7.0.14)
Creating default alias: default -> node (-> v15.3.0)

Once the installation is completed, verify the Node.js version with the following command:

node --version

You should get the following output:

v15.3.0

If you want to install the latest LTS version, run the following command:

nvm install --lts

You can now list all installed Node.js versions using the following command:

nvm ls

You should get the following output:

->     v14.15.1
        v15.3.0
default -> node (-> v15.3.0)
node -> stable (-> v15.3.0) (default)
stable -> 15.3 (-> v15.3.0) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/fermium (-> v14.15.1)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.23.0 (-> N/A)
lts/erbium -> v12.20.0 (-> N/A)
lts/fermium -> v14.15.1

If you want to switch the current Node.js version, run the following command:

nvm use 14.15.1

You should get the following output:

Now using node v14.15.1 (npm v6.14.8)

In the above guide, you learned three different ways to install Node.js on Ubuntu 20.04 server. You can now choose your desired way to install the Node.js as per your needs.

install node.js and npm on ubuntu 20.04

Of course, you don’t have to do any of this if you use one of our Node.js VPS Hosting services, in which case you can simply ask our expert Linux admins to setup this for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Leave a Comment

To prove you are human please solve the following *