How to install Node.js on Ubuntu

Just ten commands or fewer will get you started, showing you how to write a “Hello World” program using Node.js on our optimised, pre-installed image of Ubuntu 13.04.

If you want to use a different Linux distribution, most of this tutorial will still be correct. You will have to translate all of the apt-get statements and package names to your distribution of choice. Configuration file paths and layouts may also differ.

If you would like more information about how to set up a Linux VM using our system, please visit our other tutorials.

Step 1: Install Node Package Manager

First, fire up a fresh terminal and ensure your package lists are up-to-date:

sudo apt-get update

First you should install git, so that we can then install NVM, or Node Version Manager.

sudo apt-get install git

Since NodeJS is so new, version control can otherwise be a nightmare. NVM will help keep track of those constant updates and varying versions. This method uses an easy install script:

curl | sh

If you’ve been successful, you should be prompted to restart your terminal. Now you’ve added to the path in ~/.profile, you need to update it:

source ~/.profile

Now let’s type the command suggested when you ran that install script:

nvm ls-remote

This throws you a big, long list of all the available NodeJS versions. At the time of writing this tutorial, we’re up to v.0.11.10, so let’s install that one:

nvm install 0.11.10
Useful commands Check current version of Node:
node --version

Install and start using version 0.10.13:

nvm install v0.10.13

Swap back to using 0.11.10:

nvm use 0.11.10

Install current version of Node globally, making it the same and accessible for all users on this VM (you will be prompted for your password):

n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local

Step 2: Install Express web server

Express is a minimal yet very flexible HTTP server framework. Install it using:

npm install -g express

Express has a very simple way of creating all the necessary tools and files for a simple web app. To make an app called “helloworld” you simply write:

express helloworld
Default project structure Express creates a number of files by default when you create a new project using the command:

The directory tree should look like this:

  ├── public
  │   ├── images  &lt--for images
  │   ├── javascripts  &lt--for client-side javascript
  │   └── stylesheets  &lt--for stylesheets 
  │       └── style.css
  ├── routes  
  │   └── index.js  &lt--for routes (render index.jade)
  ├── views
  │   ├── index.jade  &lt-- index template
  │   └── layout.jade  &lt-- optional layout template
  ├── app.js  &lt-- contains server-side javascript
  └── package.json  &lt-- for dependencies etc.

Now move to that fresh directory:

cd helloworld

Now we need to get all the necessary dependencies. The command for this is:

npm install

You can now test the app by running it with node. By default it is called app.js:

node app.js

The Express server should now reply that it is listening on port 3000. This is default behaviour, it can be changed within app.js, but for now, let’s just check it’s working. Point your browser to the IP address of your VM (visible from your Elastichosts control panel) on port 3000. Your url should look like this:


Step 3: Configure your Express web server

Now we can edit the file using your favourite text editor. The simplest is probably nano:

nano app.js

This is where the majority of your server-side javascript is held. For now we’re just going to tweak the default script that controls our express server. Change the configuration so that express listens on the standard HTTP port 80. Do this by changing the port number under the “// all environments” comment to:

app.set('port', process.env.PORT || 80);

Now, if we want to operate like a more familiar web server, we need to edit the index.jade file. You can find this in the “view” directory of your project, so the command should look like this:

nano views/index.jade

The file itself isn’t written in standard html, but rather a templating language called Jade. Aside from the templating conventions, you can now edit this file as you would index.html:

extends layout

block content
  h1 Hello
  p Hello world, I'm running NodeJS on an Elastichosts VM!

Old habits die hard if you want to make it reassuringly simple and recognisable, simply add this line to your index.jade file:
include whatever.html

and express can render that good old-fashioned html file with no trouble.

Step 4: Bask in the glory of your own awesomeness

Now just navigate to http://YOUR_IP_ADDRESS once again to see your Node.js application in action!

Further reading: