In previous post “How to install Nodejs on Ubuntu using NVM”, I have introduced the way to setup Node.js environment using NVM (Nodejs Version Manager). Usually, nvm is used to setup development environment, so it will be installed in individual user account.

However, sometime you want to install Nodejs for all users, example for testing deployment in different users or roles. You also want to switch Nodejs to run in many different versions. The solution is to install NVM in /opt/nvm as root. The directory “/opt/nvm” seemed like an appropriate location.

Clone the nvm source code into /opt/nvm directory:

# git clone git@github.com: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
export NPM_CONFIG_PREFIX=/usr/local/node
export PATH="/usr/local/node/bin:$PATH"

source /opt/nvm/nvm.sh

Re-login to a shell session, then set the default node version:

# nvm install 0.10.37
# nvm alias default 0.10.37

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.

Comments