How to Install Node.js on Ubuntu VPS

Node.js is a JavaScript runtime built using Google’s V8 JavaScript engine. It allows you to create tools for networking, web servers and much more using JavaScript. Node.js uses event loop which means that commands are executed asynchronously. This guide will show you how to install and manage Node.js on Ubuntu (14.04) VPS.

What you’ll need

Before you begin this guide you’ll need the following:
  • SSH access to VPS running Ubuntu 14.04

Step 1 — Installing Node.js on Ubuntu

Ubuntu 14.04 already contains Node.js in default repositories. However, the version is outdated so it is advised to use personal package archive (PPA) from NodeSource.
Firstly download and add PPA to your local configuration with the following command:
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -

This will install latest stable Node.Js v6. If you need version 7 or any other just change setup_6.x to the version you want. For Node.js v7 command would look like this:
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -

Now you can install Node.js the same way you install other packages on Ubuntu:

sudo apt-get install -y nodejs

-y flag is used so you wouldn’t need to confirm the installation. After Node.js is installed you can check the exact version that was installed by entering:
nodejs -v

The output example:
v6.9.1

That’s it, you have successfully installed Node.js on your VPS.

Node.js comes with a package manager (NPM) which allows you to install and manage your Node.js packages. To install a package you can run:
sudo npm install name_of_the_package

Step 2 — Installing NVM (optional)

Node.js has a version manager (NVM) so you could install multiple instances of Node.js and manage them easily. To install NVM you would need to install some build tools first:
sudo apt-get install build-essential libssl-dev -y

Once these tools are installed you can install NVM:
curl https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash


v0.32.1 is NVM version, you can check latest version here
You will receive a message similar to this:
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="/root/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm

Now you can either reopen your terminal or run mentioned commands to start using NVM. To confirm that NVM has been installed correctly type:
nvm --version


If you get the version number it means that nvm has been installed correctly. Now you can use NVM to check all available Node.js versions:
nvm ls-remote

Output example:
         ......
         v6.6.0
         v6.7.0
         v6.8.0
         v6.8.1
         v6.9.0   (LTS: Boron)
         v6.9.1   (Latest LTS: Boron)
         v7.0.0
         v7.1.0

To install v7.1.0 version of Node.js simply run:

nvm install v7.1.0
To use recently installed Node.js version run:
nvm use v7.1.0

Using the same method you can now install and manage multiple other Node.js versions as well. You can check what versions you have installed locally with the following command:
nvm ls

Output example:
->       v6.9.1
         v7.1.0
         system
default -> v7.1.0
node -> stable (-> v7.1.0) (default)
stable -> 7.1 (-> v7.1.0) (default)
lts/boron -> v6.9.1

As you can see the default version currently is v7.1.0 so instead of typing nvm use v7.1.0 you can simply use:
nvm use default

You can also change the default version with:

No comments

Powered by Blogger.