NVM & Node.js - Recommended install for all users
node.jsNvmnode.js Problem Overview
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
not sure if there is a better way?
thanks
node.js Solutions
Solution 1 - node.js
Here is what I did:
-
Installed
nvm
in/opt/nvm
as root. Seemed like an appropriate location.# git clone [email protected]:creationix/nvm.git /opt/nvm
-
Created the directory
/usr/local/nvm
. This is where the downloads will go ($NVM_DIR
)# mkdir /usr/local/nvm
-
Create the directory
/usr/local/node
. This is where the NPM global stuff will go:# mkdir /usr/local/node
-
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"
-
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.
Solution 2 - node.js
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.
Solution 3 - node.js
- Login as root:
sudo -s
- Install nvm:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | NVM_DIR=/usr/local/nvm bash
- Created 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
- Run
/etc/profile.d/nvm.sh
- Install node:
nvm install node
- Optionally update npm with:
npm install -g npm
Solution 4 - node.js
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
Solution 5 - node.js
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'
Solution 6 - node.js
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.
Solution 7 - node.js
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