Categories: TutorialsUbuntu

How to Install Node.js and Npm in Ubuntu?

What is Node.js?

Node.js is an open-source and cross-platform JavaScript runtime environment. Usually, JavaScript is associated with web development and browser executions.

But Node.js enables its user to run JavaScript as a stand-alone application, on any medium. It means you can run JavaScript outside the browser.

What is Npm?

Node Package Manager (npm) is both an online repository for modules and packages of Node.js, and a command-line utility for installation, version, and dependency management.

Npm is easy to use and makes working with Node.js very convenient. Additionally, npm comes with Node.js installation so you do not have to install it separately.

In this article, you will learn about different methods to install Node.js and Npm in your Ubuntu operating system.

Prerequisites

Sponsored

Linux-based system

Terminal access

A user account with sudo privileges.

Install Node.js Ubuntu Package Using apt

You will need a curl command to enable the NodeSource repository. If you lack curl on your system, install curl on your system using the following command.

sudo apt-get install curl

To enable the NodeSource repository and update the apt-cache. run the following command.

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

Now you can go ahead and install the Node.js package with the following apt command.

sudo apt install nodejs

You can see the Node.js version with the following command.

node --version

As npm comes with the Node.js installation, you can check the npm version with the following command.

npm --version

Install Node.js With PPA Software Repository Using apt

If you want your repository to be self-maintained and updated so that you can work with the latest version of Node.js, you can use the NodeSource PPA for Node.js installation.

Install the NodeSource PPA with the following curl command.

curl -sL https://deb.nodesource.com/setup_16.x -o nodesource_setup.sh

Open the Nodesource setup script with nano editor and verify the configurations.

nano nodesource_setup.sh

Sponsored

To add the PPA to the configuration list and update the local cache, run the script.

sudo bash nodesource_setup.sh

Now you can install Node.js with the following command.

sudo apt install nodejs

To verify the installation and version of Node.js, run.

node -v

Install NVM for Node.js Installation and Version Management

Node Version Manager (NVM) is a script that helps with the installation and management of multiple versions of Node.js in your system.

Download NVM from the GitHub repository with the following curl command.

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

You can see that the installation has made changes to the .bashrc file. Source your.bashrc file with the following command to proceed with the installation.

source ~/.bashrc

To see available versions of Node.js, run the following command.

nvm list-remote

Select the version and run the following command with your selected version.

nvm install v17.0.1

To see all the Node.js installations in your system, run the following command.

nvm list

You can see the current version that is in use mentioned in the first line of the output.

You can switch to another installed version and use it with the following command.

nvm use 17.0.1

Conclusion

In this article, you have learned about the different methods to install Node.js in your Ubuntu system. While installing Node.js, the fastest and easiest way is to use the default Ubuntu package, but if You need a more updated and maintained installation, You can choose the NodeSource PPA. However, the most flexible and recommended method is to use NVM to install Node.js as it can install and manage multiple versions side by side.

Ubuntu Server Admin

Recent Posts

Everything you need to know about FIPS 140-3 on Ubuntu | Videos

FIPS 140 is a highly demanding security standard that’s mandatory for almost all high-security and…

2 hours ago

A CISO’s preview of open source and cybersecurity trends in 2026 and beyond

Open source has come a long way. Recently I was watching a keynote address by…

5 days ago

Canonical Kubernetes officially included in Sylva 1.5

Sylva 1.5 becomes the first release to include Kubernetes 1.32, bringing the latest open source…

5 days ago

Canonical expands total coverage for Ubuntu LTS releases to 15 years with Legacy add-on

Expansion ensures business continuity without forcing major upgrades Today, Canonical announced the expansion of the…

6 days ago

Announcing YARD-Lint: Keep Your Ruby Documentation Solid

TL;DR: YARD-Lint catches documentation issues, just like RuboCop for code. Star it and use it…

6 days ago

Canonical releases FIPS-enabled Kubernetes

Deploy a FedRAMP-ready kubernetes cluster and application suite, with FIPS 140-3 crypto and DISA-STIG hardening…

1 week ago