61

How can I install NodeJS 4 on Ubuntu using apt-get utilities?

Braiam
  • 67,791
  • 32
  • 179
  • 269

5 Answers5

75

Instructions were taken from here: https://github.com/nodesource/distributions

wget -qO- https://deb.nodesource.com/setup_4.x | sudo bash -

and then:

sudo apt-get install nodejs

Here is the system versions:

ubuntu@424c7702-0947-e7c7-c532-dfec484fc109:~$ lsb_release -r
Release:    15.04
ubuntu@424c7702-0947-e7c7-c532-dfec484fc109:~$ node -v
v4.0.0
ubuntu@424c7702-0947-e7c7-c532-dfec484fc109:~$ npm -v
2.14.2
Braiam
  • 67,791
  • 32
  • 179
  • 269
jarsever
  • 1,095
  • 10
  • 13
  • @jarserver - "The following packages have unmet dependencies: nodejs : Depends: rlwrap but it is not installable E: Unable to correct problems, you have held broken packages."- Error while apt-get install. How to fix this? – Kumar Sambhav Sep 12 '15 at 04:22
  • @KumarSambhav sounds like your package database is in a broken state search the site for how to fix apt broken state. – battlemidget Sep 14 '15 at 03:51
  • This doesn't work for me on ubuntu 15.10 It always installs v0.10.25 (lower version than the OP, so I have no idea what's going on...) – HankScorpio Feb 24 '16 at 22:03
  • @HankScorpio It sounds like the apt-repo was not updated. Try following the manual installation instruction (https://github.com/nodesource/distributions#manual-installation) and see if that fixes it. – jarsever Feb 25 '16 at 04:41
  • Yes, that's what the problem was. Some GPG keys (whatever they are) were missing, so apt-get update was failing every time. I used y-ppa-manager to fix the problem and now I have the latest version of NodeJS 4. – HankScorpio Feb 25 '16 at 18:09
  • https://www.seancassidy.me/dont-pipe-to-your-shell.html - Friends don't let friends pipe to sh – Guss May 31 '17 at 09:23
  • We really should not be encouraging wget | sudo bash. Downvoted. – kiko Jul 04 '18 at 13:39
37

Node Version Manager always has the latest

I'm strongly of the opinion that installing Node with Node Version Manager is the best option on Ubuntu, if you're installing it on a computer where you intend to do development (instead of a production server).

When you install through the official repositories, you end up with something terribly outdated. You can always add a PPA, but you'll still end up with messy permissions where globally installing modules from npm requires admin privileges.

With NVM, everything is kept in your home folder (so no need for sudo), and you can install multiple versions of Node (including 4.0) and switch between them with ease.

Installation with NVM

Taken from the NVM installation instructions:

Grab the latest copy of NVM (you may need to sudo apt-get install curl first):

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

Tell your shell to use nvm (you may want to add this to ~/.bashrc so it happens automatically in the future):

source ~/.nvm/nvm.sh

Then install the latest node version:

nvm install 4.0

And tell nvm which version of Node you want to use:

nvm use 4.0

You may also want to add the nvm use 4.0 line to your ~/.bashrc, so that you don't have to pick a node version each time you start your terminal.

Now if you check which node it should give you a path to the node executable inside your home folder. Running node --version should tell you you're running v4.0.0.

  • 6
    I don't think the messy permissions comment is accurate, I install node from nodesource apt repo and can run and install node modules in my local home directory (including -g global option). All it takes is updating ~/.npmrc to define your prefix directory. – battlemidget Sep 14 '15 at 03:54
  • 3
    Ah, that's a very fair point. I'm still a fan of nvm for the version management aspect, but if that's not your thing, setting a prefix in ~/.npmrc definitely seems to be the right way to handle permissions for global modules. – Michael Martin-Smucker Sep 15 '15 at 22:28
  • I believe the "outdated" note about the official repositories, in the answer is a bit ... outdated ;-) The official repositories now support any publicly released version on a supported release train - there're repos for version 6, 7 and even 8, and has been like that since at least my answer below (10/15) – Guss May 31 '17 at 09:10
35

With kudus to @jarsever, I personally don't subscribe to the "curl|sh" paradigm.

If you feel the same kind of unease as I do when asked to just pipe some arbitrary text off the internet and into a root account's shell process, then you may want to try this for the same effect but with (slightly) less fear, uncertainty and doubt:

version=4
apt-key adv --keyserver keyserver.ubuntu.com --recv 68576280
apt-add-repository 'deb https://deb.nodesource.com/node_${version}.x precise main'
apt-get update
apt-get install nodejs

I believe the process should be clear, and you can also do the same through Ubuntu's Software Properties UI.

Guss
  • 3,535
  • Is there any disadvantage to doing it this way? Seems like the best option... – Catskul Oct 27 '15 at 03:36
  • 6
    It's more than one line, and allows you to learn about your system?... Some people don't like that, I think, that's the only way I can explain the curl|sh phenomenon. – Guss Oct 27 '15 at 08:21
  • 3
    In addition to getting to learning about about your OS's package manager, it's also a security issue. curl|sh pulls a script from a server, and executes it directly in a shell. I personally trust nodesource, but it's not a great habit to get into. http://unix.stackexchange.com/questions/46286/read-and-confirm-shell-script-before-piping-from-curl-to-sh-curl-s-url-sh – blanket_cat Nov 21 '15 at 01:40
  • And there asking you to pipe it into sudo -E bash. It's running as root. I highly recommend you read, and understand the script, if you're going to pipe it from the net into a root shell. – blanket_cat Nov 21 '15 at 01:56
  • 1
    Generally speaking, by installing a deb package you take the same amount of risk as piping the internet into a root shell. That being said, one implies you understand your system, the other implies that you don't - and that is a huge difference. – Guss Nov 21 '15 at 08:47
  • This answer is at least more reliable and secure, as it follows a chain of trust. – Braiam Jul 14 '16 at 01:32
0

I like to use nodeenv from pypi (https://pypi.python.org/pypi/nodeenv), you install the package using pip, then setup a "node/virtualenv" and tell it to install a prebuilt version, fast and simple. Paul

Paul
  • 111
-1

This worked for me

echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
. ~/.bashrc
mkdir ~/local
mkdir ~/node-latest-install
cd ~/node-latest-install
curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
./configure --prefix=~/local
make install 
curl https://www.npmjs.org/install.sh | sh

Source: https://gist.github.com/isaacs/579814