Node.js is an open-source, cross-platform, asynchronous JavaScript run-time environment that executes JavaScript code on the server-side. This means that you can run JavaScript code on the server as a standalone application without involving any browser. Node.js is designed to develop high-performance and scalable network applications.

The main strength of Node.js lies in an event-driven, non-blocking I/O model that makes it lightweight and efficient.

Node Package Manager (NPM) is the default package manager for Node.js and the largest ecosystem of open source libraries in the world.

In this article, you will learn how to install Node.js and NPM on Ubuntu 18.04. The same instructions apply for any Ubuntu-based distribution like Kubuntu, Linux Mint, and Elementary OS.

There are several ways to install Node.js on a Ubuntu 18.04 server depending on your environment and needs. Here we will show you the three simplest and most efficient ways.

Prerequisites

Before continuing with the installation of Node.js, make sure:

Installing Node.js and NPM from Ubuntu Repository

The simplest and fastest way to install Node.js and npm on a Ubuntu 18.04 server is through the official distribution repository. Both Node.js and npm packages are included in default Ubuntu repositories.

At the time of writing this article, the Node.js version included in the Ubuntu repositories is 8.10.0 which is the last LTS version.

To install this version, you can use the apt package manager. Let us start with refreshing the local package index by running the following command:

$ sudo apt update

Next, execute the following command to install Node.js and npm:

$ sudo apt install nodejs npm

That's it. You have successfully installed both Node.js and npm on your Ubuntu 18.04 server.

Remember: The Node.js executable from the Ubuntu repositories is called nodejs and not node. It is because of the conflict with an already existing package.

You can now check which versions of Node.js and npm are installed by typing:

$ node -v && npm -v

You will see the following output:

v8.10.0
3.5.2

Although the Node.js version installed through this method is not the latest LTS release, this is the easiest and faster way of getting started with Node.js and npm.

Installing Node.js and NPM using NVM

Ab alternative to installing Node.js through apt package manager is to use NVM (Node Version Manager) tool. NVM is a bash script that is used to install and manage multiple Node.js versions on the same machine.

Through NVM, you can quickly add or remove any Node.js version to your Ubuntu 18.04 machine — something really helpful for testing the code on a specific Node.js version. You can easily switch between an LTS version and the latest available release.

Follow the below steps to install Node.js and npm through NVM.

Step 1 — Install NVM Script

Let us download and install the NVM script by running the following command:

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

The above command will copy the NVM repository contents from GitHub to the ~/.nvm directory on your local file system. It will also update your ~/.profile file.

The next step is to either log out and log back in or load the changes made to your bash profile by using the source command:

$ source ~/.profile

Once the NVM installation is completed and the script is added to your PATH variable, you can verify it by tying the following:

$ nvm --version

You should see the following output:

0.34.0

Step 2 — Install Node.js and NPM

With NVM installed, you can install the latest Node.js version by running the following command:

$ nvm install node

The output should look something like below:

Downloading and installing node v12.11.0...
Downloading https://nodejs.org/dist/v12.11.0/node-v12.11.0-linux-x64.tar.xz...
############################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.11.0 (npm v6.11.3)
Creating default alias: default -> node (-> v12.11.0)

Verify the installation by typing:

$ node -v

You should see the following output:

v12.11.0

To install the latest LTS version through NVM, just use --tls flag like below:

$ nvm install --lts 

The above command will install and switch to the latest TLS version which is v10.16.3 at the time of writing. You should always use this version in a production environment for the sake of stability and support.

To list all the available Node.js versions that you can install, type the following:

$ nvm ls-remote

Here is how the list looks like (I have omitted the older versions):

...
->     v10.16.3   (Latest LTS: Dubnium)
        v11.0.0
        v11.1.0
        v11.2.0
        v11.3.0
        v11.4.0
        v11.5.0
        v11.6.0
        v11.7.0
        v11.8.0
        v11.9.0
       v11.10.0
       v11.10.1
       v11.11.0
       v11.12.0
       v11.13.0
       v11.14.0
       v11.15.0
        v12.0.0
        v12.1.0
        v12.2.0
        v12.3.0
        v12.3.1
        v12.4.0
        v12.5.0
        v12.6.0
        v12.7.0
        v12.8.0
        v12.8.1
        v12.9.0
        v12.9.1
       v12.10.0
       v12.11.0

Feel free to choose the one you want from the above list.

Let us install one more Node.js version — the previous LTS version 8.10.0:

$ nvm install 8.10.0

NVM will automatically switch to the most recently installed version. You can list all the installed versions by typing:

$ nvm ls

The output should look something like below:

->      v8.10.0
       v10.16.3
       v12.11.0
default -> node (-> v12.11.0)
node -> stable (-> v12.11.0) (default)
stable -> 12.11 (-> v12.11.0) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/dubnium (-> v10.16.3)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.16.1 (-> N/A)
lts/dubnium -> v10.16.3

The arrow (->) indicates the Node.js version being used in the current shell session. The current default version is set to v12.11.0. The default version is the one that will be active when a new session is started.

To change the currently active version to LTS, type the following:

$ npm use 10.16.3

You will see the following output:

Now using node v10.16.3 (npm v6.9.0)

To change the default Node.js version, run the following command:

$ nvm alias default 10.16.3

This version will be auto-selected when the next session starts. Type the following to switch to default version:

$ nvm use default

The good thing about NVM is that each installed Node.js version keeps track of its own packages. npm is also automatically installed to manage these packages. You can type the following to find the current npm version installed:

$ npm -v

You will see the following output:

6.9.0

Run the following command to learn more about all the options available with NPM:

$ nvm help

Installing Node.js and NPM from NodeSource

The 3rd and last way of installing Node.js and npm on Ubuntu 18.04 is by using the PPA (Personal Package Archive) repository maintained by NodeSource. Just like NVM, they provide up-to-date Node.js versions than the official Ubuntu repositories and allow you to select the specific Node.js version you want.

At the time of writing this article, NodeSource repository offers the following Node.js versions: v12.x, v11.x, v10.x, and v8.x.

Let us install the current LTS version of Node.js, v10.x, Dubnium.

First of all, enable the NodeSource repository by running the following curl command:

$ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -

The above command will add the PPA to your configuration file, create an apt sources repository file, install all required packages, and update the apt local package cache.

After executing the setup script from NodeSource, you can install Node.js and npm packages by typing the following:

$ sudo apt install -y nodejs

The nodejs package contains binaries for both Node.js and npm. So you do not need to separately install npm.

To verify that both Node.js and npm were installed successfully, type the following command:

$ node -v && npm -v

You will see the following output:

v10.16.3
6.9.0

To compile and install native addons from npm, you will need to install the build-essential package:

$ sudo apt install build-essential

Uninstalling Node.js and NPM

You can uninstall Node.js and npm by using apt or nvm, depending on the version you want to remove.

Uninstall apt Packages

To remove Node.js and npm packages installed from Ubuntu's official repositories or NodeSource PPA via apt package manager, execute the following command:

$ sudo apt remove nodejs npm

The above command will remove the packages and keep the configuration files. If you no longer need these configuration files at a later point, then run the following command:

$ sudo apt purge nodejs npm

This will uninstall both packages and remove the configuration files associated with them.

As the last step, you should run the following command to remove unused packages that were installed automatically with the removed packages:

$ sudo apt autoremove

Uninstall NVM Packages

To uninstall a Node.js version that you have added through NVM, first run the following command to determine whether the version you want to remove is the currently active version or not:

$ nvm current

If it is not the currently active version, just typing the following:

$ nvm uninstall node_version

Replace node_version with the version name you want to remove e.g. 8.10.0. This command will uninstall the targeted version of Node.js.

If you want to uninstall the current active Nod.js version, you should first deactivate it by typing:

$ nvm deactivate

Then remove it by running the following command:

$ nvm uninstall node_version

To uninstall NVM and remove all Node.js versions, just delete the $NVM_DIR (usually ~/.nvm) directory:

$ rm -rf "$NVM_DIR"

Edit ~/.bashrc (or other shell resource config) and remove the following lines:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[[ -r $NVM_DIR/bash_completion ]] && \. $NVM_DIR/bash_completion

Conclusion

That's all for folks for today. You have learned about three different ways to install Node.js and npm on your Ubuntu 18.04 server. The method that suits you the best depends on your requirements and the environment where you are going to run your Node.js code.

If you are looking to deploy Node.js applications on a production environment, then the best option is to install Node.js from the NodeSource repository.

Developers may tend to like NVM for installing Node.js as it offers more flexibility to quickly install and switch between different Node.js versions.

Read Next: How to Install MongoDB on Ubuntu 18.04

✌️ Like this article? Follow @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. Buy me a coffee (cost $3)

Need help to start a new Spring Boot or MEAN stack project? I am available for contract work. Hire me to accomplish your business goals with engineering and design. Let’s talk about your project: hi@attacomsian.com.