"Command 'npm' not found" / "Command 'nvm' not found" on root account

Terminal returned with `Command 'npm' not found, but can be installed with: apt install npm` and `Command 'nvm' not found, did you mean: command 'nvim' from snap nvim`. How to fix it?

"Command 'npm' not found" / "Command 'nvm' not found" on root account

Issue

I was installing Ghost CMS on a Ubuntu 22.04.05 LTS with root account. One Ghost instance is already running on the machine. Ghost requires npm so it must be installed on the machine. However the terminal returned with Command 'npm' not found, but can be installed with: apt install npm . I usually use nvm but I also had Command 'nvm' not found, did you mean: command 'nvim' from snap nvim (nvim is good btw).

We found .npm and .nvm folder under a user's home folder. Which means npm and nvm was once installed but can't be used by root account.

Fix

When nvm (Node Version Manager) is installed under one user, the environment and commands for nvm are usually set up only for that user's shell environment. Other users on the machine will not have access to the nvm command by default because nvm works by sourcing scripts in the user's shell profile files (like .bashrc, .zshrc, or .profile).

To allow new users to access the nvm command, here are the typical ways:

  1. Install nvm separately for each user:

The simplest and recommended approach is for each user to install nvm in their own home directory by running the official install script:

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

This sets up nvm in their shell environment independently.

  1. Share a system-wide installation (advanced):
  • nvm is designed as a per-user tool and doesn’t officially support system-wide installs.
  • However, in some cases, administrators set up nvm in a shared directory (like /usr/local/nvm) and add source commands to all users' shell profiles.
  • This requires:
    • Installing nvm manually to a shared folder
    • Ensuring all users have read and execute permission on that directory
    • Adding something like this to all users' .bashrc or /etc/profile:
export NVM_DIR="/usr/local/nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
  • Note that managing node versions may be tricky under this model due to file permission conflicts.
  1. Using Node installed globally without nvm:
  • Alternatively, if users just need Node.js and npm without nvm's version management, Node can be installed globally via package managers like apt or downloaded binaries.

Summary

  • The recommended and cleanest way: have each user install their own nvm.
  • Sharing nvm across users is technically possible but requires careful setup with shared directories and environment configuration.

Subscribe to Dummy Zone

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe