NVM & Node.js - Recommended install for all users

Viewed 67785

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

8 Answers

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

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

There is also this fork of nvm designed for global usage: https://github.com/xtuple/nvm

wget -qO- https://raw.githubusercontent.com/xtuple/nvm/master/install.sh | sudo bash

sudo chown -R $USER /usr/local/nvm

nvm install 8

Update : I tried various ways to use xtuple's nvm and also n to manage a global node environment and I always ran into edge cases where there were issues. In the end what worked best for me was to download a few versions of node from their website and uncompress them to /usr/local. Then update my path with the version I want. e.g.

export PATH=/usr/local/node-v7.10.1-linux-x64/bin:$PATH

Note: You will probably have to chmod 777 the node path or dedicate one user to mange it.

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.

Related