4

I installed Nodejs on my Ubuntu:

$ cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.6 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.6 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial

with the following commands:

sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm

The result is:

$ nodejs -v
v4.2.6

In the internet I see there much newer versions https://nodejs.org/en/download/releases/ . So,

  1. Why such old version is installed by default
  2. How to install newer one?

Thank you!

1
  • Because you use a 5 year old version of Ubuntu ... update to a newer ubuntu version to get newer software. Update path is 16.04 >> 18.04 >> 20.04, or make a new install 0f 20.04 directly.
    – Soren A
    Jul 17, 2020 at 12:45

2 Answers 2

4

4.2.6 is indeed the version of Node that is in the Xenial repos. Ubuntu does not do major version updates of packages within the same distribution, so if Node 4 was what was around when Xenial came out (which is to say, April of 2016, which sounds about right -- it would have been the then-current LTS release), then you're stuck with version 4 all the way through Xenial's lifetime until you upgrade the distro.

At least, that's the case when using the official repositories. Fortunately, a more up-to-date repository for Node does exist and is recommended on Node's official site. You can find detailed instructions here, but the tl;dr is:

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

Ubuntu has this version of nodejs because it was one of the LTS versions (long term support) and is very stable.

There are a few different ways to install newer versions. I would recommend installing it through nvm so you get access to the latest versions. Plus it helps to do it this way if you will be developing on multiple systems rather than leaving it up to the systems package manager. To do this first remove the current version

sudo apt-get remove nodejs

To ensure it's completely removed you can do sudo apt-get purge nodejs and sudo apt-get autoremove

Now we can prepare to use npm by installing build-essential and libssl-dev

sudo apt-get install build-essential libssl-dev

Now we can download the nvm installation script from the projects Github page

curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh -o install_nvm.sh

Run the install script with

bash install_nvm.sh

At this point it is easiest to logout, and then log back in to complete the setup of npm.

Now we can use the following commands to install the target nodejs version

npm ls-remote shows you the available versions

npm install (versionNumber)

npm use (versionNumber) You can have multiple versions installed so you can choose which one to use

then verify the version you have chosen to use with

node -v <- Please note that with npm the command is node and NOT nodejs

There is a great write up about all of this on Digital Ocean. For more information visit the link https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04

1
  • Great instructions, but I believe you've got three typos towards the end. It should be nvm ls-remote, nvm install (versionNumber) and nvm use (versionNumber). Oct 10, 2023 at 20:06

Not the answer you're looking for? Browse other questions tagged .