NPM modules won't install globally without sudo
Asked Answered
O

17

141

I have just reinstalled Ubuntu 12.04 LTS, and before anything else i did these steps:

  1. Installed Node via package manager with the following script

    sudo apt-get update
    
    sudo apt-get install python-software-properties python g++ make
    
    sudo add-apt-repository ppa:chris-lea/node.js
    
    sudo apt-get update
    
    sudo apt-get install nodejs
    
  2. Tried to install yeoman, express, n, yeoman's generators globally and all of them returned the same error

    npm ERR! Error: EACCES, symlink '../lib/node_modules/n/bin/n'

    npm ERR! { [Error: EACCES, symlink '../lib/node_modules/n/bin/n'] errno: 3, code: 'EACCES', path: '../lib/node_modules/n/bin/n' }

    npm ERR!

    npm ERR! Please try running this command again as root/Administrator.

    npm ERR! System Linux 3.8.0-29-generic

    npm ERR! command "/usr/bin/node" "/usr/bin/npm" "install" "-g" "-d" "n"

    npm ERR! cwd /home/heberlz

    npm ERR! node -v v0.10.20

    npm ERR! npm -v 1.3.11

    npm ERR! path ../lib/node_modules/n/bin/n

    npm ERR! code EACCES

    npm ERR! errno 3

    npm ERR! stack Error: EACCES, symlink '../lib/node_modules/n/bin/n'

    npm ERR!

    npm ERR! Additional logging details can be found in:

    npm ERR! /home/heberlz/npm-debug.log

    npm ERR! not ok code 0

  3. Reclaimed ownership of the following folders recursively ~/.npm, /usr/lib/node, /usr/lib/node_modules, and of the following symlinks /usr/bin/node, /usr/bin/nodejs with absolutely no success

I need to install yeoman and its generators without sudo not to be in trouble later on :(

Outspan answered 14/10, 2013 at 3:13 Comment(3)
If you want npm to install global packages in $HOME rather than /usr, follow the guide github.com/sindresorhus/guides/blob/master/…Navvy
this is what worked for me github.com/nodejs/node-v0.x-archive/issues/… : ln -s /usr/bin/nodejs /usr/bin/nodeCelesta
Possible duplicate of npm throws error without sudoParhelion
A
332

Ubuntu 12.04 and using Chris Lea's PPA for install the following works for me:

npm config set prefix '~/.npm-packages'

and adding $HOME/.npm-packages/bin to $PATH

Append to .bashrc

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

For more see this answer from @passy

Angulation answered 11/2, 2014 at 20:12 Comment(6)
This looks like a much cleaner solution. If you don't like system-folders in your home directory, you could also use a location such as ~/.local/share/npm.Paratuberculosis
It should be noted that after you change the prefix, you should reinstall all global modules that you had installed previously, including npm itself.Washy
This seems fine, but I found it much easier to give user's permission to write to /usr/lib/node_modules/. Or maybe even appropriate to create a nodejs unix group and give that group permission to write to that directory.Solus
One small detail to add is that you have to make the ~/npm directory yourself. Else, npm will exit with a ENOENT errors.Became
work for ubuntu 14.04, using nodejs install by digitalocean digitalocean.com/community/tutorials/…Herisau
You may also use environment vars instead of npm config (so you keep it even when uninstalling or apt purge npm). For example, to set it globally for all users, in /etc/profile.d/npm.sh: export PATH="$PATH:/home/shared/npm/bin"; export NPM_CONFIG_PREFIX="/home/shared/npm";. In this example I have /home/shared/npm with write permissions for a npmjs group, which contains the users who can install npm packages. Also you might want to uninstall all packages and reinstall after changing the path.Renell
B
23

If you already have $HOME/bin in your path, a simpler solution is just ...

npm config set prefix ~
  • New node commands will now install into your $HOME/bin directory.
  • No need to change your path!

Since this discussion is really about reducing the security risks of running sudo, you should also be aware that any node app could potentially be installing an app name that does not match the registered node package name you think you're installing. So there is a security risk that an npm install will replace an existing system command or one you already have in $HOME/bin. If you're concerned, check the bin, and scripts properties in the package.json file of the app you're installing first.

In general, it's safest to:

  • (a) Place $HOME/bin last in your path so system commands are not superseded.
  • (b) don't include "." or any relative path in your $PATH so you don't accidentally run a command that happens to be in the current directory.

Reference:

Bowie answered 6/8, 2014 at 3:10 Comment(3)
Another important security issue with npm is that the source code you see on on a github.com repo for a node package may not the same code that you're installing from the npm.org registry server!Nutshell
While in theory a well crafted node package will happily install in ~/bin, some node packages (or their dependant packages) have only been tested as installing with sudo so be aware that you may be encounter an untested "installation environment" issue. Like any other environment issue (like change of OS) the principle of "write once, test everywhere" still holds true.Nutshell
The disadvantage of this method is npm creates an additional directory, called ~/lib. Depending on your organization practices, this may not be desirable.Alex
E
12

As for October 2014:

Node.js is available from the NodeSource Debian and Ubuntu binary distributions repository.

curl -sL https://deb.nodesource.com/setup | sudo bash -
sudo apt-get install -y nodejs

That's it.

Outdated answer:

The fastest way without using sudo is like described here by isaac

I strongly encourage you not to do package management with sudo! Packages can run arbitrary scripts, which makes sudoing a package manager command as safe as a chainsaw haircut. Sure, it's fast and definitely going to cut through any obstacles, but you might actually want that obstacle to stay there.

I recommend doing this once instead:

sudo chown -R $USER /usr/local

EDIT:

There are certain security concerns and functionality limitations regarding changing the ownership of /usr/local to the current user:

Having said that, if you want to install global module without using sudo, I don't see any better solution (from pragmatic point of view) than mentioned. Security vs easy of use is very broad topic, and there is no easy answer for that - it just depends on your requirements.

Echt answered 14/10, 2013 at 8:6 Comment(10)
This is a bad idea because it makes all of /usr/local belong to a single user.Mope
As @ReedG.Law pointed out, this is really a bad idea. Do not change hte ownership of /usr/local to any specific user.Talus
@ReedG.Law, ChristopherWill, thank you for the comments, I updated the answer, including security concerns you mentioned.Echt
I have to agree with Reed G Law - changing /usr/local is a BAD BAD BAD idea. In my case I was looking at directions and did so: then recalled that I have to SHARE this with another person doing development. I went back and (hopefully) got all the changes. It's not just security, but functionality as well.Recipe
Is there any downside to this solution if there is only one user on the system?Heeling
If you have only one user, there is no downside that I am aware ofEcht
Chowning /usr/local/node_modules and /usr/local/node seems to be enough.Reluctant
Sorry, meant /usr/local/lib/node_modules and nodeReluctant
So I tried the Oct 2014 answer on Ubuntu 14.04, and it doesn't install npm, if I do apt-get install npm then I have the same problem :(Tangled
@ReedG.Law Why is this a bad idea? There is only one single user using my machine. Me. I think is the best approach for local dev.Hosea
O
11

The issue was i installed node using sudo, to avoid errors when installing npm modules globally one MUST NEVER install node with sudo.

My solution was to reinstall node it this way:

Download latest stable node sources from nodejs.org #in my case node-v0.10.20.tar.gz

tar -zxf node-v0.10.20.tar.gz #uncompress sources

cd node-v0.10.20 #enter uncompressed folder

sudo chown -R $USER /usr/local

./configure --prefix=/usr/local && make && make install

One thing to note is that only taking ownership of the /usr/local folder wouldn't work in my case because node installation itself was made with sudo

Last step to install yeoman: #although at yeoman.io it says that doing "npm install -g yo" already installs bower and grunt, there are some submodules of grunt that fail, so i fixed that by installing it by itself

npm install -g bower

npm install -g grunt

npm install -g yo

npm install -g generator-angular

Outspan answered 14/10, 2013 at 6:11 Comment(2)
Great in theory, but is there any way to achieve the same effect without being forced to build Node and install 4 additional modules just to have access to modules globally?Fidelity
The selected answer works just as you need. This is what i did 2 years ago when there was no other public solution yet, but now it is uselessOutspan
A
5

I solved this problem with environment variable and shell alias:

export NPM_PREFIX=$HOME/node
alias npmg="npm -g --prefix $NPM_PREFIX"

For me npm did not honor the "prefix" config setting in .npmrc.

Angelo answered 7/6, 2014 at 19:5 Comment(0)
B
3

Find the path to npm's directory:

npm config get prefix

For many systems, this will be /usr/local.

Change the owner of npm's directories to the name of the current user (your username!):

sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}

This changes the permissions of the sub-folders used by npm and some other tools (lib/node_modules, bin, and share).

Here is the link for full details

https://docs.npmjs.com/getting-started/fixing-npm-permissions

Baseboard answered 31/7, 2017 at 10:30 Comment(2)
Vital. Try to avoid adding bin and share into brackets. Ignoring may lead to changing ownership of this folders to current user and problems with installing new packages to system with sudo because it`s ownership will be changed tooNipissing
this commend destroys all my file system : (Iodate
R
2

According to this similar SO post: npm throws error without sudo

Looks like you might have an ownership issue with ~/.npm directory.

As with the answer in that one, try:

sudo chown -R `whoami` ~/.npm
Ricks answered 4/4, 2014 at 4:46 Comment(6)
Actually this did not work as i explained on step 3 of what i tried when i couldn't make it work, the issue was that i had installed node using sudo, which may under some circumstances make trouble. Once i had deleted node and reinstalled it from its sources, everything started to work as expected!Outspan
@Outspan I think you're right, I just tried doing this on another machine yesterday night and it wasn't working. I had other permission issues to deal with too. Still not sure whats the best way not to run in to this installing npm from source.Ricks
Link to the question in the comments or mark the current question as a duplicate. It doesn't help to copy-paste answers from other questions.Irritated
@Outspan the downvote of this anwer is innapropiate as it address the title of the question you made.Prism
I don't remember when i downvoted the answer, but in my particular case what you wrote did not fix the issue, reason why i think i probably downvoted the answer. As stated on the question: "Reclaimed ownership of the following folders recursively ~/.npm, /usr/lib/node, /usr/lib/node_modules, and of the following symlinks /usr/bin/node, /usr/bin/nodejs with absolutely no success" I did try to gain ownership of .npm, and all those other folders without successOutspan
I don't remember when i downvoted the answer, but as stated in the question, what you wrote was one of the steps i tried and did not work, reason why i probably downvoted your answer. To be more specific, in this paragraph i state that i already tried that and did not work: "Reclaimed ownership of the following folders recursively ~/.npm, /usr/lib/node, /usr/lib/node_modules, and of the following symlinks /usr/bin/node, /usr/bin/nodejs with absolutely no success" CheersOutspan
I
1

If you're on a developping machine, you might be better off considering using nvm.

If not, you simply want to install using your favorite package manager.

Whatever the case may be, I'd recommend checking this answer on stackoverflow

Impermeable answered 20/2, 2015 at 15:30 Comment(0)
R
1

Actually, I just changed the permission of a user folder that was owned by root:

sudo chown -R $USER ~/.config/configstore

Then I could "npm install" and "bower install" without sudo! Worked fine!

Ramification answered 25/11, 2015 at 18:31 Comment(0)
B
1

The issue is due to a different owner of npm folders Best way to fix this permission issue by executing below command -

sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}

This changes the permissions of the sub-folders used by npm

Bartender answered 13/4, 2023 at 19:45 Comment(0)
P
0

This issue and other caused by the same reason can be solved installing Node in user space.

You can do it just copying and pasting in your terminal

NODEJS_ROOT=${NODEJS_ROOT:-~/nodejs}
cd /tmp
wget -N http://nodejs.org/dist/node-latest.tar.gz && tar xzf node-latest.tar.gz
NODEJS_CURRENT=$(tar tf node-latest.tar.gz|head -1)
mkdir -p $NODEJS_ROOT/$NODEJS_CURRENT
cd $NODEJS_CURRENT
./configure --prefix=$NODEJS_ROOT/$NODEJS_CURRENT && make install
cd $NODEJS_ROOT
rm current 2> /dev/null # Removes current symbolic link, if any
ln -s $NODEJS_CURRENT current

Same commands can be launched also to get Node updated to latest version.

Don't forget to edit your environment. Only once, do

echo "export NODEJS_ROOT=$NODEJS_ROOT"            >> $HOME/.bash_profile
echo 'export PATH=$NODEJS_ROOT/current/bin:$PATH' >> $HOME/.bash_profile
source $HOME/.bash_profile # reload your env, so you can use node right now

Check out this article as a reabout how to Install Node.js without sudo.

For a more general solution about this topic (i.e., install software locally) see dotsoftware.

Palinode answered 18/3, 2015 at 14:8 Comment(0)
G
0

using lubuntu 14.04.3, I tried changing ownership of .npm and npm prefix, updated my path, npm installed modules to my home directory without sudo but the path was incorrect so the modules like ember were not found, linuxbew solved the problem, quick setup guide here for node/npm

Ghetto answered 8/12, 2015 at 23:43 Comment(0)
N
0

just use nvm you will be able to dynamically switch between different node versions and it is installed locally for your user. No sudo needed. Just make sure you have fully cleaned up the old node beforehand.

https://github.com/nvm-sh/nvm

Nonconcurrence answered 23/4, 2020 at 4:36 Comment(0)
A
0

In order to install Node.js and npm locally without having to use sudo open the terminal and type:

 echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
    . ~/.bashrc
    mkdir ~/local
    mkdir ~/node-latest-install
    cd ~/node-latest-install
    wget -c http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
    ./configure --prefix=~/local
    make install 
    wget -c https://www.npmjs.org/install.sh | sh 
Arsenault answered 12/6, 2021 at 17:56 Comment(0)
O
0

Use NVM

I had the same "permission denied" issue. Instead of trying to fix the NodeJS installed from https://nodejs.org/en/ which installs into folders that are owned by root instead of $USER, I used NVM. node version manager.

  • Install it: run curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
  • Install a version of node: run nvm install 16
  • Use it:
    • nvm use 16
    • npm install -g yarn

Everything worked fine when I used Node installed by NVM.

Overshoot answered 21/9, 2022 at 7:38 Comment(0)
S
-2

The best solution I found was to install Node.js from the tar package on to user home directory & link the lib folder location. Here is what you need to do

This will install Nodejs under ~/.local/ instead of the default /usr/local/

Add this to your ~/.npmrc (create the file if it doesn't exist already):

root =    /home/YOUR-USERNAME/.local/lib/node_modules
binroot = /home/YOUR-USERNAME/.local/bin
manroot = /home/YOUR-USERNAME/.local/share/man
Download the Nodejs source code from nodejs.org and install it under your ~/.local tree:

tar xf node......
cd node........
./configure --prefix=~/.local
make
make install

Create ~/.node_modules symlink. (This directory will be automatically searched when you load modules using require "module" in scripts. I'm not sure why Node doesn't search ~/.local/lib/node_modules by default.)

cd
ln -s .local/lib/node_modules .node_modules
Is ~/.local/bin in your path? Type

which npm
If it says ~/.local/bin/npm, you're done.

Otherwise, do this...

export PATH=$HOME/.local/bin:$PATH
...and add that line to your ~/.profile file, so it'll run every time you log in.

If you still encounter ownership or permission error while installing packages, then change ownership of ~/.local/ dir by running

chown -R user:user ~/.local/

Now you should be good to install packages via 'npm'

Note: ALL OF THE ABOVE COMMANDS ARE TO BE RUN AS USER. DO NOT USE SUDO OR ROOT LOGIN

NEVER EVER CHANGE THE PERMISSION OF FOLDERS UNDER '/USR/LIB/'. WILL LEAD TO UNSTABLE OS

Solana answered 2/7, 2014 at 5:40 Comment(1)
Copied from tnovelli.net/blog/blog.2011-08-27.node-npm-user-install.html without attribution.Embryo
S
-2

I find Pawel Grzybek's explanations very convincing: They boil down to 3 simple sudo commands, never having to use sudo again for global npm installs:

sudo chown -R $(whoami) /usr/local/lib/node_modules
sudo chown -R $(whoami) /usr/local/bin
sudo chown -R $(whoami) /usr/local/share
Sera answered 28/10, 2016 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.