In this tutorial you'll download the latest Node.js source code, compile it and install on an instance of the Amazon Linux AMI (64-bit).

For this tutorial I'm assuming you've already launched an AWS EC2 instance using the latest Amazon Linux AMI and can SSH into the instance.

SSH into the instance.
$ ssh

Update the server.
$ sudo yum -y update

Install some necessary packages.
$ sudo yum install -y gcc gcc-c++ make openssl-devel

Let's use /tmp as the working directory for our downloads and installation.
$ cd /tmp

Visit the Node.js website and download the latest "Mature and Dependable" version.

Node.js download

Download the source code. At the time of this writing it's v4.24. Update the version number below if necessary.

$ curl -O

Decompress and cleanup.

$ tar -xvf node-v4.2.4.tar.gz && rm node-v4.2.4.tar.gz

Configure, make and install. The make command will run for a while depending on how super-charged your EC2 instance is. My test on a t2.medium instance took 15 minutes to compile. Warning: If you're working through this tutorial on a micro instance then go have a 3-beer lunch while the compile step is running:)

$ cd node-v4.2.4/

$ ./configure

$ make

$ sudo make install

After the code has finished compiling ... Verify the installation. You should get some version numbers back. In my case 2.14.12 and v4.2.4 respectively.

$ npm -v

$ node -v

Ok, let's run something simple and prove it works.

Move to the home directory and create a www directory.

$ cd /home/ec2-user

$ mkdir www

$ cd www

Create an app.js file.

$ sudo nano app.js

The command above opens the nano text editor with a blank document. Copy/Paste the text below into it.

var http = require('http');

var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/html"});
  response.end("<h1>Hello from Node.js</h1>\n");


console.log("Node.js is listening on port 8000");

Type ctrl+o to save (WriteOut) the app.js file to disk. Then crtl+x to exit the nano editor.

Startup the server.

$ node /home/ec2-user/www/app.js

The console will echo back "Node.js is listening on port 8000".

Make sure you have port 8000 open in the security group that your AWS instance is running in.

Then navigate to the public IP of your AWS instance and you should see...

Hello World

To shutdown the server type ctrl+c.