Step by Step Guide to Install NPM on Ubuntu

install npm on ubuntu

Table of Contents

Get up to 50% off now

Become a partner with CyberPanel and gain access to an incredible offer of up to 50% off on CyberPanel add-ons. Plus, as a partner, you’ll also benefit from comprehensive marketing support and a whole lot more. Join us on this journey today!

Node.js and the Node.js package manager, NPM (Node Package Manager), are great resources for the JavaScript application developer. Whether you are developing frontend or backend applications, NPM turns packages, dependencies, and scripts into a breeze. When you’re based on Ubuntu, to install NPM on Ubuntu is not an issue at all, but there are several ways to do this depending on your need.

This tutorial will guide you through the process of installing NPM on Ubuntu using different methods, including APT, NodeSource, and NVM (Node Version Manager). We will also cover how to verify the installation, update or uninstall NPM, and troubleshoot common issues.

Getting to Know NPM and Node.js

Install-NPM-on-Ubuntu

Let’s first understand what NPM is and why you need it before we move ahead with the installation:

  • Node.js – An environment to execute JavaScript outside the browser, and it is primarily employed for developing scalable network programs.
  • NPM (Node Package Manager) – Node.js package manager, allowing developers to install, manage, and distribute JavaScript packages.

NPM comes bundled with Node.js, and therefore when you install Node.js, you install NPM on Ubuntu as well.

Prerequisites

Before installing, ensure your system meets the following requirements:

  • An Ubuntu installation (20.04, 22.04, or later).
  • A sudo account.
  • A reliable internet connection to install packages.

To check if Node.js and NPM are already installed, use:

Tech Delivered to Your Inbox!

Get exclusive access to all things tech-savvy, and be the first to receive 

the latest updates directly in your inbox.

node -v
npm -v

If the above commands print version numbers, then Node.js and NPM are already installed.

Method 1: Install NPM Using APT (Default Ubuntu Repository)

Ubuntu’s package manager, APT (Advanced Package Tool), provides a straightforward way to install NPM on Ubuntu and install Node.js. Official Ubuntu repositories’ versions can be outdated, however.

Step 1: Update Package Lists

You’ll need to update your package lists before you can install NPM on Ubuntu. You can get the most recent available versions by following these steps:

sudo apt update && sudo apt upgrade -y

Step 2: Install Node.js and NPM

Execute the following command to install Node.js and NPM:

sudo apt install nodejs npm -y

Step 3: Validate the Installation

After installing, check Node.js and NPM versions:

node -v
npm -v

If you find numbers as the version, then the installation is a success.

Step 4: Update NPM (Optional)

Because the APT repository might not have the most up-to-date version, you may manually update NPM by:

sudo npm install -g npm@latest

This ensures you’re running the most recent version.

Method 2: Install NPM with NodeSource (Latest Stable Version)

For the latest Node.js and NPM version, use NodeSource, a third-party repository providing newer Node.js packages.

Step 1: Add NodeSource Repository

First, install the dependencies:

Enhance Your CyerPanel Experience Today!
Discover a world of enhanced features and show your support for our ongoing development with CyberPanel add-ons. Elevate your experience today!

sudo apt install curl -y

Then, add the NodeSource repository for the latest LTS version (most users will be fine with this):

curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -

For the latest current version (if needed for some features):

curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -

Step 2: Install Node.js and NPM

After installing the repository, install Node.js and NPM with:

sudo apt install nodejs -y

Step 3: Verify the Installation

Verify the installed versions:

node -v<br>npm -v

Step 4: Install Extra Build Tools (Optional)

In certain situations, you may need to install build-essential:

sudo apt install build-essential -y

Method 3: Install NPM Using NVM (Node Version Manager)

If you have more than one project with varying Node.js versions, NVM (Node Version Manager) is the best option.

Step 1: Install NVM

Install NVM using the following command:

curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
Then, reload your shell configuration:
source ~/.bashrc

Step 2: Verify NVM Installation

Check if NVM has installed successfully:

nvm --version

Step 3: Install Node.js and NPM

To install the latest LTS version of Node.js and NPM:

nvm install --lts
To install a particular version:
nvm install 18

Step 4: Make Default Node.js Version

To set a particular version as the default:

nvm use 18
nvm alias default 18

Step 5: Check Installation

Check the installed versions:

node -v
npm -v

NPM Upgrade

If you have already installed NPM and want to update it to the latest version, run:

sudo npm install -g npm@latest

For those users who have installed Node.js using NVM, upgrade NPM by:

npm install -g npm@latest

Uninstalling NPM and Node.js

If you have to uninstall NPM and Node.js, then follow the steps based on how you had installed.

1. Uninstalling Node.js and NPM Installed via APT

sudo apt remove --purge nodejs npm -y
sudo apt autoremove -y

2. Uninstalling Node.js and NPM Installed using NodeSource

sudo rm -rf /etc/apt/sources.list.d/nodesource.list
sudo apt remove --purge nodejs -y

3. Uninstalling Node.js Installed with NVM

nvm deactivate
nvm uninstall <version>

To uninstall version 18, for instance:

nvm uninstall 18

To remove NVM completely:

rm -rf ~/.nvm

Then remove mentions from .bashrc, .bash_profile, or .zshrc.

Troubleshooting Common Issues

1. NPM not found

Verify Node.js installation:

<code>bash node -v</code>

Reload shell if installed with NVM:

<code>source ~/.bashrc</code>

2. EACCES permission errors

Fix permissions:

sudo chown -R $(whoami) ~/.npm

3. NPM not updating

Try:

npm cache clean -f
<code><span style="background-color: initial; font-family: inherit; font-size: 1rem; color: initial;">npm install -g npm@latest</span></code>

Conclusion

To install NPM on Ubuntu is simple and can be done using APT, NodeSource, or NVM, based on your requirements.

  • Use APT if easy installation with stable releases.
  • Use NodeSource for newest official releases.
  • Use NVM to easily switch between multiple Node.js versions.

FAQs

What is NPM and why do I require it on Ubuntu?

NPM (Node Package Manager) is Node.js’s native package manager. It enables developers to install, manage, and update JavaScript libraries, tools, and frameworks. If you are developing or operating JavaScript-based apps on Ubuntu, you will most probably require NPM to work with dependencies and scripts.

Why is the APT version of NPM out of date?

APT repositories do not always contain the most recent versions of packages. To install the latest Node.js and NPM versions, try installing from NodeSource or with NVM.

Can I install multiple versions of Node.js and NPM on Ubuntu?

Yes, with NVM (Node Version Manager), you can easily install and switch between several Node.js/NPM versions.

Shumail
Shumail is a skilled content writer specializing in web content and social media management, she simplifies complex ideas to engage diverse audiences. She specializes in article writing, copywriting, and guest posting. With a creative and results-driven approach, she brings fresh perspectives and attention to detail to every project, crafting impactful content strategies that drive success.
Unlock Benefits

Become a Community Member

SIMPLIFY SETUP, MAXIMIZE EFFICIENCY!
Setting up CyberPanel is a breeze. We’ll handle the installation so you can concentrate on your website. Start now for a secure, stable, and blazing-fast performance!