73

These were my steps to install node on Ubuntu 16.04:

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs 
sudo apt-get install -y npm

which are the official instructions:

https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions

After doing this, running nodejs --version returns v4.2.6.

As I used setup_6.x I was assuming a version beginning with 6 would be installed?

I thought perhaps setup_6.x should be setup_6.2.1, but that page returns a 404, see:

https://deb.nodesource.com/setup_6.x (there is a page there)

https://deb.nodesource.com/setup_6.2.1 (returns a 404)

How do I install the latest stable version of node on Ubuntu 16.04?

Edit:

These are results after running sudo apt-get install -y nodejs:

sudo apt-get install -y nodejs 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  gyp libboost-python1.58.0 libjs-inherits libjs-node-uuid libjs-underscore
  libssl-dev libssl-doc libuv1-dev linux-headers-4.4.0-18
  linux-headers-4.4.0-18-generic linux-headers-4.4.0-21
  linux-headers-4.4.0-21-generic linux-image-4.4.0-18-generic
  linux-image-4.4.0-21-generic linux-image-extra-4.4.0-18-generic
  linux-image-extra-4.4.0-21-generic linux-signed-image-4.4.0-18-generic
  linux-signed-image-4.4.0-21-generic python-configobj python-pycurl
  python-pyexiv2 python-pyexiv2-doc
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
  nodejs
0 to upgrade, 1 to newly install, 0 to remove and 0 not to upgrade.
Need to get 0 B/3,162 kB of archives.
After this operation, 13.2 MB of additional disk space will be used.
Selecting previously unselected package nodejs.
(Reading database ... 329473 files and directories currently installed.)
Preparing to unpack .../nodejs_4.2.6~dfsg-1ubuntu4_amd64.deb ...
Unpacking nodejs (4.2.6~dfsg-1ubuntu4) ...
Processing triggers for doc-base (0.10.7) ...
Processing 1 added doc-base file...
Registering documents with scrollkeeper...
Processing triggers for man-db (2.7.5-1) ...
Setting up nodejs (4.2.6~dfsg-1ubuntu4) ...
update-alternatives: using /usr/bin/nodejs to provide /usr/bin/js (js) in auto mode

Edit:

Results of apt-cache policy nodejs:

apt-cache policy nodejs
nodejs:
  Installed: 4.2.6~dfsg-1ubuntu4
  Candidate: 4.2.6~dfsg-1ubuntu4
  Version table:
 *** 4.2.6~dfsg-1ubuntu4 500
        500 http://au.archive.ubuntu.com/ubuntu xenial/universe amd64 Packages
        100 /var/lib/dpkg/status
  • Include a copy of the output of apt-cache policy nodejs please into your question as an edit. – Thomas Ward Jun 13 '16 at 2:33
  • The fact the system pulled in NodeJS 4.x means that either you ran the incorrect scripts, or the repository was not correctly added to the system - using the exact steps outlined in the page you link to, I was able to get the latest installed as-is, so there could be any number of issues - if you can provide apt-cache data so we can see what your computer sees exists in repos, we may be able to help further. – Thomas Ward Jun 13 '16 at 2:42
  • @ThomasW. - Added data to post. – user1063287 Jun 13 '16 at 3:00
  • 1
    Thanks for the updated data. Looks like the script didn't do what it was supposed to for whatever reason. I detail that this is the case, and the 'manual' way to do what the script is doing, in my answer below. You can use that to get the latest version from NodeJS. It's not as nice and easy as a script, but it's really what the script does at its core. – Thomas Ward Jun 13 '16 at 3:02
  • 1
    @pipe Thankfully there's people like me who run scripts in destructible VMs so we can see what it's doing. That's how I derived the instructions in my answer, so we can do what that 'script' does manually heh. – Thomas Ward Jun 13 '16 at 13:28
130

The core reason is that the setup script didn't run correctly. Therefore, thanks to the updated data from the OP which supports this, the data from the NodeSource repository was never seen by apt because it was not properly configured by the script.

The script, therefore, may not have found your distribution, or it may have messed up when configuring the repository, or there may have been a network interruption, or any of a thousand reasons it was disrupted and didn't do its job.

The fact you are seeing version 4.x getting installed means that the script didn't do its job right, so the script is not necessarily at fault. This just means we have to potentially do this a harder way.

I should point out: the script doesn't actually do any installing - all it does is determine the Debian/Ubuntu version you're on, and configure the repository for it to get data from. The installation part is actually the sudo apt-get install step you ran by hand.


Rather than rely on the script, we can do what the script is doing the old-school way: by hand, ourselves, set up the repository configuration and install NodeJS.

Here's the manual way of making this work, and it is basically what the script does (except for Step 4, which is to make sure you are getting accurate version data):

  1. Create a new file: /etc/apt/sources.list.d/nodesource.list

    You'll need to create this file with sudo, but when you create the file, put this inside it:

    deb https://deb.nodesource.com/node_6.x xenial main
    deb-src https://deb.nodesource.com/node_6.x xenial main
    

    Then, save the file. (replace node_6.x with node_7.x or node_8.x, etc. for newer Node versions)

  2. Download the GPG Signing Key from Nodesource for the repository. Otherwise, you may get NO_PUBKEY errors with apt-get update:

    curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key add -
    
  3. Manually run sudo apt-get update.

    This refreshes the data from the nodesource repo so apt knows a newer version exists.

    If you get a NO_PUBKEY GPG error, then go back to Step 2

  4. Check apt-cache policy nodejs output.

    This is not done by the script, but you want to make sure you see an entry that says something like this in the output (though the version might be different if you are not using 6.x as the version string; the only thing we care about is that there's a newer version number provided via nodesource):

    Version table:
        6.2.1-1nodesource1~xenial1 500
           500 https://deb.nodesource.com/node_6.x xenial/main amd64 Packages
        4.2.6~dfsg-1ubuntu4 500
           500 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages
    

    If you do not see entries like this, and only see 4.2.6, start over. Otherwise, proceed.

  5. Install the nodejs binary. Now that you have confirmed 6.x is available on your system, you can install it: sudo apt-get install nodejs

  6. nodejs --version should now show v6.2.1 or similar on output (as long as it starts with v6. you're on version 6 then; this may be a higher version number if you're using a newer version than 6 but as long as it is not 4.2.6 you should be good to go).

  • 3
    Followed this to a T and all points matched described behaviour. Thank you very much. – user1063287 Jun 13 '16 at 3:08
  • 1
    @user1063287 You're welcome! Hopefully this helps others if the script doesn't work for them either! Enjoy using the latest NodeJS! – Thomas Ward Jun 13 '16 at 3:09
  • Oh, so that's why I don't actually have Node 6 when I thought I do, and programs are complaining I need version 6. Thanks! – cat Jun 13 '16 at 12:32
  • Thanks, worked great, I noticed that in my /etc/apt/sources.list.d/nodesource.list that there was a comment about it being disabled - also had the old 0.x version in there and I was at 4.x with the node -v. Nice clear instructions. – DaveStSomeWhere Oct 15 '16 at 16:35
  • 1
    Given that curl isn't installed by default, an alternative is to use wget wget -O - https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key add - – gerardw Jun 19 '17 at 15:30
37

I had an older version of node. All I needed to do was to purge the old one:

sudo apt-get purge nodejs npm

And then, replacing 6 in v=6 with 7, 8, 9 as needed for the respective versions (see official installation instructions):

v=6
curl -sL https://deb.nodesource.com/setup_$v.x | sudo -E bash -

(be sure you have curl installed.)

And lastly,

sudo apt-get install -y nodejs

Boom, latest version of node.

  • 4
    Or, https://deb.nodesource.com/setup_7.x for node v7. See official guide – ryenus Dec 27 '16 at 6:12
  • 2
    Mine still installs v4.2.6 – Freek Nortier Apr 19 '18 at 13:29
  • 1
    @FreekNortier I was also having this problem on Linux Mint 18. I was able to install version 11 after purging and running sudo apt-get install python-software-properties. I found this suggestionon tecadmin and codebind. – Ben Mar 7 at 1:54
4

For the Ubuntu 16.04.2 version user(with a little bit change from Thomas'post and thank for him )

1.open the software updater

2.setting

3.other software

4.Add the sources but remember to choose all new sources option later exp:


deb https://deb.nodesource.com/node_6.x xenial main
deb-src https://deb.nodesource.com/node_6.x xenial main

5.reload

6.sudo apt-get update

7.apt-cache policy nodejs //to get the new version table and check if the source is setup done

8.sudo apt install nodejs

9.nodejs --version

NOW It is all set.......

warning: do not change your Linux default driver for your nvidia card from the updater panel ....the system will be crashed....!!!

3

I had the same issue. But in my case I had to upgrade my curl command. You can see the issue by running with the -S option.

curl -s -S https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key add -

Which showed the issue

curl: (1) Protocol "https" not supported or disabled in libcurl
gpg: no valid OpenPGP data found.

So I fixed this issue by running the following.

sudo apt-get install curl
sudo apt autoremove
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install nodejs
1

By adding @Thomas Ward answer those who are facing issue in installing nodejs 8.x with https URL or the code keep on installs v4.2.6 by showing certificate error try adding nodesource.list with below lines

deb http://deb.nodesource.com/node_8.x xenial main
deb-src http://deb.nodesource.com/node_8.x xenial main

next step : Run

sudo curl -sL http://deb.nodesource.com/setup_8.x | sudo -E bash -

Then

sudo apt-get install -y nodejs

This will solve your problem by installing updated nodejs version and npm version as it solved mine for Ubuntu 16.04 LTS...

0

I found this link and it help me to install Nodejs8____https://www.ubuntuupdates.org/ppa/nodejs_7.x . I had to join Nojs8https in that line because it was saying that I needed to be level two to be able to post more than two links. I thinks that is random but whatever.

Before installing it I ran the command: apt-get remove --purge version nodejs

To verify you don't have any version installed run: apt-cached policy nodejs and or nodejs --version

I was able to install Node js 8 by just changing the 7 to 8 and I ended up with version 8.1.3. To do that I ran the commands that are in that page I posted above but here are the commands anyways:

Download the the repository key with:

curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add -

Then setup the repository::

sudo sh -c "echo deb https://deb.nodesource.com/node_7.x zesty main \ > /etc/apt/sources.list.d/nodesource.list"

sudo apt-get update

sudo apt-get install nodejs

Again, if you want node js 8 you can change number 7 in that command line to 8 and voila!

0

I attempted to follow the instructions on https://github.com/nodesource/distributions for version 8, but that did not work for me. I visited https://deb.nodesource.com/setup_8.x directly in browser and copied and pasted the script into a file on my system nodejs.sh. Then I executed the following commands

sudo -E bash nodejs.sh
apt-cache policy nodejs
sudo apt-get install nodejs

The command apt-cache showed two versions with a preference for installing 8. You may delete the install file afterwards. Not sure why copying the file locally helped because the curl command showed the script properly.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

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