Using nvm to get rid of sudo in your Node.js workflow

Unless you read this or a similar article before you installed Node.js, you likely installed node the conventional way, which puts important directories into root protected areas of your system. This makes it difficult to fully automate deployments, including npm package updates, without involving root. This post will cover how to get the most out of nvm, both for development and deployment.

Node Version Manager, aka nvm, is a tool for maintaining multiple versions of Node.js and io.js on a single system. In addition to this, it installs these versions and their important directories into userspace, which makes them accessible without the use of sudo.

Before you begin

If you have Node.js already installed, you will want to remove it prior to proceeding. For details on a manual cleanup, see this answer on Stack Overflow.


nvm requires that you have a c++ compiler installed. So, start by installing Xcode and the command line tools if you are running OSX or apt-get install build-essential libssl-dev if you are running Ubuntu or some other Debian flavor. Next, clone the nvm repo into the .nvm directory of your home directory as follows:

git clone ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`

Then, activate nvm in the context of your current shell with this:

source ~/.nvm/

Let's install io.js and make it our default:

nvm install iojs; nvm alias default iojs;

You should now be able to operate as usual with the node and npm commands. You will find files related to nvm and npm in the ~/.nvm and ~/.npm directories, respectively.


There is one catch to this process: sourcing the script is not persistent between reboots. So, we need to add that to our .bashrc. However, your .bashrc may terminate early for non-interactive shells, which includes ssh remote commands that can be useful for deployment scripts. If that's the case, you'll probably find something like this near the top:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;

Just source above that chunk and you will be able to use node both in your interactive shells and via ssh remote commands.

Deploying with nvm

This should be very climactic, but it isn't. Everything should just work, as long as the user you choose to run your node application under is properly sourcing the script in their .bashrc, you can now write deployment scripts that don't need root access, even to run npm update. Here's an excerpt from mine that works wonders:

git pull
npm install
bower install
node build.js

Notice how there is no need to do sudo npm install.

Upgrading nvm

This one is easy. Since we installed manually from git, you can update by pulling and checking out the latest tag like this:

cd ~/.nvm && git checkout master && git pull && git checkout `git describe --abbrev=0 --tags`

You're all set to use nvm in development and production without dirtying the process with sudo, su, or root logins.


The previous upgrade command did not work after the first use. The repo would become detatched from a branch and the pull would fail.

nvmNode.jsJavaScriptdevopsenvironmentquick tips