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: A Structured Engineer’s Guide

Installing npm on Linux via package managers is notorious for introducing subtle issues—dependency drift, broken global installs, or locked Node versions. Proper control requires trading convenience for reproducibility.


Avoid the Usual Pitfalls: Why Package Managers Fall Short

Quick apt install npm? Expect friction:

  • Outdated: Debian/Ubuntu repos typically lag—Node.js v12 or npm 6 in 2024 is still sadly common.
  • Broken Coupling: System nodejs and npm may be mismatched ("npm WARN npm npm does not support Node.js vXX.XX.XX").
  • Global Permissions: npm install -g <pkg> will prompt for root; security anti-pattern.
  • Multiple Node Versions: Manual upgrades leave ghosts in /usr/bin or /usr/local/bin.

Step 1: Remove System Node.js and npm

Consistent state beats uncertainty. Purge stock versions:

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

Verify clean slate:

node -v    # Should return 'command not found'
npm -v     # Same here

Note: Still getting a version number? Node.js binaries often end up in /usr/local/bin from previous manual installs. Check and remove if necessary:

sudo rm -f /usr/local/bin/node /usr/local/bin/npm

Step 2: Install Node.js (and npm) from NodeSource

NodeSource distributes actively maintained binaries for most LTS and current Node.js versions. Choose LTS (e.g., v18.x) unless there’s a specific requirement.

Add NodeSource repo and install Node.js:

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
  • Verifies node and correct npm are paired:
node -v       # Expect v18.x.x
npm -v        # Should typically be >=8.x for Node 18.x

What if you see a mismatch? Force a clean install, and never mix with distro package manager from this point.


Step 3: (Optional, but Advised) Install a Node Version Manager

Maintaining multiple Node versions is common in active SDLC environments. Two main contenders exist: n and nvm. Here, we use n for its straightforward global usage.

Install n globally:

sudo npm install -g n
  • sudo is still required here due to global install path (we’ll fix that next).
  • Now, get the latest (or LTS) Node:
sudo n lts
sudo n latest

Quick node switching:

n   # Opens an interactive selector for installed Node.js versions

Known issue: n modifies symlinks in /usr/local/bin/. If using company-managed workstations, verify permissions or opt for nvm.


Step 4: Reconfigure npm Global Package Location (No More sudo)

Default global installs target /usr/lib or /usr/local/lib, causing permission prompts. Solve by scoping global installations to your user:

Set up isolated global prefix:

mkdir -p ~/.npm-global
npm config set prefix '~/.npm-global'

Add to PATH:
Place the following in ~/.profile, ~/.bashrc, or ~/.zshrc:

export PATH="$HOME/.npm-global/bin:$PATH"

Apply changes:

source ~/.profile  # or whichever applies; check your shell

Now test:

npm install -g typescript   # Should *not* require sudo
which tsc                   # Should point to ~/.npm-global/bin/tsc

Gotcha: Some CLI tools hardcode /usr/local/bin, requiring manual symlink for recognition:

ln -s ~/.npm-global/bin/your-cli /usr/local/bin/

Step 5: End-to-End Validation

A squeaky-clean toolchain should be able to:

  1. Init a project
  2. Install dependencies
  3. Run code

Example:

mkdir ~/node-check && cd ~/node-check
npm init -y
npm install lodash
node -e "console.log(require('lodash').random(1,42))"
# Example output: 37

If the above workflow fails, review permissions and PATH—missing one step often leads to subtle errors (“Cannot find module...” or “EACCES: permission denied”).


Side Notes & Tradeoffs

  • Audit regularly: npm audit surfaces package-level vulnerabilities—surprisingly useful before deploying to production.
  • Version pinning: Use .nvmrc or engines in package.json if you onboard multiple projects with divergent Node requirements.
  • Alternative: nvm for non-root usage and per-shell versioning, but beware: it operates by shell hooks and can lead to inconsistencies across CI and system-wide tooling.
  • Container builds: In CI/CD or Docker environments, prefer explicit version installs (e.g. FROM node:18-bullseye) to avoid ambient system state.

Conclusion

A system-level Node/npm install should balance flexibility with reproducibility. Conventional package managers often lead to pain—outdated binaries, global permission headaches, and messy upgrades. NodeSource + n (with proper npm prefixing) delivers an environment that’s both stable and easy to maintain.

Remember: global package isolation isn't perfect—some CLI tools don’t respect custom prefixes. Accept that tradeoff, and adjust workflows as edge cases arise.


Encountering a non-obvious error or integrating into a custom pipeline? Capture the logs, include which node and which npm output, and share specifics—diagnosing systemic issues depends on these details.