How to Install Node.js and NPM On Ubuntu 20.04 - TechvBlogs

How to Install Node.js and NPM On Ubuntu 20.04

Node.js is a JavaScript runtime for server-side programming. It allows developers to create scalable backend functionality using JavaScript.


Suresh Ramani - Author - TechvBlogs
Suresh Ramani
 

3 years ago

TechvBlogs - Google News

Node.js is a powerful JavaScript runtime. It’s a free and open-source cross-platform solution that’s primarily for server-side programming. It allows developers to have scalable backend functionality using JavaScript. Most of the time, it’s used to create back-end apps. However, it’s also popular for full-stack and front-end solutions. npm is the default package manager for Node.js and the world’s largest software registry.

In this blog, we will show you three different ways of getting Node.js installed on an Ubuntu 20.04 server:

  • using apt to install nodejs package from Ubuntu's default software repository.
  • using apt with an alternate PPA (Personal Package Archive) software repository to install specific versions of the nodejs package.
  • installing nvm, the Node Version Manager, and using it to install and manage multiple versions of Node.js.

Option 1: Installing Node.js with Apt from the Default Repositories

The recommended way to install Node.js on your Ubuntu 20.04 LTS is by using the apt command to install the stable default Node.js version from the standard Ubuntu Repository:

sudo apt install nodejs

Once installed, check the node.js version:

node -v

How to Install Node.js and NPM On Ubuntu 20.04 - TechvBlogs

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:

sudo apt install npm

This will allow you to install modules and packages to use with Node.js.

At this point, you have successfully installed Node.js and npm using apt and the default Ubuntu software repositories.

Option 2: Installing Node.js with Apt Using a NodeSource PPA

To install a different version of Node.js, you can use a PPA (personal package archive) maintained by NodeSource. NodeSource is a company focused on providing enterprise-grade Node support. It maintains an APT repository containing multiple Node.js versions.

At the time of writing, the NodeSource repository provides the following versions:

  • v14.x - The latest stable version.
  • v13.x
  • v12.x - The latest LTS version.
  • v10.x - The previous LTS version.

We’ll install Node.js version 14.x:

Run the next command as a person with sudo privileges to obtain and execute the NodeSource set up script:

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

As soon as the NodeSource repository is enabled, set up Node.js and npm:

sudo apt install nodejs

How to Install Node.js and NPM On Ubuntu 20.04 - TechvBlogs

Now you can verify Node.js and NPM Version with this command:

node -v
npm -v

How to Install Node.js and NPM On Ubuntu 20.04 - TechvBlogs

Read Also: Install Apache on Ubuntu 20.04 LTS

Option 3: Installing Node.js and NPM using NVM

NVM is also known as “Node Version Manager” is a script that allows you to manage multiple versions 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.35.3/install.sh | bash

Output:

How to Install Node.js and NPM On Ubuntu 20.04 - TechvBlogs

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

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

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

Because the output above says, it’s best to both shut and reopen the terminal or run the instructions so as to add the trail to nvm script to the present shell session. You are able to do no matter is simpler for you.

As soon as the script is in your PATH, confirm that nvm was correctly put in by typing:

nvm --version

How to Install Node.js and NPM On Ubuntu 20.04 - TechvBlogs

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

nvm list-remote

The command will print a list of all available Node.js versions. It’s a very long list!

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

nvm install node

After Install Node.js, you can check the version of Node.js and NPM using this command:

node -v
npm -v

How to Install Node.js and NPM On Ubuntu 20.04 - TechvBlogs

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

nvm install --lts
nvm install 10.9.0

How to Install Node.js and NPM On Ubuntu 20.04 - TechvBlogs

If you want to change the currently active version, enter:

nvm use 15.9.0

After running this command, you can see now we use node 15.9.0 Version.

How to Install Node.js and NPM On Ubuntu 20.04 - TechvBlogs

Thank you for reading this article!!

Read Also: Install Nginx on Ubuntu 20.04 LTS

 

Comments (0)

Comment


Note: All Input Fields are required.