Introduction
Node.js is a platform that uses event-driven, non-blocking I/O model for providing an easy way to build scalable network programs. It is built on JavaScript runtime and it is perfect for real time applications that run across distributed devices.
In this tutorial, we will guide you how to install Node.js on Ubuntu 14.04 server.
Installation of Distro-Stable Version
Node.js is already present in the Ubuntu 14.04 repositories. Even though it may not be the latest version, but it should be quite stable. Usually version 0.10.25 is found in the repositories.
We will use the apt package manager to install node.js. Before we get down to install, we should first run an update of local packages:
sudo apt-get update sudo apt-get install nodejs
Usually, you will also need npm (node.js package manager) which can be installed using the command:
sudo apt-get install npm
This will allow easy installation of packages and modules for node.js.
Installation of Node.js Using PPA
PPA stands for personal package archive and it can be used to get the latest version of Node.js. First of all, you have to install PPA to get access to its contents. Run the following command to install PPA:
sudo add-apt-repository ppa:chris-lea/node.js
PPA will be installed and you can now proceed to install the node.js package using the apt-get command:
sudo apt-get update sudo apt-get install nodejs
For installing npm, run the following command:
sudo apt-get install npm
Installation of Node.js Using NVM
NVM is Node.js version manager and it’s a special tool to install node.js in an alternative way. NVM allows you to install multiple self-contained versions of node.js.
First of all, get the software packages from Ubuntu repositories to build source packages.
sudo apt-get update sudo apt-get install build-essential libssl-dev
Once you have all the required packages, you can install using the nvm installation script as shown below:
curl https://raw.githubusercontent.com/creationix/nvm/v0.7.0/install.sh | sh
This command will download and run the script, install software in a subdirectory of your home directory ~/.nvm.
In order to access the nvm functionality, log out and log in back or source the ~/.profile file:
source ~/.profile
Once you have completed installing nvm, you can start to install node.js or different versions of it. You can get the available versions node.js using the command:
nvm ls-remote
. . . v0.11.6 v0.11.7 v0.11.8 v0.11.9 v0.11.10 v0.11.11 v0.11.12 v0.11.13
For installing a particular version, type:
nvm install 0.11.13
By default nvm will switch to the latest installed version. But you can explicitly give the version by giving:
nvm use 0.11.13
Here is a list of commands that comes handy:
node –v
By using this command, you can get the version that is used currently.
nvm ls
You can see the installed version in case if there are multiple node.js versions.
nvm alias default 0.11.13
It can be used to default one of the versions.
nvm use default
By using an alias this version will be selected automatically.
npm install express
npm can be used to install packages to the node.js project’s directory.
npm install -g express
the –g flag will install it globally, that is making it available to other projects.
nvm help
this command will allow you to explore more options available with nvm.
That’s it!