CrazySynthax
CrazySynthax

Reputation: 15058

How to use 'npm install' without 'sudo'?

Unfortunately, I can't just type 'npm install ' and install the package. I must use 'sudo npm install '. Do you know how to make npm work without running it as administrator?

Upvotes: 1

Views: 2989

Answers (1)

sgouros
sgouros

Reputation: 494

Uninstall any existing instances:

sudo npm uninstall npm -g
sudo rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/{npm*,node*,man1/node*}
sudo rm -rf /usr/local/bin/npm /usr/local/share/man/man1/node* /usr/local/lib/dtrace/node.d ~/.npm ~/.node-gyp /opt/local/bin/node opt/local/include/node /opt/local/lib/node_modules

Install any missing required libraries:

sudo apt-get install build-essential g++ flex bison gperf ruby perl libsqlite3-dev libfontconfig1-dev libicu-dev libfreetype6 libssl-dev libpng-dev libjpeg-dev python libx11-dev libxext-dev
sudo apt-get install python-dev

Download and install nvm:

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

Ensure permanent access from every bash instance:

sudo gedit ~/.bash_profile 

when the file opens in the editor, type the following, save and exit gedit:

export NVM_DIR="/home/your_username/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm

next, run:

nvm ls-remote

and choose a version you like from the list (I prefer current LTS). Then install it like this:

nvm install 6.11.0

check if everything is ok:

npm -v

now you should be able to run npm without sudo

Upvotes: 7

Related Questions