But before you can build things with it, you'll need to have it installed on your machine.
In this article, we'll walk you through three different ways to install Node.js on a machine running the Ubuntu 18.04 operating system.
Below are the three methods we'll cover:
- Using the Ubuntu Default Repository &
- Using the NodeSource Node.js PPA (Personal Package Archive)
- Using the Nvm (Node.js Version Manager) Tool
Feel free to skip to whatever section you wish to use.
Let's get started!
Table of Contents
- Install Using the Ubuntu Default Repository
- Install With the NodeSource Node.js PPA (Personal Package Archive)
- Install Using the Nvm (Node.js Version Manager) Tool
Option 1 — Ubuntu Default Repository
Ubuntu 18.04 holds a version of Node.js in its default repositories. This won't be the latest version, but it will be stable and sufficient for most use cases.
You can install the Node.js default repository using the
apt command. This is a command-line tool that makes things like installing new software packages, upgrading existing software packages, and updating the package list index super easy.
First, we need to ensure all your local package indexes are up-to-date:
sudo apt update
apt package index is a database of available packages from the repository defined on your system. After running that command, that index on your machine will be updated with all the latest changes made to those repositories.
When that's done updating, you can install Node.js using this command:
sudo apt install nodejs
To ensure you've successfully installed Node.js, execute this command:
This will output the version of Node.js you installed.
In most cases, you'll also want to install NPM alongside Node.js. Npm (Node.js Package Manager) is a package manager for Node.js and will allow you to easily install NPM packages and import them into your code.
You can install it with this command:
sudo apt install npm
And you can verify you've successfully installed NPM with this command:
That will output the current version of NPM installed on your machine.
Awesome! You now have Node.js and NPM installed on your Ubuntu 18.04 machine. In the next sections, we will go over some additional ways to install and configure Node.js that provide some additional features.
Option 2 — NodeSource Node.js PPA (Personal Package Archive)
Another option for installing Node.js on Ubuntu 18.04 is to use the NodeSource Node.js PPA (Personal Package Archive). This will have a wider collection of Node.js versions for you to choose from to install.
First, you need to use
wget to get the installation script for the specific Node.js version you want to install.
Make sure you are in your home directory:
And you can get the installation script and run it with this command (make sure you replace the
10.x portion with whichever version you want):
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
When that command finishes, the PPA will be added to the configuration on your machine. And you can now install the Node.js package with this command:
sudo apt-get install -y nodejs
To ensure everything was installed correctly, check for the version of Node.js currently installed on your machine:
This will output something similar to this:
NPM is included in what you just installed, so you don't need to install it separately.
You can verify
NPM has been installed with this command:
It'll output something similar to this:
You'll need to run one more additional command for
NPM to work properly in all cases:
sudo apt-get install -y build-essential
Awesome! You now have Node.js and
NPM configured on your Ubuntu 18.04 machine. And you're ready to start coding!
Option 3 — Nvm (Node.js Version Manager) Tool
The third installation method we'll cover is using the Nvm (Node.js Version Manager) tool. This tool will make it easy to install and use multiple versions of Node.js without adding much extra complexity. There will be times when an application needs different versions of Node.js to work, so having the flexibility to change that is important and will save you a lot of time.
To get the
nvm installation script from their Github page, use either the
wget command below:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
To gain access to using
nvm, you may need to open a new terminal window or log out and back in again:
nvm is installed correctly, something similar will be printed to your console:
Now that you have
nvm installed on your machine, you can install isolated versions of Node.js.
You can use this command to see what versions are available to be installed:
This will log a huge list of Node.js versions for you to choose from:
. . . v9.10.0 v9.10.1 v9.11.0 v9.11.1 v10.0.0 v10.1.0
To install a specific version from that list, you can use a command similar to this (update the command to install the version you want):
nvm install 10.16.3
And you can also use whatever Node.js version you want with this command:
nvm use 10.16.3
Now, that version will be used whenever you use Node.js on your machine.
To set one version as the default on your machine, execute this command:
nvm alias default 10.16.3
That version you specified will be automatically selected whenever a new session is spawned on your Ubuntu 18.04 machine.
To learn more about additional options provided by
nvm, type this command in your terminal:
Node.js is now installed using
nvm on your Ubuntu 18.04 machine!
As you can see, there are several ways for you to get Node.js installed on your Ubuntu 18.04 machine.
Thanks for reading and happy coding!