How To Install Npm In Linux

How To Install Npm In Linux

Reading time1 min
#JavaScript#Development#Linux#npm#Node.js

Mastering the Clean Installation of npm on Linux: Step-by-Step Guide for Developers

Forget quick fixes—installing npm the right way on Linux isn’t just about running apt-get. Dive into a methodical approach that guarantees a clean, stable environment and prevents common pitfalls that trip up even experienced developers.


npm, or Node Package Manager, is the backbone of the JavaScript ecosystem. From managing dependencies to running scripts and building complex applications, npm is essential for every modern developer. However, installing npm on Linux systems can sometimes be tricky — especially if you rely solely on your package manager (apt, yum, or dnf), which might provide outdated versions or conflict with your node.js setup.

In this comprehensive guide, we’ll walk you through installing npm cleanly on your Linux machine, ensuring stability, compatibility, and security for all your projects.


Why Not Just Use apt-get install npm?

The common advice is:

sudo apt-get update
sudo apt-get install npm

While this works in some cases, it’s not ideal because:

  • Outdated packages: The versions in the default repos can be old.
  • Version mismatch: Your node and npm might get out of sync.
  • Multiple versions issues: You could end up with conflicting Node/npm installations.
  • Permissions headaches: Global installs might force you to use sudo unnecessarily.

Recommended Approach: Using NodeSource PPA + n (Node version manager)

Step 1: Remove any existing node and npm installations

First, clean out any previously installed node or npm to avoid conflicts:

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

Verify removal:

node -v  # Should say command not found or similar
npm -v   # Same here

Step 2: Install Node.js via NodeSource Repository

NodeSource maintains up-to-date Node.js binaries that include matching npm versions.

Add the NodeSource repo:

For example, to install Node.js v18 (LTS recommended):

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

Then install:

sudo apt-get install -y nodejs

Check versions installed:

node -v   # Should show v18.x.x
npm -v    # Comes bundled with nodejs package now; verify version too.

Step 3 (Optional): Install n for Easier Node Version Management

If you need to switch between multiple Node.js versions regularly during development:

sudo npm install -g n

Make sure your global installs don’t require sudo by configuring npm permissions properly (more on that below).

Use n to manage versions smoothly:

sudo n latest      # Installs latest stable node version globally 
sudo n lts         # Installs latest LTS version globally 

Switch between installed versions simply by typing:

n     # Interactive version selector runs in terminal

Step 4: Fix npm Global Permissions (Avoid Using sudo)

By default, some global installs ask for sudo. This is often due to writing into system directories. You can avoid this security risk by changing where global packages are installed.

  1. Create a directory for global packages in your home directory:
mkdir ~/.npm-global
  1. Configure npm to use this directory for global installs:
npm config set prefix '~/.npm-global'
  1. Add this directory to your PATH by editing .profile or .bashrc/.zshrc:
export PATH=~/.npm-global/bin:$PATH

Reload your shell config:

source ~/.profile   # or source ~/.bashrc etc.

Now you can install global packages without needing root:

npm install -g typescript   # Example global tool installation without sudo.

Step 5: Test Your Clean Setup

Create a quick NPM project to verify everything works correctly.

mkdir my-test-project && cd my-test-project
npm init -y        # Initializes package.json quickly 

npm install lodash  # Install a sample package locally

node -e "console.log(require('lodash').random(1,100))"
# This should print a random number between 1 and 100 using lodash.

You’ve confirmed node, npm, and package management are working fine!


Bonus Tips to Keep Your Setup Healthy

  • Keep Node/npm updated via n:

Keep your runtime current without relying solely on OS package managers.

  • Use .nvmrc files for per-project Node versions:

Consider using nvm if you have complex per-project node requirements (similar usage but off this guide’s focus).

  • Audit Security of Packages Automatically:

Run npm audit periodically to catch vulnerabilities early.


Final Thoughts

Mastering the clean installation of Node.js and npm on Linux is fundamental for smooth JavaScript development. By avoiding outdated repo-built packages, handling permissions properly, and optionally employing version managers like n, you’ll have a reliable environment free from common traps—ready for any modern frontend or backend project.

If you want fast but fragile installs — sure, run plain old apt-get. But if you value stability & security in your dev stack — follow this methodical guide and invest once in a powerful setup that keeps working beautifully over time.

Happy coding! 🚀


Got questions about setups? Drop a comment below! I’m here to help make JavaScript dev life easier.