Running node.js on Windows with VirtualBox and Ubuntu
Introduction
Node.js is an event-driven I/O framework for building extremely fast and scalable network applications. It's designed to run on Unix-like platforms, and although it's possible to run node.js on Windows, it's not yet fully supported. There's a Windows port of node.js in the pipeline, but in the meantime if you're a Windows user who wants to check out the node.js hype, this article shows you how to run node.js inside your own Linux virtual machine.
For a deeper introduction to node.js, watch this Introduction to NodeJS video or read the The Node Beginner Book.
We'll start off by installing and configuring VirtualBox, set up a shared folder between Windows and the VM, install Git, node.js and NPM, then finally write a simple web server.
Install VirtualBox
The first step is to download and install VirtualBox, an open source virtualisation application that lets you run Linux inside Windows.
Create and a new Linux virtual machine
Download the ISO for your favourite Linux server distro, I went with Maverick Meerkat - Ubuntu Server Edition v10.10.
Fire up VirtualBox and create a new virtual machine (VM). Go through the setup wizard:
- Name: linuxdev
- Operating System: Linux
- Version: Ubuntu (64-bit)
- Base memory: 512 MB
- Create new dynamic 16GB hard disk
Right-click on your new virtual machine, go to Settings, then configure Storage and Network as follows:
Storage
- IDE Controller / Empty
- [CD disc button]
- Choose a virtual CD/DVD disk file...
- [Navigate to ubuntu-10.10-server-i386.iso]
Network
- Enable Network Adapter
- Attach to 'Bridged Adapter'
The Bridged Adapter feature bridges your Windows network adapter to Ubuntu and allows it the VM to independently lease its own IP address from your router, effectively making it another host on your network.
The configured VM should now look something like this:

Install Ubuntu
Start the VM, it will boot from the Ubuntu installation ISO image, which is mounted in the VM's virtual CD/DVD drive. Complete the installation, then login.

Update and upgrade your new Linux OS
Just like running Windows Update on a new Windows installation, you should now make sure the new Ubuntu installation is fully patched and running the latest software. Ubuntu uses a command-line tool for managing software packages called APT (Advanced Packaging Tool). So, before installing anything else, update the local package index by running:
$ sudo apt-get update
Then run apt-get again to upgrade all installed packages to the latest version.
$ sudo apt-get upgrade
Install VirtualBox 'Guest Additions'
This step is optional, but VirtualBox Guest Additions give you some nice features for integrating between the Ubuntu guest OS and the Windows host OS, like Shared Folders which can make a Windows folder available inside Ubuntu. To install Guest Additions:
$ sudo apt-get install build-essential linux-headers-$(uname -r)
$ sudo apt-get install virtualbox-ose-guest-utils
Share a folder between Windows and Linux
Now to add a Shared Folder. Create a new folder in Windows, then from the Ubuntu VM menu select "Devices -> Shared Folders...". Give your folder a name, I've used "windows", and select "Make Permanent":

To mount this folder inside Ubuntu, first create a mount point in your home directory, then attach the shared folder:
$ mkdir ~/win
$ sudo mount -t vboxsf windows ~/win
To mount the shared folder automatically every time Ubuntu boots, add the mount command to your
rc.local script, which executes on start up:
$ sudo pico /etc/rc.local
Add this line before the exit 0
mount -t vboxsf windows /home/chris/win
To test this, reboot:
$ sudo shutdown -r now
When the OS comes back up you should be able to login and see the contents of the Windows folder:
$ ls ~/win
Install Git
Git is a distributed version control system (DVCS) that we'll use to download node.js. Install the git package, and the OpenSSL development package required later to build node.js:
$ sudo apt-get install git-core libssl-dev
Download node.js by cloning the repository from GitHub:
$ git clone git://github.com/joyent/node.git
Build and install:
$ cd node
$ ./configure
$ make
$ sudo make install
Check node.js in installed by displaying the installed version:
$ node -v
At this point, it's also worth installing NPM, the node package manager:
$ sudo apt-get install curl
$ curl http://npmjs.org/install.sh | sudo npm_install=rc sh
Write your first node.js program
Now to write your first quick and simple node.js app, the Hello World Web Server. Create a new file
web-server.js in your home directory containing this code.
var http = require("http");
http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello from node.js running on Ubuntu!\n');
res.end();
}).listen(8888);
console.log('Server listening on port 8888');
If you're using a VirtualBox Shared Folder then you can write node.js JavaScript with your favourite Windows editor, I'm using the E text editor which is similar to TextMate for OSX. If you're editing directly inside Linux then try pico, vi or emacs.
Now to run the web server, which will listen for incoming HTTP connections on port 8888:
$ node web-server.js
Server listening on port 8888
Open a browser in Windows and point it at your new web server:

To find the IP address of your Linux VM:
$ ip addr show eth0
Congratulations, you just built a web server.