Ubuntu

How to Install NPM on Ubuntu 22.04

How to Install NPM on Ubuntu 22.04

NPM is the package manager of the Node.js programming language. It is the largest code library containing more than 2 million code packages. It functions by managing the code dependencies of different servers. NPM packages streamline the process of application development because of their well-maintained and well-organized code structure.

NPM package manager assists in installing and running the JavaScript applications on Ubuntu. It automatically handles the JavaScript packages by installing the necessary libraries and frameworks needed to run the JS applications.

In this article, you will learn:

How to Install NPM on Ubuntu 22.04

Here are the methods through which you can install NPM on Ubuntu 22.04:

Method 1: Install NPM on Ubuntu 22.04 Using apt Package Manager

The default method to install the NPM package on Ubuntu is the apt command-line tool. It is a package manager in Linux that installs, removes, updates, or upgrades the applications. Go through the following step-by-step instructions to install NPM on Ubuntu 22.04 using the apt command:

Step 1: Update apt Repository Packages

Make sure to update the system repositories before installing NPM by running the mentioned command in Terminal:

sudo apt update

Step 2: Install NPM on Ubuntu

Open Terminal and execute the mentioned command to install NPM on Ubuntu 22.04:

sudo apt install npm -y

Step 3: Verify the NPM Installation

Once the NPM is installed, verify its installation by checking its version using the below command:

npm --version

The version confirms that the NPM package manager is successfully installed.

Method 2: Install NPM on Ubuntu 22.04 Using NVM

The NVM Node Version Manager” is an alternate method to install the NPM on Ubuntu. NVM checks the existence of Node.js on the system, gets the available version that can be installed on Ubuntu, and allows the user to install the desired Node.js version. Installing the Node.js via the NVM package manager will also install the NPM on Ubuntu automatically because it contains the NPM package inside it.

Let’s overview the stepwise instructions below to install NPM on Ubuntu 22.04 through NVM.

Step 1: Install NVM on Ubuntu

Before installing NVM, verify its latest version by visiting the GitHub link. You can copy the script by navigating to the given link and then paste it to the Terminal to download NVM using the curl command line tool.

Execute this command to download and install NVM version 0.39.7 on Ubuntu:

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

Step 2: Source .bashrc File

After installing NVM on Ubuntu, run the mentioned script to load changes to the system:

source ~/.bashrc

Step 3: Install Node.js and NPM

Before installing let’s check the currently installed versions of Node.js on Ubuntu by running this command:

nvm list

It can be observed that there are no Node.js versions installed on Ubuntu.

Now, let’s check which Node.js versions we can install on Ubuntu by running this command:

nvm list-remote

This will list down all the Node.js versions we can install on Ubuntu:

There is a long list of Node.js versions we can install on Ubuntu.

Let’s install the Node.js v20.11.1 version on Ubuntu, by executing the mentioned command:

nvm install v20.11.1

The specified Node.js version is installed on Ubuntu 22.04.

Let’s list down the Node.js version to check whether it is installed on Ubuntu or not:

nvm list

Step 4: Verify Node.js and NPM Installation

Now, let’s check the Node.js and NPM versions respectively to verify the installation of NPM and Node.js via NVM. To check the Node.js version on Ubuntu, execute the given command:

node -v

Use the mentioned command to verify the NPM installation on Ubuntu using NVM:

npm -v

Method 3: Install NPM on Ubuntu 22.04 Using PPA (NodeSource) Script

Alternatively, you can install the NPM by installing Node.js via a PPA repository script for Nodesource. The Nodesource is a platform used to store the Node.js packages. The Node.js package consists of both NPM and Node.js binaries, which means installing Node.js will also install NPM on Ubuntu.

Check the steps below to install NPM on Ubuntu 22.04 through the NodeSource PPA repository script.

Step 1: Download and Run Nodesource PPA Repository Script

First, download the Nodesource PPA repository script on Ubuntu inside the /tmp folder using the following command:

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

Note: You can navigate to the official Nodesource website to check the latest Nodesource release. The current version of Nodesource is 20.x. However, you can install any version by replacing 20.x in the above command with your desired version.

Step 2: Configure and Install the Necessary Packages

After the download is completed, run the Nodesoure script with the sudo command to begin the installation of the packages required to install the Node.js and NPM on Ubuntu:

sudo bash /tmp/nodesource_setup.sh

Step 3: Install Node.js and NPM Packages

Now, run the command similar to the first method to install node.js on Ubuntu:

sudo apt install nodejs -y

As discussed earlier installing the node.js package will install npm.

Step 4: Check the Node.js Installation

check the Node.js version by executing the mentioned command to verify the installation of the Node.js package:

nodejs -v

Now let’s check the NPM installation on Ubuntu by executing the below command:

npm -v

The NPM is successfully installed using the NodeSource (PPA).

Note: If you face any issues while executing the “sudo apt install nodejs -y” command. Then, execute

sudo dpkg -i --force-overwrite /var/cache/apt/archives/nodejs_<strong>20.11.1</strong>-1nodesource1_amd64.deb

Replace the highlighted version with your Node.js version. After that, execute the following command to fix the broken packages.,

sudo apt -f install

How to Remove NPM from Ubuntu 22.04

You can uninstall NPM from Ubuntu using:

Method 1: Remove NPM from Ubuntu 22.04 Using apt Package Manager

If you have installed Node.js through the apt package manager and NodeSource (PPA), then run the mentioned command to remove Node.js from Ubuntu:

sudo apt remove npm -y

Note: Ubuntu retains the configuration files after the removal of any application. To remove those files alongside application removal, use the purge command instead of the remove command, as shown below:

sudo apt purge npm -y

Method 2: Remove NPM from Ubuntu 22.04 Using NVM

The removal of the NPM package manager through NVM is explained below in the instructional steps.

Step 1: Verify the Current Node Version

Before removing NPM and Node.js, check the current Node.js version installed on Ubuntu by executing this command:

nvm current

Step 2: Deactivate NVM

Now, deactivate the NVM package manager on Ubuntu from the following command:

nvm deactivate

Step 3: Uninstall Node.js and NPM

Provide the nvm command followed by the uninstall command to remove Node.js and NPM from Ubuntu:

nvm uninstall v20.11.1

Conclusion

NPM is a package manager that provides access to code libraries and helps manage packages used in the Node.js projects. The NPM can be installed on Ubuntu using the APT command line tool (Official Repository). Alternatively, you can also install NPM on Ubuntu using NodeSource (PPA), and NVM package manager. Each method will let you install NPM on Ubuntu and help streamline your development process with Node.js.

Similar Posts