How to install the latest node.js on Ubuntu?

Node.js is an event-based and non-blocking I/O framework — built on Chrome’s JavaScript engine. Its goal is to provide an easy way to build scalable network programs.

In most linux distributions Node.js is available in a repository which can be accessed via a package manager such as apt-get or aptitude.

However in the repository of the brand new Ubuntu 12.10 (Quantal Quetzal) the nodejs package has the version number 0.6.19 while the current version is 0.8.14 as of this writing. Unfortunately many nodes — that’s how node.js applications are called — are constantly under development and often depend on a newer version.

If you are trying to install a node with the node package manager but providing an old node environment, you most probably expierence a similar error message to the one below.

npm ERR! error installing ws@0.4.22
npm WARN This failure might be due to the use of legacy binary "node" 
npm WARN For further explanations, please read
npm WARN /usr/share/doc/nodejs/README.Debian
npm WARN 

npm ERR! ws@0.4.22 install: `node install.js`
npm ERR! `sh "-c" "node install.js"` failed with 127
npm ERR! 
npm ERR! Failed at the ws@0.4.22 install script.
npm ERR! This is most likely a problem with the ws package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node install.js
npm ERR! You can get their info via:
npm ERR!     npm owner ls ws
npm ERR! There is likely additional logging output above.
npm ERR! 
npm ERR! System Linux 3.5.0-17-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install" "ws"
npm ERR! cwd /home/slopjong/
npm ERR! node -v v0.6.19
npm ERR! npm -v 1.1.4
npm ERR! code ELIFECYCLE
npm ERR! message ws@0.4.22 install: `node install.js`
npm ERR! message `sh "-c" "node install.js"` failed with 127
npm ERR! errno {}

Update (March 15th, 2013)

At the moment the repository has version 0.8.14 included while the latest version is version 0.10. This tutorial has been updated and successfully tested.

As maintaining old tutorials and old blog posts costs time, feel free to donate me a virtual beer. Drop me a message here and tell me what your favourite donation channel is (like flattr whehere I haven’t an account yet).

Preparation

The nodejs package depends on a couple of other packages, be sure that you have the repository component universe enabled.

# in /etc/apt/sources.list
deb http://archive.ubuntu.com/ubuntu quantal main restricted universe
deb http://security.ubuntu.com/ubuntu quantal-security main restricted universe
deb http://archive.ubuntu.com/ubuntu quantal-updates main restricted universe

After enabling it run sudo apt-get update.

Installation steps

In order to get a more recent nodejs version you need to add a personal package archive (ppa) and update the package index files. To do so open Terminal and execute the following commands.

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

Pay attention to the second line, if you are trying to execute apt-apt-repository you won’t have luck.

Install the node package manager

Update (March 15th, 2013)
You no longer need to install npm separately as nodejs includes it now.

To manage your nodes it’s recommended to install the node package manager (npm) too.

sudo apt-get install npm

Who prefers to compile npm from its sources will find this script very useful.

Update (March 20th, 2013)
It looks like somebody forgot to pay his bill. The link to the script is currently broken.

Update (April 15th, 2013, 19:50 UTC+2)
A guy from Israel has grabbed the domain which has expired three and a half hours ago.

29 Comments

  1. Rufus

    Thanks!!! this worked for me.

    Just pasting my old error so others will find this solution too.

    # ws@0.4.22 install /home/......./ttys/node_modules/tty.js/node_modules/socket.io/node_modules/socket.io-client/node_modules/ws
    # node install.js
    
    sh: 1: node: not found
    npm ERR! error installing ws@0.4.22
    npm WARN This failure might be due to the use of legacy binary "node"
    npm WARN For further explanations, please read
    npm WARN /usr/share/doc/nodejs/README.Debian
    npm WARN
    npm ERR! error installing socket.io-client@0.9.10
    npm ERR! error installing socket.io@0.9.10
    npm ERR! error installing tty.js@0.2.8
    
    npm ERR! ws@0.4.22 install: `node install.js`
    npm ERR! `sh "-c" "node install.js"` failed with 127
    npm ERR!
    npm ERR! Failed at the ws@0.4.22 install script.
    npm ERR! This is most likely a problem with the ws package,
    npm ERR! not with npm itself.
    npm ERR! Tell the author that this fails on your system:
    npm ERR!     node install.js
    npm ERR! You can get their info via:
    npm ERR!     npm owner ls ws
    npm ERR! There is likely additional logging output above.
    npm ERR!
    npm ERR! System Linux 3.5.0-18-generic
    npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install" "tty.js"
    npm ERR! cwd /home/wouterdb/install/ttys
    npm ERR! node -v v0.6.19
    npm ERR! npm -v 1.1.4
    npm ERR! code ELIFECYCLE
    npm ERR! message ws@0.4.22 install: `node install.js`
    npm ERR! message `sh "-c" "node install.js"` failed with 127
    npm ERR! errno {}
    npm ERR!
    npm ERR! Additional logging details can be found in:
    npm ERR!     /home/wouterdb/install/ttys/npm-debug.log
    npm not ok
    
  2. Binay

    Hey Thanks a lot.. It works beautiful i have v0.8.16 installed now :)

  3. xavierzhao

    非常感谢

  4. slopjong

    For those who don’t understand Chinese, xavierzhao said “Thank you”.

    http://translate.google.de/#auto/de/%E9%9D%9E%E5%B8%B8%E6%84%9F%E8%B0%A2

  5. ssm

    thank’s

  6. Excellent article! Your commands worked like a charm.

  7. slopjong

    I haven’t worked with node.js & Ubuntu for a while, can somebody tell me if the instructions still work as version 0.10 has recently been released?

  8. Max

    Don’ t work for version 0.10 (Ubuntu 12.10)

  9. slopjong

    I’ve reviewed the installation steps and updated it in some places. I guess universe is disabled on your machine as the instructions themselves still work.

    I’ve tested the instructions with the live version of ubuntu (12.10) it took me some time to find out that universe wasn’t enabled.

    Thankfully to your it doesn’t work feedback I came across the grep-aptavail tool which I didn’t know yet. Though a precise description with a paste of your terminal’s output would have been helpful.

  10. ABC

    Since node.js releases new versions frequently and there are often breaking changes, it is practical to look into something like node version manager (“nvm”) which helps you manage installations for multiple versions of node.

    https://github.com/creationix/nvm

  11. helmi

    As Ubuntu 13.04 minimal (maybe since 12.10)

    sudo apt-get install software-properties-common

  12. Justin

    Yes, as @helmi noted, the add-apt-repository command was moved to software-properties-common in Ubuntu 12.10.

  13. slopjong

    If you have universe enabled then the instructions should work. I guess it wasn’t enabled on your machine.

  14. Gregory Mace

    on Ubuntu 12.x
    apt-add-repository is obtained by:
    sudo apt-get install software-properties-common

  15. Mark

    https://github.com/mark-webster/node-debian

    We’ve been using this to build node.js packages direct from sources. It works at least for 0.8.x and 0.10.x

  16. hans

    Works like a charm! Thank you!

  17. kram

    this does not work in 13.04 – after doing the above it still keeps installing nodejs 0.6 it worked a day ago – tried this on this ami: ami-c30360aa – public amazon ami for 13.04 ubuntu on us east.

  18. Sándor Alvajáró

    It didn’t work for me on an ARM-based machine! It’s likely I have to upgrade my node.js from source : (
    Otherwise thanks!

  19. slopjong

    Sándor, would you tell me what exactly is not working? Doesn’t the ARM repository provide you a package or something else?

  20. 1fort

    sh: 1: node: not found

    can be fixed by installing ‘node-legacy’

  21. hare

    ty!!! goot work!!

  22. Sølve

    For ubuntu 13.10 this worked for me:

    sudo apt-get install npm
    sudo ln -s /usr/share/nodejs /usr/share/node

Trackbacks / Pings

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>