Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I install node.js in Ubuntu? I've been looking around, and I can't find anything. Is there a Ubuntu package for node.js, or do I have to compile it myself?

share|improve this question
    
You can also update node.js with npm davidwalsh.name/upgrade-nodejs – user284127 May 22 '14 at 7:54
1  
@jirg you should consider mentioning Nur's answer since this is the way current versions of node are installed now: askubuntu.com/questions/49390/… – jhohlfeld May 21 '15 at 15:12

12 Answers 12

  1. There is a nodejs-package in the official repositories (15.04). Consider also using nodejs-legacy for the node command.
  2. to update to the latest version, use the n package installed via npm:

    sudo npm cache clean -f
    sudo npm install -g n
    sudo n stable
    

See this SO question for a comparison of NVM and N.

share|improve this answer

As this question has the word latest and NodeJS latest release version is now v0.12.2 (as of today) and if you want to install this version you need to run following command

# Note the new setup script name for Node.js v0.12
curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash -

# Then install with:
sudo apt-get install -y nodejs

Update

NodeJS released v5.8.0 and I still found no ppa to install yet. So I install it using NVM as follows

First install nvm

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.26.1/install.sh | bash

Then install NodeJS v5.8.0

nvm install v5.8.0
share|improve this answer
    
confirmed. this is the current way to get node updated on ubuntu now. – jhohlfeld May 21 '15 at 15:11
    
Source: nodesource.com/blog/… – AlonL May 27 '15 at 8:47
    
I get an error from this: W: Failed to fetch https://deb.nodesource.com/node_0.12/dists/trusty/main/source/Sources Received HTTP code 403 from proxy after CONNECT — does this not work through apt-cacher-ng? – detly Jun 13 '15 at 12:34

Here's a solution that checks the md5sum once and compares it to the downloaded file, with an option to delete the file if the md5 sums don't match. It should address the safety complaints from Arda's answer.

#!/bin/bash
if [[ -z $1 ]]; then
  printf "Usage: ./scriptname <file or url> <optional output filename>\n"
  exit 1
fi

resource=$1
md5=`curl --silent --location ${resource} | md5sum | awk '{ print $1 }'`
filename="$(date +%Y-%M-%d-%H-%m-%s-file)"
if [[ -n $2 ]]; then
  filename=$2
fi
curl --silent --location $resource -o $filename
md52=`md5sum $filename | awk '{ print $1 }'`

if [[ $md5 == $md52 ]]; then
  printf "File sums match.\n"
  printf "Saved file to $filename\n"
else
  printf "File sums don't match.\n"
  #wrapping line to add newline, ugly, but it works
  read -rep "Delete file?
  " -n 1
fi

if [[ $REPLY =~ ^[Yy]$ ]]; then
  rm $filename
  exit 1
else
  exit 0
fi

Save that to a file such as safer-curl.sh, then do chmod +x safer-curl.sh Then execute like this:

./safer-curl.sh <file or url> <optional output filename>

Tested on Ubunt 12.04

share|improve this answer
up vote 156 down vote accepted

You can use the NodeSource repository:

sudo apt-get install curl
curl --silent --location https://deb.nodesource.com/setup_5.x | sudo bash -
sudo apt-get install nodejs

Then, you have the latest version of node.js installed on your machine.

share|improve this answer
1  
@muru Done... It is waiting peer reviewing. BTW, NodeSource is recommended by node.js team. – IAnsari Oct 20 '15 at 11:08
1  
@IAnsari Sounds great! – James Oct 20 '15 at 14:12
3  
-1 because this solution involves piping curl into a superuser shell. Could someome please follow up with a solution that doesn't use curled bash scripts to add the repositories? – nickguletskii Nov 22 '15 at 7:42
3  
@James I understand. However, saying that these directions are flawed is an understatement (although you could say that about pretty much everything node.js). While this is not as bad as it could be (at least the script is served over https), it still may result in a partial download, so that rm -rf /usr/blah/blah may turn into rm -rf /usr. This is just a generally very bad thing to do and I don't think that "that's the directions the devs provide" is a good excuse. – nickguletskii Nov 23 '15 at 15:44
2  
my problem it is installing as nodejs not node .. so if after installing nodejs , I am not able to access nodejs as "node" but as "nodejs" – Rizwan Patel Feb 25 at 16:47

NVM allows you to use multiple versions of Node and without sudo.

It is analogous to Ruby RVM and Python Virtualenv, widely considered best practice in Ruby and Python communities.

It downloads a pre-compiled binary where possible, and if not it downloads the source and compiles one for you.

Example:

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | sh
source ~/.nvm/nvm.sh

# May take a while if compilation required:
nvm install 0.9.0
nvm install 0.9.9

node use 0.9.0
node --version
#v0.9.0
node use 0.9.9
node --version
#v0.9.9

Since the sourcing has to be done for every new shell, you will probably want to:

echo 'source ~/.nvm/nvm.sh
nvm use 0.9.9 &>/dev/null
' >> ~/.bashrc
share|improve this answer

Generally speaking, loading arbitrary data from a URL into a root shell session is not a good idea and I wish people would stop peddling it as a solution for everything - "Please just run this script I'm sending you, and also while we're at it - I have a bridge you'd probably be interested in purchasing".

As an alternative, here's the "Ubuntu Way" of doing the same, where you can see how the system is being updated and know what repos and what keys are added to your system configuration:

apt-key adv --keyserver keyserver.ubuntu.com --recv 68576280
apt-add-repository "deb https://deb.nodesource.com/node_5.x $(lsb_release -sc) main"
apt-get update
apt-get install nodejs

This is for the latest (at time of writing) Nodejs version 5. Other versions can also be gotten with a simple change to the repo URL - consult nodesource.com documentation for details.

share|improve this answer

Node is one of the easier projects to build. Just change the version as that continues to change.

cd /usr/local/src
wget http://nodejs.org/dist/v0.8.21/node-v0.8.21.tar.gz
tar -xvzf node-v0.8.21.tar.gz
cd node-v0.8.21
./configure
make
sudo make install
which node

You should see /usr/local/bin/node.

share|improve this answer
2  
Tested on Ubuntu 14.04 LTS with Node.js v0.10.30 and it worked perfectly. To get the most recent release, go to nodejs.org/download. To see all releases: github.com/joyent/node/releases. – Lucio Paiva Aug 9 '14 at 15:12
6  
To whom it may concern, NPM will also be built and installed automatically. – Lucio Paiva Aug 9 '14 at 15:16

I was recently installing a utility via NPM when I learned that my version of Node.js itself was out of date. No worries -- simply upgrade my Node.js install and move forward. Of course I could just hit nodejs.org and get the new image, but figured there had to be an easier way. It turns out there is -- you can upgrade your local Node.js with NPM:

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

And adding to PATH, example (for Ubuntu)

echo "export NODE_PATH=$NODE_PATH:/usr/local/lib/node_modules" >> ~/.bashrc && source ~/.bashrc
share|improve this answer

answer for @jrg is correct, But Chris Lea's Launchpad PPA will will not be supporting Node.js v0.12 and beyond. So to install last version for Node.js From new nodesource PPA according to post in nodesource Blog And joyent/node

First :

curl -sL https://deb.nodesource.com/setup | sudo bash -

This script will:

  1. Clean up references to the old PPA if you are already using it
  2. Add the NodeSource signing key to your keyring
  3. Add deb.nodesource.com to your APT sources
  4. Perform an apt-get update with your new sources

Then install Node.js:

sudo apt-get install -y nodejs

Update: according post in nodesource blog

To install nodejs version 0.12.X

you nedd to run command:

curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash -

To install nodejs version 0.10.X

you nedd to run command:

curl -sL https://deb.nodesource.com/setup_0.10 | sudo bash -

Then

sudo apt-get install -y nodejs
share|improve this answer

I am always leery of using a non-official PPA - it usually works out, but I like there to be some level of official association between the distribution channel and the project that I am using...

Personally, this is the best bang for my buck when it comes to a resource for the many good ways to install Node - https://gist.github.com/isaacs/579814

share|improve this answer

Chris Lea PPA offers the most updated compiled version of Node.js.

Whom (and how) could I beg to maintain official "universe" version of Node.js as updated as Chris Lea's version?

I'd like to install official package from official repositories...Maybe it's a sillyness, but doing this gives me a feeling that there's more than just one person behind the future maintenance of the package.

Thanks

share|improve this answer
    
Looks like he can't put it into the repositories. twitter.com/chrislea/status/351941395893862402 also, he works for mediatemple, and there have been talks of an official nodejs ppa, just like there is for nginx. – James Jul 4 '13 at 1:02

Yes, go to Synaptic, search for "nodejs". The packages are located in the universe repository. I suggest you install all of the packages starting with nodejs if you are doing development.

Just in case that doesn't work:

sudo apt-get install g++ curl libssl-dev apache2-utils git-core
git clone git://github.com/joyent/node.git
cd node
./configure
make
sudo make install

That will download the sourcecode of node.js, make it and install it.

share|improve this answer
1  
Wouldn't "sudo apt-get build-dep nodejs" be more appropriate than your "apt-get install" line? – freddyb Jun 18 '11 at 16:10
    
@freddyb Doesn't hurt to have it like this. – nickguletskii Jun 18 '11 at 16:20
    
Thanks for your answer - however, I recently discovered that node.js has a "officially unofficial" PPA - so I answered with that, since it wouldn't be polite to rewrite your entire answer with the "correct" instructions. – James Nov 28 '11 at 1:23
2  
However I prefer this solution instead of sudo apt-get install nodejs, this last doesn't give you the latest version. – Rubens Mariuzzo Jan 12 '13 at 19:28
1  
@rubens not if you use the PPA I describe above. – James Oct 23 '13 at 12:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.