Install Node.js on an Ubuntu server


Node.js is a Javascript platform for server-side programming that allows users to build network applications quickly. By leveraging Javascript on both the front-end and the back-end, development can be more consistent and be designed within the same system.

Install NodeJs

We have to use the apt package manager. We should refresh our local package index prior and then install from the repositories:

sudo apt-get update
sudo apt-get install nodejs

This is all we need to get set up with Node.js, but in most cases, we also want to also install npm, which is the Node.js package manager. We can install this using

sudo apt-get install npm

This will allow you to easily install modules and packages to use with Node.js.

Install Express

Now that we have Node running, we need the rest of the stuff we’re going to actually use to create a working website. To do that we’re going to install Express, which is a framework that takes Node from a barebones application and turns it into something that behaves more like the web servers we’re all used to working with. To install express type:

npm install -g express

This installs some core Express functionality right into our Node installation, making it available globally so we can use it anywhere we want. Express is now installed and available.

Create an Express Project

Anyway, still in /opt/node/ or wherever you’re storing your node apps, type this:

express nodeproject1

You’ll see something like this:

   create : nodeproject1
   create : nodeproject1/package.json
   create : nodeproject1/app.js
   create : nodeproject1/public
   create : nodeproject1/routes
   create : nodeproject1/routes/index.js
   create : nodeproject1/routes/users.js
   create : nodeproject1/views
   create : nodeproject1/views/index.jade
   create : nodeproject1/views/layout.jade
   create : nodeproject1/views/error.jade
   create : nodeproject1/public/javascripts
   create : nodeproject1/public/images
   create : nodeproject1/bin
   create : nodeproject1/bin/www
   create : nodeproject1/public/stylesheets
   create : nodeproject1/public/stylesheets/style.css

   install dependencies:
     $ cd nodeproject1 && npm install

   run the app:
     $ DEBUG=nodeproject1 ./bin/www

Edit Dependencies

Note that the express installation routine created a file called package.json in your nodeproject1 directory. Open this up in a text editor and it’ll look like this:

  "name": "nodeproject1",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  "dependencies": {
    "express": "~4.9.0",
    "body-parser": "~1.8.1",
    "cookie-parser": "~1.3.3",
    "morgan": "~1.3.0",
    "serve-favicon": "~2.1.3",
    "debug": "~2.0.0",
    "jade": "~1.6.0"

This is a basic JSON file describing our app and its dependencies. We need to add a few things to it. Specifically, calls for MongoDB. Let’s make our dependencies object look like this:

"dependencies": {
    "express": "~4.0.0",
    "serve-favicon": "~2.1.3",
    "morgan": "~1.0.0",
    "cookie-parser": "~1.0.1",
    "body-parser": "~1.0.0",
    "debug": "~0.7.4",
    "jade": "~1.3.0",
    "mongodb": "*",

Install Dependencies

Now we’ve defined our dependencies and we’re ready to go. Note that the asterisks tell NPM “just get the latest version” when you run the install, which we’re about to do.

Return to your command prompt, cd to your nodeproject1 directory, and type this:

npm install

You now have a fully-functioning app ready and waiting to run. Before we do that, though, we need to do one quick thing to prepare for setting up our database later. Still in your nodeproject1 directory, type this:

mkdir data

That’s where we’re eventually going to store our MongoDB data. If that directory doesn’t exist, the database server will choke when we go to run it later. We’re not doing anything with it right now, though, so let’s test out our web server! Type the following:

npm start

Everything working? Great! Open a browser and type http://localhost:3000 where you will see a welcome to Express page.

You are now running your own Node JS webserver, with the Express engine and Jade HTML preprocessor installed.