How to get node.js running in Amazon EC2 using npm

I spent a while trying to figure out how to elegantly install node.js and npm in EC2 with Amazon Linux using an rpm package manager like yum.  Node.js takes 30 minutes to compile on a micro instance so if you plan to use cloud-formation or spin up new instances to handle load, you need to install with a package manager.

The first thing you have to do is create a machine.  I created a 64 bit micro instance of Amazon.  You can also do this through the console but I prefer the CLI.

ec2-run-instances ami-1624987f -k YOUR_KEYPAIR_HERE --instance-type t1.micro -z us-east-1a

Then I find out the CNAME of the machine.

ec2-describe-instances | grep running

Then login to the machine.

ssh -i YOUR_KEYPAIR_FILE_HERE ec2-user@YOUR_MACHINE_ADDRESS_HERE.compute-1.amazonaws.com

There is a project on git called node-npm that has a good wiki on how to build the node.js package.  Now that you are into the machine, install the stuff we need for nodejs-rpm and the build process.

yum install rpm-build rpmdevtools openssl-devel zlib-devel redhat-rpm-config

Then get the nodejs-rpm project and move to that directory.

git clone https://github.com/kazuhisya/nodejs-rpm
cd nodejs-rpm

And then build the thing.

rpmdev-setuptree
spectool -g -R nodejs.spec
rpmbuild -ba nodejs.spec

It will produce your RPM files.

/home/ec2-user/rpmbuild/RPMS/x86_64/nodejs-debuginfo-0.8.16-2.amzn1.x86_64.rpm
/home/ec2-user/rpmbuild/RPMS/x86_64/nodejs-0.8.16-2.amzn1.x86_64.rpm
/home/ec2-user/rpmbuild/RPMS/x86_64/nodejs-binary-0.8.16-2.amzn1.x86_64.rpm

I then copied nodejs-0.8.16-2.amzn1.x86_64.rpm over to an S3 bucket so that I could install them directly the next time I built a machine.

Now I can just create a new machine and install node via yum.

sudo yum install --nogpgcheck https://s3.amazonaws.com/YOUR_BUCKET/nodejs-0.8.16-2.amzn1.x86_64.rpm

Leave a Reply

Your email address will not be published. Required fields are marked *