How To Install NPM Linux

How To Install NPM Linux






NPM is installed alongside Node.js and is essential for managing JavaScript dependencies. This guide covers installing NPM on Linux using multiple approaches. Each method suits different scenarios, ensuring flexibility in setup.

Prerequisites

Ensure you have the following before beginning:

A running Linux system with terminal access.

Root or sudo privileges for package installation.

What is NPM used for?

NPM handles package installation, dependency management, script execution, and package publishing. It provides a central registry for third-party libraries, allowing developers to install tools and frameworks with a single command. Running npm install express retrieves Express.js and its dependencies, reducing configuration effort.

The package.json file records installed libraries and their versions, ensuring consistency across environments. Running npm install in a project directory restores all dependencies, making onboarding and deployment efficient. Lock files like package-lock.json guarantee version stability, preventing unintended updates from breaking applications.

NPM automates development tasks through scripts defined in package.json. Running npm start can trigger commands like node app.js, while npm test executes a predefined testing workflow.

Publishing packages to the NPM registry allows developers to share reusable JavaScript libraries. A package can be published using npm publish, making it accessible through a simple installation command.

Commands like npm update and npm uninstall ensure projects stay current and maintainable. Version constraints in package.json allow controlled updates, preventing compatibility issues while keeping libraries current.

How to install NPM on Linux

To install NPM on Linux, Node.js must be set up. This guide explores three installation methods: using your distribution’s default repository, the NodeSource repository, and Node Version Manager (NVM). Each approach suits different use cases, such as stability, access to latest features, or managing multiple Node.js versions.

Step 1: Updating the system

Begin by updating your system to ensure all existing packages are current. This minimizes potential conflicts during installation.

For Debian/Ubuntu systems, run these commands:

$ sudo apt update
$ sudo apt upgrade -y

For RHEL/CentOS/Fedora systems:

$ sudo dnf update -y

This process fetches the latest package information and applies updates to installed software.

Step 2: Installing Node.js and NPM

Installing Node.js also provides NPM, the package manager for JavaScript libraries. The installation method affects version availability and system compatibility.

Option 1: Installing from distribution’s default repository

Your distribution’s package repository offers a straightforward way to install Node.js and NPM while ensuring security updates through system maintenance. The available version may not be the latest release.

1. For Debian/Ubuntu, install both packages:

$ sudo apt install -y nodejs npm

For RHEL/CentOS, enable EPEL repository first:

$ sudo dnf install epel-release -y
$ sudo dnf install nodejs npm -y

For Fedora:

$ sudo dnf install nodejs npm -y

2. Verify the installation:

$ node -v
$ npm -v

This method suits developers who prioritize stability and integration with the system’s package manager. For the latest Node.js version, use the NodeSource repository or NVM.

Option 2: Installing Node.js via NodeSource repository

The NodeSource repository provides an updated and reliable method for installing the latest stable version of Node.js on Linux. Default repositories often fall behind in version updates, which can limit access to new features and security patches.

NodeSource ensures direct access to maintained releases, making it the preferred choice for developers who need a current and well-supported Node.js environment.

1. Update your package list:

$ sudo apt update

2. Add the NodeSource repository for Node.js 20.x:

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

For RHEL/CentOS/Fedora systems:

$ curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -

This command downloads and executes the NodeSource setup script, configuring the repository and preparing your system for installation.

3. Install Node.js and NPM:

For Debian/Ubuntu:

$ sudo apt install -y nodejs

For RHEL/CentOS/Fedora:

$ sudo dnf install -y nodejs

4. Confirm the installation:

$ node -v
$ npm -v

These commands return version numbers, indicating that Node.js and NPM are correctly installed.

Managing Node.js versions

The NodeSource repository provides a single version of Node.js at a time. If you need to switch between different versions, consider using NVM. Otherwise, updating Node.js via NodeSource is straightforward:

$ sudo apt update && sudo apt upgrade -y

Installing Node.js via the NodeSource repository ensures access to the latest stable version with frequent updates. This method is ideal for developers requiring newer features, security patches, and performance improvements.

Option 3: Installing via NVM (Node Version Manager)

NVM handles multiple Node.js versions without altering system-wide configurations. It allows seamless switching between versions, ensuring projects use the correct Node.js release without dependency conflicts.

1. Download and execute the official installation script:

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

This downloads the latest installation script, clones the NVM repository into your home directory, and modifies shell configuration files to load NVM automatically.

2. Load NVM into the current shell session:

$ export NVM_DIR="$HOME/.nvm"
$ [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
$ [ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"

3. Restart the terminal or manually source the profile file:

$ source ~/.bashrc  # Bash
$ source ~/.zshrc   # Zsh

4. Install Node.js using NVM:

$ nvm install node  # Installs the latest stable release

To install a specific version:

$ nvm install 18.17.1
$ nvm install 20.10.0

5. List all installed versions:

$ nvm list

6. Switch between installed versions:

$ nvm use 20.10.0

7. Confirm the active version:

$ node -v
$ npm -v

8. Set a default version for all new terminal sessions:

$ nvm alias default 20.10.0
Note: NVM is the preferred method for developers managing multiple projects requiring different Node.js versions. Since it isolates Node.js installations per user, it avoids conflicts with system-wide packages.

When to use NVM?

NVM is essential for development environments where maintaining compatibility across projects is necessary. This flexibility makes NVM the best choice when working with multiple applications that depend on different Node.js versions.

Verifying NPM installation

After installing Node.js and NPM through any method, verify the installation:

$ node -v
$ npm -v

These commands display the installed versions. If version numbers appear, the installation succeeded.

Tip: Update NPM to the latest version using npm install -g npm regardless of your installation method.

Basic NPM commands

After installation, use these essential commands to manage packages:

Install a package locally:

$ npm install package-name

Install a package globally:

$ npm install -g package-name

Remove a package:

$ npm uninstall package-name

Update packages:

$ npm update

List installed packages:

$ npm list

Search for packages:

$ npm search keyword
Warning: Always review package documentation before installation to ensure compatibility with your project requirements.

Conclusion

This guide covered installing NPM on Linux using three methods: your distribution’s default repository, the NodeSource repository, and NVM. Each method caters to different development needs, from stability to version flexibility.

With NPM installed, you can now manage JavaScript dependencies efficiently. Explore NPM’s documentation for advanced usage to leverage its full potential in your projects.

FAQs