0% found this document useful (0 votes)
183 views

How To Install Node - Js On Ubuntu 16.04 - DigitalOcean

How to Install Node.js on Ubuntu 16.04 _ DigitalOcean
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
183 views

How To Install Node - Js On Ubuntu 16.04 - DigitalOcean

How to Install Node.js on Ubuntu 16.04 _ DigitalOcean
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Products Customers Community Pricing Docs Support Log In Sign Up

Contents

Prerequisites

 Subscribe  Share
How To Install the Distro-
Stable Version for Ubuntu

How To Install Using a


PPA

How To Install Using


NVM

Removing Node.js

Conclusion

Mark as Complete

S C R O L L TO TO P

How To Install Node.js on Ubuntu 16.04


76
Updated March 7, 2018  1.5m NODE.JS UBUNTU 16.04

Brennen Bearnes

Not using Ubuntu 16.04? Choose a different version:

Introduction
Node.js is a JavaScript platform for general-purpose programming that allows users to build network
applications quickly. By leveraging JavaScript on both the front- and back-end, development can be more
consistent and designed within the same system.

In this guide, we'll show you how to get started with Node.js on an Ubuntu 16.04 server.

If you are looking to set up a production Node.js environment, check out this link: How To Set Up a
Node.js Application for Production.

Prerequisites
This guide assumes that you are using Ubuntu 16.04. Before you begin, you should have a non-root user
account with sudo privileges set up on your system. You can learn how to do this by completing steps 1-4
in the initial server setup for Ubuntu 16.04.

How To Install the Distro-Stable Version for Ubuntu


Ubuntu 16.04 contains a version of Node.js in its default repositories that can be used to easily provide a
consistent experience across multiple systems. At the time of writing, the version in the repositories is
v4.2.6. This will not be the latest version, but it should be quite stable and sufficient for quick
experimentation with the language.

In order to get this version, we just have to use the apt package manager. We should refresh our local
package index first, and then install from the repositories:

$ sudo apt-get update


$ sudo apt-get install nodejs

If the package in the repositories suits your needs, this is all you need to do to get set up with Node.js. In
most cases, you'll also want to also install npm , which is the Node.js package manager. You can do this by
typing:

$ sudo apt-get install npm

This will allow you to easily install modules and packages to use with Node.js.

Because of a conflict with another package, the executable from the Ubuntu repositories is called nodejs
instead of node . Keep this in mind as you are running software.

To check which version of Node.js you have installed after these initial steps, type:

$ nodejs -v

Once you have established which version of Node.js you have installed from the Ubuntu repositories, you
can decide whether or not you would like to work with different versions, package archives, or version
managers. Next, we'll discuss these elements along with more flexible and robust methods of installation.

How To Install Using a PPA


An alternative that can get you a more recent version of Node.js is to add a PPA (personal package
archive) maintained by NodeSource. This will have more up-to-date versions of Node.js than the official
Ubuntu repositories, and allows you to choose between Node.js v4.x (the older long-term support version,
which will be supported until April of 2018), Node.js v6.x (supported until April of 2019), and Node.js v8.x
(the current LTS version, supported until December of 2019).

First, you need to install the PPA in order to get access to its contents. Make sure you're in your home
directory, and use curl to retrieve the installation script for your preferred version, making sure to replace
8.x with your preferred version string (if different):

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

You can inspect the contents of this script with nano (or your preferred text editor):

$ nano nodesource_setup.sh

And run the script under sudo :

$ sudo bash nodesource_setup.sh

The PPA will be added to your configuration and your local package cache will be updated automatically.
After running the setup script from nodesource, you can install the Node.js package in the same way you
did above:

$ sudo apt-get install nodejs

To check which version of Node.js you have installed after these initial steps, type:

$ nodejs -v

Output
v8.10.0

The nodejs package contains the nodejs binary as well as npm , so you don't need to install npm
separately.

npm uses a configuration file in your home directory to keep track of updates. It will be created the first
time you run npm . Execute this command to verify that npm is installed and to create the configuration file:

$ npm -v

Output
5.6.0

In order for some npm packages to work (those that require compiling code from source, for example), you
will need to install the build-essential package:

$ sudo apt-get install build-essential

You now have the necessary tools to work with npm packages that require compiling code from source.

How To Install Using NVM


An alternative to installing Node.js through apt is to use a specially designed tool called nvm , which
stands for "Node.js version manager". Rather than working at the operating system level, nvm works at the
level of an independent directory within your home directory. This means that you can install multiple, self-
contained versions of Node.js without affecting the entire system.

Controlling your environment with nvm allows you to access the newest versions of Node.js and retain
and manage previous releases. It is a different utility from apt-get , however, and the versions of Node.js
that you manage through it are distinct from the distro-stable version of Node.js available from the Ubuntu
repositories.

To start off, we'll need to get the software packages from our Ubuntu repositories that will allow us to build
source packages. The nvm script will leverage these tools to build the necessary components:

$ sudo apt-get update


$ sudo apt-get install build-essential libssl-dev

Once the prerequisite packages are installed, you can pull down the nvm installation script from the
project's GitHub page. The version number may be different, but in general, you can download it with
curl :

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

And inspect the installation script with nano :

$ nano install_nvm.sh

Run the script with bash :

$ bash install_nvm.sh

It will install the software into a subdirectory of your home directory at ~/.nvm . It will also add the
necessary lines to your ~/.profile file to use the file.

To gain access to the nvm functionality, you'll need to log out and log back in again, or you can source the
~/.profile file so that your current session knows about the changes:

$ source ~/.profile

Now that you have nvm installed, you can install isolated Node.js versions.

To find out the versions of Node.js that are available for installation, you can type:

$ nvm ls-remote

Output
...
v8.5.0
v8.6.0
v8.7.0
v8.8.0
v8.8.1
v8.9.0
v8.9.1
v8.9.2
v8.9.3
-> v8.9.4 (Latest LTS: Carbon)

As you can see, the newest LTS version at the time of this writing is v8.9.4. You can install that by typing:

$ nvm install 8.9.4

Usually, nvm will switch to use the most recently installed version. You can explicitly tell nvm to use the
version we just downloaded by typing:

$ nvm use 8.9.4

When you install Node.js using nvm, the executable is called node . You can see the version currently
being used by the shell by typing:

$ node -v

Output
v8.9.4

If you have multiple Node.js versions, you can see what is installed by typing:

$ nvm ls

If you wish to default one of the versions, you can type:

$ nvm alias default 8.9.4

This version will be automatically selected when a new session spawns. You can also reference it by the
alias like this:

$ nvm use default

Each version of Node.js will keep track of its own packages and has npm available to manage these.

You can have npm install packages to the Node.js project's ./node_modules directory by using the normal
format. For example, for the express module:

$ npm install express

If you'd like to install it globally (making it available to the other projects using the same Node.js version),
you can add the -g flag:

$ npm install -g express

This will install the package in:

~/.nvm/node_version/lib/node_modules/package_name

Installing globally will let you run the commands from the command line, but you'll have to link the
package into your local sphere to require it from within a program:

$ npm link express

You can learn more about the options available to you with nvm by typing:

$ nvm help

Removing Node.js
You can uninstall Node.js using apt-get or nvm, depending on the version you want to target. To remove
the distro-stable version, you will need to work with the apt-get utility at the system level.

To remove the distro-stable version, type the following:

$ sudo apt-get remove nodejs

This command will remove the package and retain the configuration files. These may be of use to you if
you intend to install the package again at a later point. If you don’t want to save the configuration files for
later use, however, then run the following:

$ sudo apt-get purge nodejs

This will uninstall the package and remove the configuration files associated with it.
As a final step, you can remove any unused packages that were automatically installed with the removed
package:

$ sudo apt-get autoremove

To uninstall a version of Node.js that you have enabled using nvm, first determine whether or not the
version you would like to remove is the current active version:

$ nvm current

If the version you are targeting is not the current active version, you can run:

$ nvm uninstall node_version

This command will uninstall the selected version of Node.js.

If the version you would like to remove is the current active version, you must first deactive nvm to enable
your changes:

$ nvm deactivate

You can now uninstall the current version using the uninstall command above, which will remove all files
associated with the targeted version of Node.js except the cached files that can be used for reinstallment.

Conclusion
As you can see, there are a quite a few ways to get up and running with Node.js on your Ubuntu 16.04
server. Your circumstances will dictate which of the above methods is the best idea for your circumstance.
While the packaged version in Ubuntu's repository is the easiest, the nvm method is definitely much more
flexible.

Brennen Bearnes Upvote (76)  Subscribe  Share

Build something great with a $100, 60 day credit


Build the internet on DigitalOcean with a $100, 60 day credit to use across Droplets, Block Storage,
Load Balancers and more!

REDEEM CREDIT

Related Tutorials
How To Use npm to Build and Publish Node.js Packages on a Linux Server
Geddy.JS: A No-Brainer MVC Node.js Framework
How To Use node.js, request and cheerio to Set Up Simple Web-Scraping
How To Install Express, a Node.js Framework, and Set Up Socket.io on a VPS
How To Build a Node.js Application with Docker

25 Comments

Leave a comment...

Log In to Comment

timobrien May 17, 2016

0 Thanks for the guide :-)

One small issue, the installer script reads:

sudo bash node_setup.sh

where is should read:

sudo bash nodesource_setup.sh

Take care,
Tim

bpb May 17, 2016


0 Good catch. Fixed.

oddrationale May 19, 2016

0 You can also use nave to install node:

curl https://raw.githubusercontent.com/isaacs/nave/master/nave.sh | sudo bash -s -- usemain

You can rerun the command to update node at any time.

https://gist.github.com/oddrationale/83cd0c1ceceabb4552cf

deksden June 14, 2016

0 Didnt this works (n is one of node version manager?

[sudo] npm install -g n


[sudo] n latest

Maybe nvm can be installed via npm also. Npm can be installed via any stable node build from standard
package sources.

bcooke91 July 6, 2016

0 Following all the steps gets me a 403 on apt-get install nodejs, no matter which method.

403 Forbidden [192.241.164.26 80]


Failed to fetch http://mirrors.digitalocean.com/ubuntu/pool/universe/r/rlwrap/rlwrap_0.37-5

vishwas July 17, 2016

0 You guys are the best. Cheers :)

tanveer August 31, 2016

0 thanks man.

ccuilla September 19, 2016

0 Is there a way to install an OLDER version (0.10) of nodejs on Ubuntu 16?

iloveu2015 October 1, 2016

0 greate article! Now I master the installation of node

tanveer October 18, 2016

0 if i make one of the versions default then i will get error messages if i remove those versions.

so in my case, i set default to the stable version (instead of a specific version) by:

nvm alias default stable

nxtoanit November 29, 2016

0 1. I install npm on ubuntu 16.04 (sudo apt-get install npm) then get the below error: The following
packages have unmet dependencies: npm : Depends: node-gyp (>= 0.10.9) but it is not going to
be installed E: Unable to correct problems, you have held broken packages.

2. I add more depens to this: sudo apt-get install npm node-gyp nodejs-dev libssl-dev libssl-doc

3. Latest error: The following packages have unmet dependencies: libssl-dev : Depends: libssl1.0.0
(= 1.0.2g-1ubuntu4) but 1.0.2g-1ubuntu4.5 is to be installed E: Unable to correct problems, you
have held broken packages.

4. At here I dont know how to do. Please help me

katjuell MOD January 11, 2018


0 @nxtoanit: Try running sudo apt-get -f install to install unmet dependencies. Once your
dependencies have been resolved you can begin at the start of the tutorial.

amiashraf March 5, 2017

0 I have install nodejs by executing the command

sudo apt-get install nodejs

then run the command

sudo apt install nodejs-legacy

for show same version with

node -v

or

nodejs -v

and it showing the version v4.2.6


then I have installed nvm and install the latest stable package of node (v6.10.0).
now the command "node -v" showing v6.10.0 and "nodejs -v" showing v4.2.6
how can i show same version (v6.10.0) for both command?

vishalshetty1990 February 26, 2018


0 Ubuntu repository isn't maintaining the latest version of the NodeJS and MongoDB...

nmodi9 March 10, 2017

0 I followed distro stable part of this guide, but when I run "node -v", I get v0.10.25, not v4.2.6.

sivaneshraina June 21, 2017

0 Thanks for the Guide !!

ParthMehrotra June 26, 2017

0 For anyone else who ran into issues because they installed using apt:

https://github.com/nodejs/node-v0.x-archive/issues/3911

andersl January 8, 2018

1 Would be very helpful if after this command:

sudo apt-get install nodejs

That you recommend users to create a symbolic link to nodejs in its usual location:

sudo ln -s which nodejs /usr/bin/node

This way they can run node as usual by typing node.

xargrigoris January 9, 2018

0 keep is stupid simple with one command that installs anything

https://github.com/xargr/nodejs_fresh_install

howtomakeaturn January 16, 2018

0 Great guide! thanks a lot!

SaneeshKV January 30, 2018

0 Try this:
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | sh
source ~/.nvm/nvm.sh
nvm install --lts
nvm use --lts
npm --version

Load More Comments

This work is licensed under a Creative


Commons Attribution-NonCommercial-
ShareAlike 4.0 International License.


Copyright © 2018 DigitalOcean™ Inc.

Community Tutorials Questions Projects Tags Newsletter RSS 

Distros & One-Click Apps Terms, Privacy, & Copyright Security Report a Bug Write for DOnations Shop

You might also like