78

is there a recommended install for nvm so all users can use it? i cannot find anything on the web regarding this.

this is what i did

  • installed nvm in a common directory
  • put the nvm.sh script locationin .profile for all users
  • created a nvm/alias directory (nvm complains if this is not here for other users)

then each user must either run "nvm use " or put it in their profile by default

not sure if there is a better way?

thanks

jadent
  • 3,674
  • 5
  • 27
  • 32

7 Answers7

57

Here is what I did:

  1. Installed nvm in /opt/nvm as root. Seemed like an appropriate location.

    # git clone git@github.com:creationix/nvm.git /opt/nvm
    
  2. Created the directory /usr/local/nvm. This is where the downloads will go ($NVM_DIR)

    # mkdir /usr/local/nvm
    
  3. Create the directory /usr/local/node. This is where the NPM global stuff will go:

    # mkdir /usr/local/node
    
  4. Created a file called nvm.sh in /etc/profile.d with the following contents:

    export NVM_DIR=/usr/local/nvm
    source /opt/nvm/nvm.sh
    
    export NPM_CONFIG_PREFIX=/usr/local/node
    export PATH="/usr/local/node/bin:$PATH"
    
  5. Re-login to a shell session, then set the default node version.

    # nvm install 0.10
    # nvm alias default 0.10
    

The node binaries should now be in the PATH for all users the next time you login to a shell session. NPM will install global things to the /usr/local/node prefix.

Tim Smart
  • 1,015
  • 10
  • 10
  • 1
    Great setup - however NPM_CONFIG_PREFIX isn't being respected when I follow this setup and global modules are being installed in nvm against the current node version. Any ideas? – leepowell Aug 28 '14 at 15:46
  • 3
    I miss a step 4.5: `source /etc/profile.d/nvm.sh`. Only then nvm is available to me. – czerasz Aug 13 '15 at 08:40
  • @czerasz you shouldn't have to do that; try logging in again? I ran into that too but it went away after I logged out/in – jcollum Dec 14 '15 at 22:21
  • I did this but for some reason when I try to install it tells me that I don't have permission. So I try it with sudo and it says it can't find nvm. – jcollum Dec 14 '15 at 22:24
  • @jcollum I agree that re-logging is a nice way to enable nvm. but when you need to install NVM and do something with it in one script then you need to load it manually. – czerasz Dec 16 '15 at 08:57
  • not required in recent version: step3 and in the file: export NPM_CONFIG_PREFIX=/usr/local/node – Shimon Doodkin Dec 23 '15 at 12:01
  • 5
    I tried following these instructions. When I ran `nvm install node` i get the following error `nvm is not compatible with the "NPM_CONFIG_PREFIX" environment variable: currently set to "/usr/local/node"` – Mr. Doomsbuster Dec 31 '15 at 02:07
  • @TechPro I'm getting that too: `nvm is not compatible with the "NPM_CONFIG_PREFIX" environment variable: currently set to "/usr/local/node"; Run `unset NPM_CONFIG_PREFIX` to unset it` .. I wonder if unsetting that is wise. – jcollum Jan 08 '16 at 20:55
  • 95
    `nvm` maintainer here. `nvm` is NOT COMPATIBLE with the "prefix" option, and you should NOT EVER install `nvm` as root. `nvm` is per-user. If you want to share node across users, `nvm` is the wrong tool to use. – LJHarb Feb 04 '16 at 16:51
  • 9
    I think comment from @LJHarb need more attention here. He is the maintainer of the nvm afterall. Maybe you should post that comment as answer? – chenz Feb 11 '16 at 04:01
  • 1
    @LJHarb is it still true in 2019? – Oleg Abrazhaev Jan 16 '19 at 11:42
  • 4
    Yes, it will be true forever. There's no reason to use the "prefix" option at all anyways, and nvm can't possibly ever be compatible with it. – LJHarb Jan 17 '19 at 21:28
  • 1
    @LJHarb great man lesson learned here - ready the comments first !! – Raja Khoury Apr 05 '20 at 19:16
  • 1
    Installing a utility like this at the system level for all users removes the option for each user to choose which version of the utility they wish to run. It's common to create an application based on specific versions of tools, libraries, etc.. This creates the opportunity to test new versions of dependencies and help ensure that bugs or security issues are not introduced when upgrading. – Lazor Dec 08 '20 at 00:23
51

It's best to install one copy of node globally so that other users can access it. To do this, run the following command (entering your user's password at the prompt):

n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local

This commend is copying whatever version of node you have active via nvm into the /usr/local/ directory and setting the permissions so that all users can access them.

To check that it works, become the root user and do another which command to make sure that node is now installed to /usr/local/bin:

sudo -s
which node

If you ever want to change the version of node that's installed system wide, just do another nvm use vXX.XX.XX to switch your user's node to the version you want, and then re-run the first command above to copy it to the system directory.

Glowin
  • 4,906
  • 1
  • 16
  • 10
  • 21
    Please credit sources: https://www.digitalocean.com/community/tutorials/how-to-install-node-js-with-nvm-node-version-manager-on-a-vps – joemaller Feb 01 '15 at 16:24
  • 2
    this broke a lot of permissions for myself, just a fyi – Shane Sep 23 '15 at 02:09
  • 1
    This is most certainly not "best". It may be "convenient" but also the opposite of best if you need to run multiple versions of node under different user accounts. Using `nvm` (_never_ installed as root) is the only easily applied option there. – Mike 'Pomax' Kamermans Dec 11 '21 at 17:38
  • I'm getting an `cp: cannot stat '/{bin,lib,share}': No such file or directory` error here, on ubuntu debian. Any ideas for me? – Seth Lutske Feb 07 '22 at 20:22
  • This command can break your sudo. Strongly reconsider using this – Kyle Crossman Aug 26 '22 at 21:05
  • Why this command will break sudo? Anyone? I'm interesting in doing the same in VPS – gremo Feb 10 '23 at 17:46
28
  1. Login as root: sudo -s

  2. Create destination folder: mkdir /usr/local/nvm

  3. Install nvm: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | NVM_DIR=/usr/local/nvm bash

  4. Create a file called nvm.sh in /etc/profile.d with the following contents:

    #!/usr/bin/env bash
    export NVM_DIR="/usr/local/nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
    
  5. Set permissions chmod 755 /etc/profile.d/nvm.sh

  6. Run /etc/profile.d/nvm.sh

  7. Install node: nvm install node

  8. Optionally update npm with: npm install -g npm

Pablo Bianchi
  • 1,824
  • 1
  • 26
  • 30
Biscar
  • 381
  • 3
  • 4
9

Install NVM on your Linux server, after that install node version using NVM (run all the command as root user). After that run the below command for all the users get nodejs available with nvm

n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local

The above command is a bit complicated, but all it’s doing is copying whatever version of node you have active via nvm into the /usr/local/ directory (where user installed global files should live on a linux VPS/server) and setting the permissions so that all users can access them.

/root/.nvm/versions/node/v8.10.0/bin/node

Switch the user name check your node version.

su - username
which node
/usr/local/bin/node
bibincatchme
  • 333
  • 4
  • 10
  • 1
    This command can break your sudo. Strongly reconsider using this as if you don't have a root account you will be in for a world of pain – Kyle Crossman Aug 26 '22 at 21:20
6

Since LJHarb recommends not installing this globally, I decided to create a script to install nvm when you login to the server. I needed this as I had several users setup that may login, but needed access to pm2 (to monitor one of our applications).

Create the script in /etc/profile.d/ (named nvm.sh for example):

#!/bin/bash
NODE_VER=6.2.2
if [ ! -f ~/.nvm/nvm.sh ]; then
    # May need to be updated with the latest nvm release
    wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh | bash
fi
source ~/.nvm/nvm.sh
if ! command -v node | grep -q $NODE_VER; then
    echo "Node is not installed"
    nvm install $NODE_VER
    nvm alias default $NODE_VER
fi

For our application, we needed pm2 shared between users:

if ! command -v pm2 &>/dev/null; then
    echo "pm2 not installed"
    npm install -g pm2
fi
# Share pm2 configuration between users
alias pm2='env HOME=/opt/sora pm2'
matth
  • 6,112
  • 4
  • 37
  • 43
  • You might want to add NVM_DIR="" to the beginning too. If you don't, then 'su-ing' into another user will cause the install to fail. This is because NVM_DIR is used by the install script, but not initially set before it's used. So it has the value set, which might actually be the previous user. – DerekE Nov 16 '16 at 21:50
1

I had a small VPS with two users, wanted to avoid installing multiple copies of node due to limited space

Simply moved $HOME/.nvm of user that had NVM to /opt/nvm and chmod -R 777 /opt/nvm so writable by all users

Then set each user's NVM_HOME in .bashrc to /opt/nvm

Seemed like most obvious solution to me, since one user had a pre-existing .nvm folder

Re: ugly permissions, this copy will only be used for development purposes - I would def recommend anyone using node in production not consider this method

AveryFreeman
  • 1,061
  • 2
  • 14
  • 23
0

I found a cleaner way, and this way you can probably have multiple versions with tweaks, I haven't tried it.

Login to your root account and follow these steps.

  1. install nvm
  2. install node with it, eg:
nvm install 14
  1. run
n=$(which node);
n=${n%/bin/node};cd $n;cd ..;
ls
  1. pick a version and run it in :
node_v=the_version_you_picked
  1. run
mv $node_v /usr/local/node/$node_v;cd /usr/local/node/$node_v;chmod -R 755 ./

echo "PATH=\${PATH}:/usr/local/node/$node_v/bin;export PATH" > /etc/profile.d/gloabl_node.sh

and exit and re-login, now run which node or node -v to confirm it's working.

Now you can rename the bin files and all the other configs it needs to run multiple versions. eg: rename node -> node-14 and so on.

Steve Moretz
  • 2,758
  • 1
  • 17
  • 31