Skip to content

Instantly share code, notes, and snippets.

@jozsefsallai
Last active May 24, 2021 08:56
Show Gist options
  • Save jozsefsallai/7b9833183d5c4ab1706d13dfc30f750c to your computer and use it in GitHub Desktop.
Save jozsefsallai/7b9833183d5c4ab1706d13dfc30f750c to your computer and use it in GitHub Desktop.
Easiest method to make node and npm work for all users (non-root too). User-based global packages can also be installed by any user using this method.

Node and npm for all users (in 3 easy steps)

Warning! These instructions are mainly for Ubuntu and Debian. There are probably similar ways of accomplishing the exact same thing in other distros as well.

  1. If you want to install node and npm for all users, even users who don't have admin privileges, first you need to install them as a root (sudo) user.
sudo apt update
sudo apt upgrade
sudo apt install curl build-essential

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt install -y nodejs
  1. Make sure both node and npm are functional, use the expected version, and work globally on the machine.
node -v
npm -v
sudo node -v
sudo npm -v

If for some reason you have an old version, you can use nvm (Node Version Manager) to install and use whatever version you want.

Now you can switch to any user you want to be able to use node and npm. They should already be able to use node and npm but can't install global packages. To fix this, the user needs a separate directory to store the global modules in. This needs to be configured in .npmrc.

  1. We are going to change the prefix for the npm modules of the user by altering the option with the following line in the npm configuration file. This file needs to be called .npmrc and needs to be placed in the home directory of the user.
echo "prefix = ${HOME}/.npm/node_modules" >> ~/.npmrc

Adding the node modules binaries folder to your path is recommended. Add this to your shell config:

export PATH=/home/your_username/.npm/node_modules/bin:$PATH

And that's all you need to do to get all of node and npm running for any user on your machine! Enjoy :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment