How to Fix “npm command not found” on Ubuntu Server (Step-by-Step Guide)

Introduction

Encountering the “npm command not found” error on Ubuntu can be frustrating, especially if you’re deploying a project or setting up a development environment. Whether you’re a seasoned developer or an amateur IT enthusiast, this guide will walk you through every step needed to solve this issue efficiently.

What is npm?

Understanding npm

  • npm stands for Node Package Manager.
  • It’s the default package manager for Node.js, used to install and manage packages (libraries and tools).

Why You Might Need npm

  • Required for frontend frameworks like React, Angular, Vue
  • Used in full-stack apps (MEAN, MERN stacks)
  • Essential for managing JavaScript project dependencies

Common Causes of the Error

1. Node.js is Not Installed

  • npm comes bundled with Node.js
  • If Node.js isn’t installed, npm won’t be available

2. Installation Path Not Included in $PATH

  • Your system may not be able to locate npm even if installed
  • A broken symlink or partial uninstall may leave npm inaccessible

Step-by-Step Fix

Step 1: Check If Node.js is Installed

node -v
npm -v

If both commands return “command not found”, Node.js likely isn’t installed.

Step 2: Install Node.js and npm via apt (Official Way)

sudo apt update
sudo apt install nodejs npm -y

Verify Installation

node -v
npm -v

NVM allows multiple Node.js versions and better flexibility.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install --lts

Recheck

npm -v
sudo ln -s /usr/bin/nodejs /usr/bin/node

If npm is still not found, try:

sudo ln -s /usr/bin/npm /usr/local/bin/npm

Step 5: Add npm to PATH (if it’s installed but not accessible)

Add to .bashrc or .zshrc:

export PATH=$PATH:/usr/local/bin

Then:

source ~/.bashrc

Pro Tips

Use NVM for Flexibility

  • Easily switch between versions: nvm use 18
  • Set default version: nvm alias default 18

Avoid sudo npm install -g Unless Necessary

  • May create permission issues
  • Use nvm or set a custom global directory

Check Global npm Directory

npm config get prefix

Customize it:

npm config set prefix ~/.npm-global

Conclusion

Fixing the “npm command not found” error on Ubuntu is usually a matter of either installing Node.js properly or correcting the system path. For developers, using NVM is highly recommended for flexibility and ease of updates. By following this guide, you’ll not only fix the immediate problem but also set up a more robust development environment.


FAQs

1. How do I check where npm is installed?

which npm

2. Why is node found but not npm?

Your Node.js installation may be incomplete. Reinstall via nvm.

3. Can I install npm separately?

Technically yes, but it’s bundled with Node.js. Installing separately is not recommended.

4. What if I’m using zsh or another shell?

Ensure you update the correct config file like .zshrc instead of .bashrc.

5. Is it better to use apt or nvm?

For developers, nvm is better. apt is simple but less flexible.

We will be happy to hear your thoughts

Leave a reply

DevStackHub | Cloud Hosting & AI for Developers - Reviews & Guides
Logo
Compare items
  • Total (0)
Compare
0