Linux Tutorials, Guides & Latest Tech Stuffs

How to install nodejs on Ubuntu 16.04 Server

Node Js is a javascript platform that is developed by Ryan Dahl for server-side programming. Currently, Node Js is one of the most sought programming languages.  In this guide, we will guide you to successfully install Nodejs on Ubuntu 16.04 Server.

Requirements

It is recommended to install Nodejs with a non-root user with sudo access

Method -1

Installing Nodejs distro stable version via apt-get | Quick Installation

It is one of the easiest ways to install node js, just run these three commands and nodejs will be installed on your Ubuntu Server

sudo apt-get update

sudo apt-get install nodejs

sudo apt-get install nodejs-legacy

After running these three commands Nodejs should be installed successfully. These commands will install distro stable version which may be lower than your nodejs application requirement. If the version suits your need then your node js installation is completed

you can verify the installation by checking install nodejs version.

node -v

Installing NPM

NPM stands for Node.js Package Manager, this will allow you to easily download different Node modules and packages that will be required for most of node based applications. You can install npm by typing

sudo apt-get install npm

Method -2

Installing NodeJs using NVM

NVM stands for Node Version Manager, it is basically a bash script that installs and manage node.js versions. Before installing it you need to get some software packages from Ubuntu that will allow you to build NVM source packages.

sudo apt-get install update

sudo apt-get install build-essentials libssl-dev

After these packages are downloaded, you can fetch bash script from NVM GitHub repository. You can use curl or wget for this purpose. I am using curl here in my example

curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.33.6/install.sh -o install_nvm.sh

you can inspect bash script by using

vim install_nvm.sh

As everything is fine now run the bash file

bash install_nvm.sh

exit terminal and then run nvm, if it shows Node Version Manager, then congratulations your installation is completed

Now you can install required node.js version by using

nvm install 8.4.0

sample output

Downloading and installing node v8.4.0...
Downloading http://nodejs.org/dist/v8.4.0/node-v8.4.0-linux-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v8.4.0 (npm v5.3.0)
Creating default alias: default -> 8.4.0 (-> v8.4.0)

you can replace 8.4.0 from any other version as per your project requirement

Now check your node and npm version using

node -v
npm -v

By default, NVM uses latest node.js version you can switch to your required version by using nvm use command as shown below

nvm use 6.0.0

Method -3

Installing Nodejs Using a PPA

It is a flexible way to install required Node.js version, you will need to install PPA in order to get access to its contents.

Make sure you are in your home location ( ~ ) and use curl command to install required version, Just replace the red word in the curl PPA script

cd ~ && curl -sL https://deb.nodesource.com/setup_8.x -o nodesource_install.sh

As a next step, you need to run recent nodesource_install.sh bash file

sudo bash nodesource_install.sh

After running this command you can run

sudo apt-get install nodejs

Node js package already contained NPM ( Node package manager ) so you will not be required to install npm separately.

You will also need to install build-essential package in order to run packages that depend on compiling code from source

sudo apt-get install build-essential

 

Best Node.js Installation Method

Method -2 Installing Node.js using node version manager ( nvm ) is best if your project required latest node version and if you are working on several node projects and you need to switch between different versions. Method -1 is also quite quick because it requires only three commands to run

Pranav K

Pranav K is a software engineer and all-round computer geek. His interests include AWS, Ubuntu and Wordpress

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.