Qua locus

Keeping both eyes on the long game.

How I installed NVM and Node.JS globally


This blog has been split in three and all content moved to the new blogs – Qua Locus LifeQua Locus Tech and Qua Locus Puzzle – comments are disabled here.

Node.JS is a great little server and it’s also very easy to install and set up. In the same fashion, NVM is a fantastic tool for installing and switching from one installed version of Node to another. The problem arises when you develop as one user but the deployed application needs to be run as a different user and you need to guarantee that the Node configurations they are using are identical. How do you use NVM to install node globally for all users?

node

What worked

The following worked for me on Ubuntu 12.04. All users automatically have access to a consistent version of Node and you have to be root (or “sudo -i”) to install a new node version. Individual users could also install Node locally if they want to develop against a different version.

sudo git clone https://github.com/creationix/nvm.git /opt/nvm-repo
# ^^^ this command clones the repo from github as root
sudo mkdir /opt/nvm
sudo chmod a+rx /opt/nvm
echo "export NVM_DIR=/opt/nvm" | sudo tee -a /root/.profile
echo "source /opt/nvm-repo/nvm.sh" | sudo tee -a /root/.profile
sudo su -
# ^^^ To use 'sudo su' or just 'sudo' instead of 'sudo su -' or 'sudo -i':
#     source /root/.profile in /root/.bashrc

# And then all of the following as root:
nvm install 0.10.26
ln -s /opt/nvm/v0.10.26/bin/node /usr/local/bin/node
npm install -g nodemon
ln -s /opt/nvm/v0.10.26/bin/nodemon /usr/local/bin/nodemon
npm install -g forever
ln -s /opt/nvm/v0.10.26/bin/forever /usr/local/bin/forever
npm install -g grunt-cli
ln -s /opt/nvm/v0.10.26/bin/grunt /usr/local/bin/grunt
# And likewise for any other packages you want to use

Changing the global node version

One of the weaknesses in this approach is that you can’t just issue “nvm use” commands to change the version of Node, either as a user or as root. The solution to that is a small bash script (here) that I’ve added to my homedir. It would be straight forward to install this script globally as well.

Why it works

By default, NVM installs to ~/.nvm and you do NOT want to make your home directory globally readable. This approach works because it installs NVM and the Node versions somewhere else and makes them globally readable. It then ensures that the desired version is on the path for all users by symlinking to the desired version from /usr/local/bin. To be honest, pretty simple.

Note that you can’t just copy the bin files to /usr/local/bin but have to symlink to them because Nodemon and others are javascript files that specify paths relative to their install location. Also, using symlinks makes it more obvious which version is currently being used.

Advertisements

Written by Christo Fogelberg

April 19, 02014 at 11:19

Posted in Tech

Tagged with , , ,

One Response

Subscribe to comments with RSS.

  1. […] but that didn't help, either. I finally tried reinstalling Node.js, npm, and Cordova globally using this guide so that can everything is accessible to both user and root, and that seems to be working for […]


Comments are closed.

%d bloggers like this: