Install Node.js on UNIX environment

There are many ways to install Node.js, you can just choose whatever you like. We will talk about Windows environment later.

First of all, please make sure you have prerequisites. If you are using Mac, please make sure you have XCode.

$ cc --version
i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ python
Python 2.7.1 (r271:86832, Aug 5 2011, 03:30:24)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

Before you start to install Node.js, you might need to check if you already installed wget and git. If not, please install them before you start.

For Mac user

You can use Homebrew to manage those packages. It's very easy to use, please refer Homebrew to check more information.
$ ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"
$ brew install wget
$ brew install git

For Unix User

Unix usually has wget inside, and you can just download Git and manually install it. For more information, please check Introduction to GIT on Linux – Install, Create Project, Commit Files

$ mkdir git_src
$ cd git_src
$ wget http://kernel.org/pub/software/scm/git/git-1.7.6.tar.bz2
$ make
$ make install

Install Node.js

After all those preparations, we can start to install Node.js now. And there are four different ways for Node.js installation.

If you just want to test something about Node.js, your can try first two ways; and for Windows user, using .exe file to install Node.js automatically is probably the easiest way. But if you want to start some serious Node.js development, nvm could be your best friend.

I. Easy Installation

Just go to http://nodejs.org/ and click Install. Just install it like a normal software.

II. Manually Install

$ mkdir src
$ cd src
$ wget http://nodejs.org/dist/node-v0.4.8.tar.gz
$ tar xvfz node-v0.8.15.tar.gz
$ cd node-v0.8.15
$ ./configure --prefix=$HOME/node/v0.8.15
$ make
$ make install
$ echo 'export PATH=$HOME/node/0.8.15/bin:${PATH}' >> ~/.bashrc
$ source ~/.bashrc

If you want to uninstall this package, please use:

$ cd node-v0.8.15
$ make uninstall

III. Using Homebrew to install Node.js

Mac OS user can use Homebrew to install Node.js in a very easy way. It will automatically install the latest version of Node.js for you.

$ brew install node

If you need to uninstall Node.js, please use:

$ brew uninstall node

IV. Using nvm to install Node.js

nvm is the abbreviation of Node Version Manager. It can help you to switch between different version of Node.js. The reason we need to do this is because Node.js changes very fast, and nvm can help developers to update the latest or fallback to previous version. However, if you don't need to worry about switching between different versions of Node.js, then you won't need to install this.

$ mkdir ~/.nvm
$ git clone git://github.com/creationix/nvm.git ~/.nvm
$ echo ". ~/.nvm/nvm.sh" >> ~/.bashrc
$ source ~/.bashrc
$ nvm install v0.6.6
$ nvm alias default v0.6.6

Normally, nvm will install Node.js under `~/.nvm` folder. To uninstall Node.js, you can just use `$nvm uninstall v0.6.6`.

And like I said before, nvm is a version manage tool for Node.js, that means you can install different versions of Node.js and easily switching between them. All you need to do is to use `$nvm install {other version}`, then use the command below to choose which version you want to use now.

$ nvm use {Node.js version}

If you want to check available versions, please use:

$ nvm ls

For further nvm commands, please refer to here.

After you complete the installation, you can run this code to see the result.

$ node
>console.log('Hello World');
Hello World

Install NPM

You don't need to manually install NPM after node.js v0.6.3

NPM is a package manage tool for Node.js. Like CAPN for Perl, you can use npm to find a lot of useful package on there. And if you want to publish your package to others, you can also use npm to build your module.

You don't need to install npm by yourself since v0.6.3, but if you are using the earlier version of node.js, please use curl command to install npm manually. If you feel uncomfortable about installing directly from http://npmjs.org/; you can manually check out file by using `git clone http://github.com/isaacs/npm.git` and `sudo make install` to install npm by yourself.

$ curl https://npmjs.org/install.sh | sh

Related Article: