How to Install NPM on Debian/Ubuntu or CentOS/RHEL

Install NPM on Linux: Step-by-Step Guide for Debian, Ubuntu, CentOS, RHEL

NPM (Node Package Manager) is a command line utility for installing, sharing and managing Node.js packages and modules. It is a package manager for the JavaScript programming language. NPM is installed with Node.js and allows developers to easily share and reuse code from the NPM registry.

In this extensive tutorial, we will cover how to install NPM on Debian/Ubuntu and CentOS/RHEL operating systems in detail. We’ll go over checking existing installations, uninstalling old versions if needed, and walk through the installation process step-by-step. Let’s get started!

Checking for Existing Installations

Before installing or reinstalling NPM, it’s a good idea to check if you already have Node.js and NPM installed and which versions currently exist on your system.

Open a terminal and type:

$ node --version
$ npm --version

If Node.js and NPM are installed, these commands will show the current version number. For example, on my system I see:

$ node --version
v18.19.0
$ npm --version  
10.2.3

This output indicates I already have Node v18.19.0 and NPM v10.2.3 installed.

If these commands don’t return a version number it likely means that Node.js and NPM are not installed on your system yet.

Removing Old Versions (If Required)

If you have an older version installed that you want to replace, we’ll go over how to remove Node.js and NPM cleanly from Debian/Ubuntu and CentOS/RHEL systems.

Note: Removing old installations is only required if you want to completely wipe out the existing version and dependencies for a new fresh install. If you’re fine with the current version and just want the latest updates, you can skip ahead to the installation sections.

Uninstall Node.js on Debian/Ubuntu

To remove Node.js on Debian/Ubuntu, run:

$ sudo apt-get remove nodejs
$ sudo apt-get purge nodejs
$ sudo apt-get autoremove

This will uninstall Node.js from your system along with any related packages and configuration files.

You can also run:

$ sudo apt-get remove --purge nodejs npm

This will remove Node.js as well as NPM.

Uninstall Node.js on CentOS/RHEL

To uninstall Node.js on CentOS/RHEL, run:

$ sudo yum remove nodejs
$ sudo yum autoremove

This will remove the Node.js package and clean up any dependencies.

To also remove NPM, use:

$ sudo yum remove npm

With the old installations removed, you are ready to install the latest version of Node.js and NPM on your system.

Installing on Debian/Ubuntu

There are a few different options for installing Node.js and NPM on Debian and Ubuntu systems. We’ll go over using the default repositories and then some alternative installation methods.

Installing from Default Repositories

This method involves installing Node.js and NPM from the default Ubuntu repositories. The versions included may not be the latest, but this is a reliable way to install on Ubuntu systems.

To install both Node.js and NPM, run:

$ sudo apt update
$ sudo apt install nodejs npm

This will install Node.js and NPM from the Ubuntu repositories and automatically install some dependencies as well.

You can verify the installed versions:

$ node --version
$ npm --version

Using NodeSource Repository

A popular alternative is to add the NodeSource repository to apt and install from there. This lets you install a more up-to-date version of Node.js than what is available in the default Ubuntu repositories.

First check the latest Node.js LTS version available from the NodeSource site: https://github.com/nodesource/distributions

As of writing, the latest LTS version is Node 18.x which is what we’ll use here. But check for the latest version before installing.

First, install the NodeSource PPA to get access to its contents:

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

Note: Change the version number in the URL after ‘setup_’ to match the latest LTS version you want to install.

The PPA has been added and your system repositories updated. Now install Node.js and npm:

$ sudo apt-get install -y nodejs

Finally, verify the installation:

$ node --version
$ npm --version

Using Node Version Manager (nvm)

An even more flexible option is to install the nvm script which allows you to easily install and switch between multiple versions of Node on the same system.

To install nvm on Ubuntu, first install the build tools required for some of the packages:

$ sudo apt update
$ sudo apt install build-essential libssl-dev

Then install nvm with curl and activate it:

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash  
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
$ [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

With nvm installed, you can now easily install any version of Node like this:

$ nvm install 18.19.0

Use the latest LTS version instead of 18.19.0.

And switch between versions like this:

$ nvm use 18.19.0

You can list all versions installed:

$ nvm list

And use nvm to install NPM:

$ nvm install-latest-npm

This installation method gives you complete control over Node versions installed on your system.

Building from Source

Another option is to build Node from source. This allows you to install Node with specific custom configurations if needed.

First, install the build tools required:

$ sudo apt-get install gcc g++ make

Then download the latest Node source code:

$ curl -sL https://nodejs.org/dist/v18.19.0/node-v18.19.0.tar.gz -o node.tar.gz

Replace the version 18.19.0 with the desired version number. Then extract the tarball:

$ tar -xzf node.tar.gz

Move into the extracted directory and configure for build:

$ cd node-v*
$ ./configure

Now build and install:

$ make
$ sudo make install

Lastly, verify install:

$ node --version
$ npm --version

This installation method allows for custom configurations but is more complex than the other methods.

Installing on CentOS/RHEL

Similar to Ubuntu, CentOS/RHEL has several options for installing Node.js and NPM.

Installing from Default Repositories

This involves installing from the default package repositories on CentOS/RHEL systems. The version will not be the latest but it’s a stable way to install.

For CentOS/RHEL 7:

$ sudo yum install nodejs

For CentOS/RHEL 8:

$ dnf module install nodejs:18

Using NodeSource Repository

A popular alternative is to add the NodeSource repository and install from there. This gives you access to more up-to-date Node.js versions.

First check the latest available versions from NodeSource here: https://github.com/nodesource/distributions

As of this writing, the latest LTS is v18.x so we will use that:

$ sudo yum install -y gcc-c++ make

This installs some required build tools first. Then add the NodeSource repository:

$ curl -sL https://rpm.nodesource.com/setup_18.x | sudo bash -

Install Node.js and npm:

$ sudo yum install nodejs

Check versions:

$ node --version
$ npm --version

Using nvm

You can also install nvm to manage multiple Node.js versions on CentOS/RHEL.

Install the required build tools:

$ sudo yum install gcc-c++ make

Then install nvm:

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

Activate nvm:

$ export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
$ [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

Now you can install any Node.js version:

$ nvm install 18.19.0  

Switch between installed versions:

$ nvm use 18.19.0

List installed versions:

$ nvm list

Install NPM:

$ nvm install-latest-npm

Using nvm gives you the flexibility to install and manage multiple Node.js versions as needed.

Building from Source

You can also compile Node from source if you want custom configurations.

Install the required build tools:

$ sudo yum groupinstall "Development Tools"

Download the desired Node version source code:

$ curl -sL https://nodejs.org/dist/v18.19.0/node-v18.19.0.tar.gz -o node.tar.gz 

Replace 18.19.0 with your desired version.

Extract the tarball:

$ tar -xzf node.tar.gz

Move into the extracted directory and configure:

$ cd node-v*  
$ ./configure

Build and install:

$ make
$ sudo make install 

Verify installation:

$ node --version
$ npm --version

While more complex than other methods, building from source allows custom configuration options.

Updating NPM

If Node.js and NPM are already installed and you want to update to the latest version, use:

$ sudo npm install -g npm@latest

This uses NPM to update itself to the latest available version.

Or to update to a specific version:

$ sudo npm install -g [email protected]

Replace 10.2.3 with the NPM version number you want to update to.

You can also use the Node Version Manager (nvm) to update to the latest NPM version:

$ nvm install-latest-npm

This will install the latest available NPM version for the active Node.js version on the system.

Usage

With NPM installed, you can now use various npm commands to install and manage packages.

To install a package:

$ npm install <package_name>

This installs the latest version of the package to the local node_modules folder in your current working directory.

To install a package globally:

$ npm install -g <package_name>  

Global packages are installed to the system’s global node_modules folder and can be accessed from anywhere on the system.

To install a specific package version:

$ npm install <package_name>@<version>

Replace with the package semver number you want to install.

Update packages:

$ npm update  

This updates all packages in the current working directory to their latest versions.

To list installed packages:

$ npm list

This lists all the packages installed in the local folder.

To see all installed global packages:

$ npm list -g --depth 0

There are many more NPM commands for publishing packages, managing dependencies, running scripts and more. Some other commonly used commands include:

  • npm init – Create a new package.json file
  • npm test – Run tests for a package
  • npm start – Run the start script defined in package.json
  • npm run <script_name> – Run a custom defined script
  • npm outdated – Check for outdated packages
  • npm uninstall <package_name> – Uninstall a package

For an extensive reference on all npm commands, visit: https://docs.npmjs.com/cli/v10/commands

Conclusion

In this comprehensive article, we covered how to check for existing Node.js and NPM installations, remove old versions if needed, and various methods for installing or updating NPM on Debian/Ubuntu and CentOS/RHEL systems.

We went over using the default repositories, NodeSource, Node Version Manager (nvm), building from source code, and updating NPM to the latest version. We also touched on some of the most commonly used NPM commands and usage once installed.

NPM is an indispensable tool for any JavaScript developer. Following these step-by-step instructions should give you the knowledge to install and manage NPM on your chosen Linux distribution. Feel free to refer back to this guide whenever you need to install, update or manage NPM on a Linux system.

LEAVE A COMMENT