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

Posted on

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

How to Install NPM on Debian/Ubuntu or 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 npm@<version_number>

Replace <version_number> 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 <version> 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:

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.

Alternative Solutions for Installing NPM

While the article covers several effective methods for installing NPM, here are two alternative approaches: using Docker and using a package manager like asdf.

1. Installing NPM using Docker

Docker provides a containerization platform that allows you to run applications in isolated environments. You can use a Docker image that comes pre-installed with Node.js and NPM, ensuring a consistent environment across different systems.

Explanation:

This method avoids direct installation on the host operating system. It is particularly useful when you want to isolate your Node.js environment from the rest of the system, or when you need to work with multiple Node.js versions without conflicts. Docker images are self-contained and include all the necessary dependencies, libraries, and configurations.

Steps:

  1. Install Docker: Follow the official Docker installation instructions for your specific operating system.

  2. Pull a Node.js Docker Image: Use the docker pull command to download a Node.js image from Docker Hub. Choose an image that includes NPM.

    docker pull node:18

    This command pulls the Node.js version 18 image, which comes with NPM pre-installed.

  3. Run a Container: Create and start a Docker container using the docker run command.

    docker run -it --name my-node-app -v $(pwd):/app -w /app node:18 bash
    • -it: Runs the container in interactive mode, allowing you to execute commands inside the container.
    • --name my-node-app: Assigns a name to the container for easy management.
    • -v $(pwd):/app: Mounts the current directory on your host machine to the /app directory inside the container. This allows you to work on your project files from your host machine while running the application inside the container.
    • -w /app: Sets the working directory inside the container to /app.
    • node:18: Specifies the image to use for the container.
    • bash: Starts a Bash shell inside the container.
  4. Verify NPM Installation: Once inside the container, verify that NPM is installed by checking its version.

    npm --version

Code Example (Dockerfile):

Alternatively, you can create your own Dockerfile to customize the Node.js environment:

FROM node:18

# Set the working directory
WORKDIR /app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the source code
COPY . .

# Expose a port (optional)
EXPOSE 3000

# Command to run the application
CMD [ "npm", "start" ]

This Dockerfile does the following:

  • Starts from the node:18 base image.
  • Sets the working directory to /app.
  • Copies the package.json and package-lock.json files.
  • Installs the project dependencies using npm install.
  • Copies the rest of the source code.
  • Exposes port 3000 (if your application uses it).
  • Specifies the command to run the application using npm start.

To build and run this Dockerfile:

docker build -t my-node-app .
docker run -p 3000:3000 my-node-app

2. Installing NPM using asdf version manager

asdf is a version manager that can handle multiple language runtimes, including Node.js. It allows you to install and manage different versions of Node.js and NPM on a per-project basis.

Explanation:

asdf provides a unified interface for managing multiple language runtimes, making it easy to switch between different Node.js versions as needed for different projects. This can be useful if you are working on multiple projects with varying Node.js version requirements.

Steps:

  1. Install asdf: Follow the official asdf installation instructions for your operating system (https://asdf-vm.com/).

  2. Add the Node.js Plugin: Use the asdf plugin-add command to add the Node.js plugin to asdf.

    asdf plugin-add nodejs https://github.com/asdf-vm/asdf-nodejs.git
  3. Install a Node.js Version: Use the asdf install command to install a specific version of Node.js.

    asdf install nodejs 18.19.0
  4. Set the Node.js Version: Use the asdf global or asdf local command to set the Node.js version for your system or project.

    asdf global nodejs 18.19.0  # Set globally
    asdf local nodejs 18.19.0   # Set locally for the current project
  5. Verify NPM Installation: Verify that NPM is installed by checking its version.

    npm --version

Code Example (.tool-versions file):

In your project directory, create a .tool-versions file to specify the Node.js version for that project:

nodejs 18.19.0

This file tells asdf to use Node.js version 18.19.0 when you are in that directory.

These alternative methods provide flexibility and isolation, catering to different development needs and preferences.

Leave a Reply

Your email address will not be published. Required fields are marked *