How to Install Node.js and NPM on Ubuntu 20.04

Today we are going to tell you about How to install Node.js and NPM on Ubuntu, Node.js is a cross-platform javascript that is used to make back-end applications. Node.js is built on Chrome’s Javascript and is designed to execute Javascript code on the server-side. It is also popular for full-stack and front-end solutions. npm is the default package manager for Node.js with the world’s largest software registry. WE will tell you about the three different ways to install Node.js and npm on Ubuntu 20.04.

  • From the standard Ubuntu repositories
  • From the NodeSource repository
  • Using nvm (Node Version Manager)

It’s up to you to choose the installation method that will be appropriate for your environment. If you are not sure which one will be right for you then consult the documentation of the application you’re going to deploy.

Install Node.js and npm from the Ubuntu repository

The installation is pretty easy. Run the following commands to update the package index and install Node.js and npm:

$ sudo apt update
$ sudo apt install nodejs npm

This command will install a number of packages, including the necessary tools that you need to compile and install native addons from npm.

Once you are done, verify the installation by running:

$ node -v 
v10.19.0

Installing Node.js and npm from NodeSource

NodeSource is a company that is focused on providing enterprise-grade Node support. NodeSource maintains an APT repository containing multiple Node.js versions. You can use this repository if your application requires a specific version of Node.js.

Run the following command to download and execute the NodeSource installation script:

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

The script will add the NodeSource signing key to your system, install all the necessary packages, and refresh the apt-cache.

If you want another version of Node.js change the setup_14.x, for example with setup_12.x.

Once the NodeSource is enabled, install Node.js and npm:

sudo apt install nodejs

The nodejs package contains both the node and npm binaries.

Verify that the Node.js and npm were successfully installed by printing their versions:

  1. node -v
    v14.2.0
    npm --version
    6.14.4

To be able to compile native addons from npm you’ll need to install the development tools:

sudo apt install build-essential

Installing Node.js and npm using NVM

NVM (Node Version Manager) is a bash script that allows users to manage multiple Node.js versions on a per-your basis. With the help of NVM ou can install and uninstall any Node.js version that you want to use or test.

Visit the nvm GitHub repository page and copy either the curl or wgeta command to download and install the nvm script:

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

Do not use sudo ’cause it will enable nvm for the root user.

The script will clone the project’s repository from Github to the ~/.nvm directory:

=> 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

As the above output says, you can either close and reopen the terminal or run the commands to add the path to nvm script to your current shell session. You can do as it suits you and is easy for you.

Once the script is in your PATH, verify that nvm was properly installed by typing:

nvm --version
0.35.3

To get the list of all Node.js versions that can be installed with nvm, run:

nvm list-remote

The command will print a huge list of all available Node.js versions.

To install the latest available version of Node.js, run:

nvm install node

The output should look something like this:

...
Checksums matched!
Now using node v14.2.0 (npm v6.14.4)
Creating default alias: default -> node (-> v14.2.0)

Once the installation is completed, verify it by printing the Node.js version:

node --version
v14.2.0

Now let’s install two more versions, the latest LTS version, and version 10.9.0:

nvm install --ltsnvm install 10.9.0

You can list the installed Node.js versions by typing:

nvm ls

The output should look something like this:

>      v10.9.0
       v12.16.3
        v14.2.0
default -> node (-> v14.2.0)
node -> stable (-> v14.2.0) (default)
stable -> 14.2 (-> v14.2.0) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/erbium (-> v12.16.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.20.1 (-> N/A)
lts/erbium -> v12.16.3

The entry with an arrow on the right (> v10.9.0) is the Node.js version used in the current shell session and the default version is set to v14.2.0. The default version is the version that will be active when you open new shells.

If you want to change the currently active version enter:

nvm use 12.16.3
Now using node v12.16.3 (npm v6.14.4)

To change the default Node.js version, run the following command:

nvm alias default 12.16.3

To get more detailed information about how to use the nvm the script, you can visit the project’s GitHub page.

We have told you three different ways How you can install Node.js and npm on your Ubuntu 20.04 server. Now it’s up to you to choose the right method to download Node.js as per your requirements and preference.

Leave a Comment

Our Products