Tutorial

How To Install Node.js on an Ubuntu 14.04 Server

Published on May 12, 2014
English
How To Install Node.js on an Ubuntu 14.04 Server
Not using Ubuntu 14.04?Choose a different version or distribution.
Ubuntu 14.04

Introduction

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

In this guide, we’ll show you how to get started with Node.js on an Ubuntu 14.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.

How To Install the Distro-Stable Version

Ubuntu 14.04 contains a version of Node.js in its default repositories that can be used to easily provide a consistent experience across multiple servers. The version in the repositories is 0.10.25. This will not be the latest version, but it should be quite stable.

In order to get this version, we just have to use the apt package manager. We should refresh our local package index prior 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 that 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.

Below, we’ll discuss some more flexible 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 probably have more up-to-date versions of Node.js than the official Ubuntu repositories.

First, you need to install the PPA in order to get access to its contents. This depends on the version you wish to install.

For the most recent LTS (the 6.x branch), use:

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -

For the older LTS (the 4.x branch), use:

curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -

For the currently active release (the 7.x branch), use:

curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -

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 that you did above:

sudo apt-get install nodejs

The nodejs package contains the nodejs binary as well as npm, so you don’t need to install npm separately. However, in order for some npm packages to work (such as those that require building from source), you will need to install the build-essentials package:

sudo apt-get install build-essential

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”.

Using nvm, you can install multiple, self-contained versions of Node.js which will allow you to control your environment easier. It will give you on-demand access to the newest versions of Node.js, but will also allow you to target previous releases that your app may depend on.

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 and install it with the following syntax:

<pre> curl https://raw.githubusercontent.com/creationix/nvm/<span class=“highlight”>v0.16.1</span>/install.sh | sh </pre>

This will download the script and run it. 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

. . .
 v0.11.6
 v0.11.7
 v0.11.8
 v0.11.9
v0.11.10
v0.11.11
v0.11.12
v0.11.13

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

nvm install 0.11.13

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 0.11.13

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

v.0.11.13

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 0.11.13

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:

<pre> npm install <span class=“highlight”>express</span> </pre>

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

<pre> npm install -g <span class=“highlight”>express</span> </pre>

This will install the package in:

<pre> ~/.nvm/<span class=“highlight”>node_version</span>/lib/node_modules/<span class=“highlight”>package_name</span> </pre>

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

<pre> npm link <span class=“highlight”>express</span> </pre>

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

nvm help

Conclusion

As you can see, there are a quite a few ways to get up and running with Node.js on your Ubuntu 14.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.

<div class=“author”>By Justin Ellingwood</div>

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

been trying to run p2pool-scanner and it keeps throwing up this error

http://pastebin.com/jQtpfwjG

Andrew SB
DigitalOcean Employee
DigitalOcean Employee badge
May 21, 2014

@anthonyschimke: Looks like p2pool-scanner was written for express 3.x but you have express 4.x installed. Express 4.x does not have a configure method. This is a bug in p2pool-scanner. You might be able to work around it by, uninstalling express and then reinstalling it forcing the old version:

<pre> npm uninstall -g express npm install -g express@3.8.0 </pre>

thanks that seems to have done it

Thanks for the info.

When I use the sudo add-apt-repository ppa:chris-lea/node.js method, Node installs fine (0.10.28). And later I can install NPM ok (1.4.9). However when I try npm install express (or npm install -g express), I get errors (just a sample below):

npm ERR! Error: EACCES, mkdir ‘/usr/share/nodejs/node_modules’ npm ERR! { [Error: EACCES, mkdir ‘/usr/share/nodejs/node_modules’] npm ERR! errno: 3,

npm ERR! Please try running this command again as root/Administrator.


From what I can understand, using the ppa:chris-lea method means I can only install express using sudo. But I’ve read that this is not recommended.

I’ve tried chowning to my whoami username in several directories, but I’m still having no luck finding a solution.

Can you possibly help me?

Thanks

FWIW, I have a node file (test.js) in my /usr/share/node directory which runs fine in my terminal on port 8080.

Andrew SB
DigitalOcean Employee
DigitalOcean Employee badge
June 3, 2014

@bjones1831: You should only need <code>sudo</code> if you are installing it globally with “<code>-g</code>” Are you running it in <code>/usr/share/nodejs/</code> ? If so, that’s why you need root permissions. Try moving your project to a directory you have write access to, like your home directory.

Where is node path if you install it via the PPA?

Andrew SB
DigitalOcean Employee
DigitalOcean Employee badge
June 9, 2014

The PPA also installs the binary /usr/bin/nodejs So you might want to run:

<pre> sudo ln -s /usr/bin/nodejs /usr/bin/node </pre>

I am really having a lot of difficulty in getting this to work for me. In the year and a half in to learning software development, I am finding that I most often have problems with having to fight with getting my dev environment set up correctly than I do actually learning the programming language(s). Anyway, I made up some gist files of my terminal output in the order I have tried getting this to work for me and would greatly appreciate any help with getting this to work for me.

First I tried this:

https://gist.github.com/jacqueline-homan/ce96b7e21493ca13e734#file-unable-to-get-yoeman-and-bower

Then I tried this (which worked for the npm part of it):

https://gist.github.com/jacqueline-homan/d1a051750dc360d058bb#file-this-worked

But when I tried to install nvm, it failed and I don’t know how to get it to work for me:

https://gist.github.com/jacqueline-homan/bebbb88250ee5f776e64#file-installing-nvm-did-not-work

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
July 14, 2014

@jacquelinehoman7: https://raw.githubusercontent.com/creationix/nvm/v.10.0/install.sh is invalid. You can find the correct URL here: creationix/nvm: https://raw.githubusercontent.com/creationix/nvm/v0.10.0/install.sh.

These instructions are incomplete. Packages like grunt will attempt to execute scripts at the command line using “node”, not “nodejs”. I for one am looking for instructions about the correct way to fix this. I could remove the package (“node”) but I don’t know what it does. Or I could remove it from my path…

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.