Instantly share code, notes, and snippets.

Embed
What would you like to do?
Install Node.js on Amazon Linux (EC2)

Installing Node.js on Amazon Linux AMI

The following will guide you through the process of installing Node.js on an AWS EC2 instance running Amazon Linux AMI 2016.09 - Release Notes

For this process I'll be using a t2.micro EC2 instance running Amazon Linux AMI (ami-d41d58a7). Once the EC2 instance is up-and-running, connect to your server via ssh

  • Make sure our server has the latest packages : sudo yum update -y
  • Install required packages : sudo yum install -y gcc gcc-c++ make openssl-devel

Installing Node.js

For the next steps, use /tmp as the working directory

  • Download the Node.js source code, select the recommended LTS version via the Node.js download page and copy the URL of the "Source Code" -package : curl -O https://nodejs.org/dist/v4.6.0/node-v4.6.0.tar.gz

At this time of writing the current version is v4.6.0 (which includes npm 2.15.9)

  • Unpack and cleanup : tar -xvf node-v4.6.0.tar.gz && rm node-v4.6.0.tar.gz
  • Configure, make and install,... this may take a while, especially the compiling part.
$ cd node-v4.6.0
$ ./configure
$ make
$ sudo make install

You can verify afterwards if the installation was successful by checking the versions of node and npm :

  • node -v, returns value v4.6.0
  • npm -v, returns value 2.15.9

If by any chance, your are in the root environment and the previous command returns "-bash: node: command not found", you can fix this by creating the following symbolic links :

sudo ln -s /usr/local/bin/node /usr/bin/node
sudo ln -s /usr/local/lib/node /usr/lib/node
sudo ln -s /usr/local/bin/npm /usr/bin/npm	

Testing Node.js

The best method to test Node.js is actually run an application. This this prurpose we'll configure and runs a simple webserver. Again, let's use /tmp as our working directory..

  • Create a subdirectory : mkdir www
  • Enter the directory : cd www
  • Create a file called server.js and edit the contents of the file : nano server.js
  • Paste the following code and save :
var http = require('http');

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

server.listen(8080);
console.log("Node.js is listening on port 8080");  
  • Start the application : node server.js
  • Open a browser and go to the public IP address of the EC2 instance to test : http://<ip-address-ec2-instance>:8080
  • As a result you should see the "Node webserver running" -message

Make sure the security group applied to your EC2 instance allows inbound traffic to port 8080 !

In an adjacent gist, I'm adding an instance of the Ghost blogging platform - link (coming soon)

@pranavgawri

This comment has been minimized.

pranavgawri commented Jul 23, 2017

Worked like a charm. thanks!

@marcus13345

This comment has been minimized.

marcus13345 commented Aug 17, 2017

any word on how to use something newer than 4.6?

@FrancisBou

This comment has been minimized.

@kuzmicheff

This comment has been minimized.

kuzmicheff commented Sep 2, 2017

Thanks for mentioning the symlinks! Saved my day!

@sergeylukin

This comment has been minimized.

sergeylukin commented Sep 5, 2017

It's quicker just to curl -L https://git.io/n-install | bash -s -- -y 4.6.0 or any other version. Read more on https://github.com/mklement0/n-install

If you install from CloudFormation Init, say for ec2-user, then curl -L https://git.io/n-install | N_PREFIX=/home/ec2-user/n sudo -u ec2-user bash -s -- -y 4.6.0 would do the trick

@Gribbs

This comment has been minimized.

Gribbs commented Sep 15, 2017

+1 to you sergeylukin

@mdflores

This comment has been minimized.

mdflores commented Dec 4, 2017

Hi,

Try this it's much better and simpler. https://nodejs.org/en/download/package-manager/#enterprise-linux-and-fedora

Works like charm!

@FSojitra

This comment has been minimized.

FSojitra commented May 11, 2018

@mdflores
thanks, it worked for me

@ewal2

This comment has been minimized.

ewal2 commented Jul 30, 2018

It works great, thank you.

@premsanth-rajamani

This comment has been minimized.

premsanth-rajamani commented Sep 6, 2018

The step by step instructions where really helpful, but during the setup,
i had some issue with configure and installations,
I had to install some developer tools before running the install commands, i used the below command and it worked great..

yum groupinstall 'Development Tools'

Btw thanks a lot this was helpful

@yadav1vikas1995

This comment has been minimized.

yadav1vikas1995 commented Sep 9, 2018

Thanks,It works properly.
$ make commands take some time about 10 t0 15 minute be patient.

@jatin33

This comment has been minimized.

jatin33 commented Nov 12, 2018

thanks it works great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment