How to setup a node.js development environment on Ubuntu 11.04

Install the latest node.js via apt-get


There are several ways to setup a node.js development environment on Ubuntu, you can either choose to download and compile it form source or using apt-get to do the works for you. Because node.js is still a pretty young project, it updates frequently. I suggest installing node.js via apt-get. You will find it is much easier not only to install but also update it. If you are on a Mac you can check out this post for the instruction.

Edit 2011/12/18

I’ve found it’s easier to install node through nvm, a node version management system. It’s something like rvm for ruby. You can switch between node versions for development. Also npm now come with node since version 0.6.3 which means you don’t have to install it by yourself. So basically you can ignore the Install npm step.

Install node.js

The problem is the version of node.js on apt-get is outdated. Therefore we need to add repository to apt ourself.
$ sudo apt-get install python-software-properties
$ sudo add-apt-repository ppa:chris-lea/node.js
$ sudo apt-get update
$ sudo apt-get install nodejs
$ sudo apt-get install nodejs-dev
The above methods are deprecated. Please use the following commands.
# install git
$ sudo apt-get install git
# clone repo
$ git clone git:// ~/.nvm
# enable on terminal open
$ echo ". ~/.nvm/" >> ~/.bashrc
# reopen your terminal and do the following
$ nvm install v0.6.6
# set default node
$ nvm alias default v0.6.6

Install npm

npm is the most popular package manager for node. It is like gem for ruby and pear for php. There are thousands of packages available out there including ORM, router, third party api wrapper … etc. So before you write your own solution make sure to check if it is already there on npm.
$ curl | sudo sh

Install mongodb

mongoDB is my number one choice NoSQL database for node.js. It is not the fastest but the easiest to use especially for people used to work with relational databases. However please do not design your data structure as what you used to have for a relational database. Otherwise your node.js app will run slow. Please do checkout their awesome docs before you start.
# switch to super user
$ sudo su
# add 10gen GPG key
$ apt-key adv --keyserver --recv 7F0CEB10
# add to source.list for using upstart to manage mongodb
$ echo "deb dist 10gen" >> /etc/apt/sources.list
# install
$ apt-get update
$ apt-get install mongodb-10gen
$ exit

Update node.js

Just install different versions of node like what we did above and switch between them.

Update mongoDB

Because we installed them via apt, we can update it just like other packages.
$ sudo apt-get update
$ sudo apt-get upgrade

Setup a node.js development environment on other OS

If you want to setup a node.js development environment on Windows or Mac OSX Lion, you can take a look at:
How to setup a node.js development environment on Windows
How to setup a node.js development environment on Mac OSX Lion

npm commands and node.js basics

Now you have setup a clean node.js development environment. Let’s have a quick look at how to use npm and learn some basics of javascript and node.js.

Related posts