Update: 2012/02/21

As Jonathan pointed out below, the information in this post has been made obsolete by the now supported ‘–prefix’ option in the Node.js configure scripts. As the instructions point out:

./configure --prefix=/your/chosen/path

The preceding code should be sufficient to install Node.js as a local user, and without a need for sudo.

The original post is being left intact for posterity (pun maybe intended) and because it has become the the most popular on this blog to date. Thanks for reading; hope you find something useful here.

—

The default installation instructions for node.js require using sudo, in most cases. Here’s how to install it as a non-root user. If you are already familiar with why running as a non-root user may be preferable, and have a good grasp of how to build software from source, you’ll probably get the most out of the Short version.

As Julian Gautier pointed out, the “git clone” commands in this post will clone the master branch (bleeding edge development version) of node. Presently, this branch holds v4, which is not backwards compatible with some of the available node packages.

If you need backwards compatibility, you may want to clone the v2 branch. Just replace the git clone commands below with the following:



$ git clone https://github.com/joyent/node.git -b v0.2



Thanks to Julian for pointing this out!

Short version:



$ git clone https://github.com/joyent/node.git

$ cd node

$ mkdir ~/opt

$ export PREFIX=~/opt; ./configure

$ make

$ make install

$ echo 'export PATH=~/opt/bin:${PATH}' >> ~/.bashrc



Enjoy.

If those commands seem obtuse, keep reading, all will be explained in the Long version.

Long version:

Why you should care.

Installing node as a root user means that installing npm, and npm packages also requires the use of sudo. So, you’ll be installing 3rd party libraries of code that have root access to your entire filesystem. But, you’re reading all of the source code for the libraries you use, right? Right.

While in practice, if you trust the libraries you’re using, everything should run along just fine. However, on the Ruby side of life, RVM has made a spoiled rotten developer out of me. It provides the simplest way I know to easily manage multiple versions of Ruby, and keep each version sequestered away with its own libraries and binaries. I use it with gemsets, to keep projects sandboxed, and avoid the hassle of version conflicts between dependency requirements for different projects.

Ndistro seems to fill a similar use case, but it’s newer, and I haven’t spent as much time with it, yet.

At any rate, trying to learn and work on node.js has been a little painful, since things don’t work precisely as expected (that is, with the same kind of seamless workflow which is easily achieved using RVM + Ruby + RubyGems + (your favorite framework) + Autotest…) At the very least, it seems like installing node, npm, and related packages should not require the use of ‘sudo’ – what happens when we try it out of the box?

After cloning the project, configuring the build, and giving the ‘make install’ command, ugliness ensues.



Cannot create folder '/usr/local/include/node/'

(original error: [Errno 13] Permission denied: '/usr/local/include/node/')

make: *** [install] Error 1



It’s trying to create files in ‘/usr/local’, which is owned by none other than ‘root’, see?



$ ls -la /usr

drwxr-xr-x 10 root root 4096 2010-07-21 14:18 local



So it looks like we’ll need to change the install path somehow. Reading through the configuration script shows it’s executing another script called ‘waf-light’, which in turn calls another script, which…

Long story short,

the build/install process ultimately looks at a file named ‘wscript‘ in the project root. Right around line 550 in this file looks to be the relevant part. It checks for the codesence of an environment variable called ‘PREFIX‘ and installs the project there. It stands to reason then, that if we set that variable to something in our home directory, ~/opt, for example, that it will install there. Shall we?

Create the desired install directory

$ mkdir ~/opt

Set the ‘PREFIX’ environment variable

$ export PREFIX=~/opt

Clone the repository

$ git clone https://github.com/joyent/node.git

Enter the project directory

$ cd node

Run the configuration script

$ ./configure

If all goes well, you should see something like

'configure' finished successfully (1.298s)

Now build the project with make

$ make

Expect output like

'build' finished successfully (57.711s)

Then install the project

$ make install

WIN!

'install' finished successfully (0.186s)

I CAN HAZ NODE?



$ node

The program 'node' is currently not installed. You can install it by typing:

sudo apt-get install node



FAIL. Why?

Because it’s not in our ‘PATH‘ environment variable yet! So, add ‘~/opt/bin‘ to ‘PATH‘ and it should find the node.js binary (executable program.)



$ export PATH=~/opt/bin:${PATH}

$ which node

~/opt/bin/node



Does it run?



$ node

Type '.help' for options.

node>



Does it ever.

This isn’t all, though. Your ‘PATH‘ environment variable will be reset each time you open a new command shell. So, if you’re using Bash, just put that export line in your ‘~/.bashrc‘ file (on OSX you’ll want to add it to your ‘~/.bash_profile‘)

$ echo 'export PATH=~/opt/bin:${PATH}' >> ~/.bashrc

when you start up a new shell, node should work as expected.

Good luck, and until next time, happy hacking!

Update, 2011/02/25 – Updated the uri of the git repository and tested the instructions with master (v0.4)