Why does installing node 6.x on Ubuntu 16.04 actually install node 4.2.6?

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.


Also of note: this will remove the npm package but that's because nodejs with this upstream packaging will include npm with it - no need for the npm package.


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 (use wget in this command if curl isn't installed, and if neither are installed, install one of them):

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


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.


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

Tags:

Nodejs

16.04