15

It's probably a newbie question but I'm wondering why when I install node with nvm, it is only available for this user (it's not "global").

Let's say I'm log into the server with a user "admin":

curl https://raw.githubusercontent.com/creationix/nvm/v0.7.0/install.sh | sh
source ~/.profile

nvm install 0.10.30
nvm use 0.10.30

node -v
# outputs v0.10.30

Node is up and running for this user but when I switch to the root:

su
node -v

It displays:

The program 'node' can be found in the following packages:
 * node
 * nodejs-legacy
Try: apt-get install <selected package>

Why that? Is there a way to install node and make it available to all users? (I don't want to reinstall every time I need it for a new user.)

Julien Le Coupanec
  • 7,742
  • 9
  • 53
  • 67
  • because only the root user could install the various files into system directories, e.g. /usr/bin and whatnot. as a normal user, you're limited to where you can write stuff, which is generally just your own home directory. `/home/admin/bin` is highly unlikely to be in any other user's $PATH. – Marc B Aug 18 '14 at 17:11

1 Answers1

35

The problem is that NVM installs node.js to a user's local directory, and updates that user's .profile.

Here's a one line script that can copy your install to /usr/local/bin, where everybody can use node.js:

https://www.digitalocean.com/community/tutorials/how-to-install-node-js-with-nvm-node-version-manager-on-a-vps

n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local
FoggyDay
  • 11,962
  • 4
  • 34
  • 48
  • For me is: n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/ – The Anh Nguyen Jul 16 '21 at 07:10
  • This just blanket sets 755 on /usr/local which is a terrible idea and breaks sudo – Kyle Crossman Aug 27 '22 at 00:16
  • @Kyle Crossman - one of the first things I do in a typical Linux terminal is run `sudo bash`, and just work in my root session :) But that's safely on a VM, completely isolated from the mean, hostile "real world" ;) Q: Out of curiosity, what group and what permissions would you suggest? Remember: "755" *excludes* write access for everybody besides "owner"... – FoggyDay Aug 27 '22 at 00:32
  • @FoggyDay So, on Ubuntu the main issue is you are in a lot of trouble if you don't have the ability to log in as root, which is not entirely uncommon in cloud environments. /usr/local/bin/sudo requires the setuid bit to be set, which 755 will overwrite. So now you can no longer run root level commands via sudo on a machine you don't have a root login for. Now you need to rebuild your server, get help from an admin, or have an uncomfortable conversation with your boss. Ultimately, a better solution for more use cases would be to have a more targeted approach in the folders being updated – Kyle Crossman Oct 07 '22 at 03:15
  • 1
    Sigh... So all the user has to do is an additional `chmod 4755 /usr/local/bin/sudo` (r equivalent) after the "find"... – FoggyDay Nov 09 '22 at 21:03