Achinth Gurkhi's Web Development Blog

Achinth Gurkhi is the CEO of Code Vidya. He is also a software architect. He has designed and developed products like Message Sprout and Secure Pay By Phone.

Achinth has worked extensively on web applications using Java and .NET. He is currently learning and working on new technologies like Node, Angular, Backbone, Knockout, Meteor, MongoDB, HTML5, CSS3, etc.
Recent Tweets @

Once you upgrade to a newer version of Node.js using NVM all your global NPM packages are no longer available. To copy your global NPM packages to your new version do the following:

nvm use <new-version-number>
nvm copy-packages <old-version-number>

To install Git on Ubuntu do the following:

sudo apt-get install git

To install multiple versions of Node on a single system and to easily switch between versions use the Node Version Manager or NVM.

NVM installs binaries for versions 0.8.x and above but downloads source code and compiles the binaries for older versions. So, we need to install packages that are required to build Node from the source code.

To install NVM first install the dependencies first

sudo apt-get install build-essential

sudo apt-get install python libssl-dev curl

To install NVM run

curl https://raw.github.com/creationix/nvm/master/install.sh | sh

This installs NVM in /home/achinth folder (achinth is my username)  and adds the below line to the .profile file

[[ -s /home/achinth/.nvm/nvm.sh ]] && . /home/achinth/.nvm/nvm.sh # This loads NVM

Add the above line to .bashrc file as well. Now close and re-open the terminal.

To get the list of Node versions available for install run the command

nvm ls-remote

To install a version of Node run 

nvm install v0.10.15

To install another version of Node run

nvm install v0.8.25

To find list of Node versions installed on your system run

nvm ls

To change the version of Node to use run the command

nvm use v0.10.15

To uninstall Node.js from Ubuntu do the following:

cd /usr/local
sudo rm -r bin/node bin/npm include/node lib/node_modules share/man/man/node.1
cd ~
rm -r .npm

Run the following commands to install MongoDB on Ubuntu:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10

echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/10gen.list

sudo apt-get update

sudo apt-get install mongodb-10gen

To install Oracle Java 7 on Ubuntu execute the below commands:

sudo add-apt-repository ppa:webupd8team/java

sudo apt-get update

sudo apt-get install oracle-java7-installer

To check if Java has been correctly installed run the below command:

java -version

AppFog provides free hosting of your Node.js web apps. You can sign-up for a free account at https://console.appfog.com/signup.

 To deploy apps into AppFog from Ubuntu, you first need to install the AppFog CLI tool using ruby gems:

sudo apt-get install ruby1.9.1

sudo gem install af

Let’s create a sample application “hello-node” and deploy it to AppFog. First create a directory called hello-node. Next create a file called app.js with the following code:

var app = require('express').createServer();
 
app.get('/', function(req, res) {
    res.send('Hello from AppFog');
});
 
app.listen(process.env.VCAP_APP_PORT || 3000);

process.env.VCAP_APP_PORT is the listen port for your app in AppFog, locally it will run in port 3000.

Next create a package.json file as follows:

{
    "name": "hello-node",
    "version": "0.0.1",
    "dependencies": {
        "express": ""
    }
}

Next run the following commands:

npm install

af login

af push

npm install will install all the dependencies mentioned in the package.json file, which is only express in our case.

af login will ask you for your AppFog credentials and validate it:

image

af push will deploy the application to AppFog:

image

Now, the demo application should be accessible at achinth-demo.ap01.aws.af.cm:

image

If you make any changes to the code and want to re-deploy the app to AppFog use the command:

af update

To install Node.js on Ubuntu, you first need to install the dependencies:

sudo apt-get install build-essential

sudo apt-get install python libssl-dev

Then download the latest stable version source code of Node.js, unzip, build and install it:

wget http://nodejs.org/dist/v0.10.10/node-v0.10.10.tar.gz

tar -xzvf node-v0.10.10.tar.gz

cd node-v0.10.10

./configure

make

sudo make install

To verify that Node.js has been installed correctly run the command:

node --version

This should display the Node.js version number which in my case is v0.10.10.