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=""
SUPPORT_URL=""
BUG_REPORT_URL=""
VERSION_CODENAME=xenialwith the following commands:
sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npmThe result is:
$ nodejs -v
v4.2.6In the internet I see there much newer versions . So,
- Why such old version is installed by default
- How to install newer one?
Thank you!
12 Answers
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 | sudo -E bash -
sudo apt-get install -y nodejs 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 -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