The proper way to install Nodejs on Ubuntu 18.04
How to install Nodejs on a brand new Ubuntu 18.04 installation
is a developer, technical writer, and a (former) designer.
Sometimes we need a quick refresher of the basics. Other times, we’re brand new to the subject.
Whatever might be the reason for you looking up this tutorial, here it is: how to install Nodejs on a brand new Ubuntu 18.04 installation.
Whenever we need to add a new package to our system, we need to update the apt:
sudo apt update
Once that’s done, we’ll run this command:
sudo apt install nodejs
To make sure our installation worked, we’ll run:
Alternatively, we could have run
nodejs --version, but the command above is faster to type.
Either way, we’ll get back:
To make sure we also have the Nodejs package manager (npm), we’ll run the following check:
We’ll likely get back:
Command 'npm' not found, but can be installed with: sudo apt install npm
This is exactly what we’ll do, so we’ll run:
sudo apt install npm
After the installation is done, let’s check the version:
The command above returns:
Updating Nodejs and npm versions on Ubuntu 18.04, using NVM
The next thing to do is potentially update the version of Nodejs and npm.
We could simply update them to the newest versions; however, there is a very useful alternative approach, utilizing NVM - the Nodejs version manager.
Using the Node version manager
There is a bash script known as NVM. Using NVM, we can work with a number of different versions of Node.js on our machine.
The up-to-date instructions on NVM can be found on NVM’s Github page.
Here’s how to install and use NVM on Ubuntu 18.04.
1. Install with curl
Run this curl command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.36.0/install.sh | bash
Running the above script does two things:
- clones the NVM repo to
- adds nvm path to Bash profile (or ZSH profile)
We can inspect this addition by running the less utility inside bash, like this:
If we scroll to the bottom of the
.bashrc file we opened with the above command, we’ll see this code:
export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
Another way to verify the NVM installation is to first reload
.bashrc with the following command:
… and after that simply run:
2. Install any Node.js version with NVM
After installing NVM with curl, we can now add any specific Node.js version to our system.
To get familiar with what commands are available, we can run the
nvm -h command. This command gives us a list of available commands with a short description for each.
We can inspect all the Node.js versions currently available on our system by running the following command:
To see all the Node.js versions that are available for installation, we can run:
This is quite a long list; we can filter it any way we like by piping it to the
less commands. For example, in the next command we’re filtering for LTS (long term support) versions only:
nvm ls-remote | grep -i 'LTS'
This still produces quite a long and repetitive list, so for convenience, we can instead run:
nvm ls-remote | grep -i 'Latest'
Finally, with the above command we get a simple list of only four items:
v4.9.1 (Latest LTS: Argon) v6.17.1 (Latest LTS: Boron) v8.17.0 (Latest LTS: Carbon) v10.22.1 (Latest LTS: Dubnium) v12.18.4 (Latest LTS: Erbium)
Now that we know how to inspect the versions of Node.js that exist on our system and those that we have available for installation from the remote source, we can finally install a version. Based on the above narrowed-down list of various LTS versions, we can pick a version to install. For example:
nvm install v12.18.4
We can confirm that this is the active version on our system like this:
We’ll get back
v12.18.4 in the console.
Additionally, we can confirm that
npm was also updated, with:
This time, we get back
That’s it, now we know how to install any version of Node.js on Ubuntu 18.04, using NVM.
To verify that this is the current version set by NVM, we can also run:
We’ll get back
To make this version the default one, we’ll run:
nvm alias default 12.18.4
3. Switching between various Node.js versions
To see how this works, we first need to install another version of Node.js. We’ve already done this, so it shouldn’t be too hard.
First, we’ll list all available Node versions with
After that, we’ll install another version like described earlier in this tutorial. For example, let’s say we’ve installed v10.22.1, with this command:
nvm install 10.22.1
We can check all the Node.js versions on our system using this command:
In our case, this command returns:
-> v10.22.1 v12.18.4 system default -> 12.18.4 (-> v12.18.4) node -> stable (-> v12.18.4) (default) stable -> 12.18 (-> v12.18.4) (default) iojs -> N/A (default) unstable -> N/A (default) lts/* -> lts/erbium (-> v12.18.4) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.17.0 (-> N/A) lts/dubnium -> v10.22.1 lts/erbium -> v12.18.4
Now that we have two versions of Node.js on our system, we’ll switch between them with the help of the
For example, the following command switches to the newly installed Node.js v10.22.1:
nvm use v10.22.1
And this one switches to Node.js v12.18.4:
nvm use v12.18.4
Alternatively, since we’ve se Node.js v12.18.4 to be the default version, we can switch to it using this command:
nvm use default
We’ll get back the following in the console:
Now using node v12.18.4 (npm v6.14.6)
4. Setting aliases with node versions using nvm
There’s a nifty feature of nvm: aliases.
nvm alias project-name 10.22.1
Now we can run the
use command with the
nvm use project-name
Doing the above will switch to version 10.22.1 of Node.js.
Do delete the
project-name alias, we use the
nvm unalias project-name
Doing so will return this output in the console:
Deleted alias project-name - restore it with `nvm alias "project-name" "10.22.1"`