If you already know why you want to install node.js as a non-root user, go ahead to the next paragraph. Otherwise you may want to check out this stackoverflow answer that explains the situation quite well.

This article will walk you through the step-by-step process of installing node.js (with npm) on Ubuntu as a non-root user. If you're using a linux distribution other than Ubuntu, you can probably still follow the instructions here, except with a few minor changes to match your distro's way of doing things.

Dependencies

Install build dependencies:

sudo apt-get install g++ make

Create Node.js User

Create a new user just for node.js; this will help isolate the node.js process(es):

sudo useradd -d /home/nodejs -m nodejs

Pre-Installation

Open a su session as the nodejs user:

sudo su - nodejs

Create the ~/.npmrc file:

vim ~/.npmrc

Set the contents of the ~/.npmrc file to the following:

root = /home/nodejs/.local/lib/node_modules
binroot = /home/nodejs/.local/bin
manroot = /home/nodejs/.local/share/man

Create the ~/.local directory:

mkdir ~/.local

Building Node.js from Source

Download the latest stable release of nodejs. We'll use 0.10.29 here, because that was the latest stable release as of the time this article was written:

wget https://github.com/joyent/node/archive/v0.10.29.tar.gz ~/

Unpack the gzipped archive:

tar -xzvf ~/v0.10.29.tar.gz

Change directories into the directory that was just created:

cd ~/node-0.10.29

Configure the build:

./configure --prefix=~/.local

If you encountered errors during the configure step, you may need to check that all dependencies are met by looking at the README.md file in the unpacked source directory.

After you have successfully configured the build, you can initiate the build process:

make

This will take a while.

Installing Node.js

Once the build process has finished, run the installation:

make install

Create .node_modules symlink:

ln -s ~/.local/lib/node_modules ~/.node_modules

Add npm to the nodejs user's $PATH by adding the following to the end of the ~/.profile file:

export PATH=$HOME/.local/bin:$PATH

Exit the su session:

exit

Verifying the Installation

Open a su session as the nodejs user:

sudo su - nodejs

To verify that node was installed correctly:

node -v

Should output the correct version number for node, like this:

v0.10.29

And to verify that npm was installed correctly:

npm -v

Should output the correct version number for npm, like this:

1.4.14

Exit the su session:

exit

Clean up

Once everything has been installed and is working properly, you can go ahead and delete the source files:

rm -R /home/nodejs/node-0.10.29
rm /home/nodejs/v0.10.29.tar.gz

Using Node.js

Now anytime you wish to use node.js, start a su session as the nodejs user:

sudo su - nodejs

Then, to run a node app:

node your-app.js

Or, to use npm:

npm help

Instead of doing the separate steps above, here's a more compact way to run a node app:

sudo su - nodejs -c "node your-app.js"

Once you've got your node app running, you can verify that the nodejs user is in fact running the node process:

ps aux | grep your-app.js