This article will walk you through the step-by-step process of installing node.js (with npm) from source on Ubuntu. 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

Pre-Installation

Create the ~/.npmrc file:

vim ~/.npmrc

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

root = ~/.local/lib/node_modules
binroot = ~/.local/bin
manroot = ~/.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 your $PATH by adding the following to the end of the ~/.profile file:

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

Verifying the Installation

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

Clean up

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

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