How To Install Node.js on Ubuntu

How To Install Node.js on Ubuntu

Provides all the steps required to install Node.js on Ubuntu 18.04 LTS

April 06, 2019

Node.js is an open source, cross-platform, JavaScript runtime environment that executes programs written in JavaScript programming language. It is popular to write server-side programs using JavaScript and it has an in-built web server to manage the websites built using JavaScript as the backend programming language. The package manager used for JavaScript programming language is called as npm. It's the default package manager used by Node.js JavaScript runtime environment.

This tutorial explains all the steps required to install Node.js(nodejs or node js) and npm on the popular Linux distribution i.e. Ubuntu. It provides all the steps required for Ubuntu 18.04 LTS. The steps should be the same for other versions of Ubuntu and Linux systems. You can also follow How To Install Node.js On Windows to install it on Windows.

Install Directly

We can install the default version available at Ubuntu repositories using the below-mentioned commands. Both the installation commands i.e. nodejs and npm will show a message to confirm the installation. Press y to confirm and complete the installation. If you are installing npm on fresh Ubuntu installation, the command to install npm will take some time since it's dependent on several packages.

# Refresh the packages index
sudo apt-get update

# Install Node.js
sudo apt-get install nodejs

# Verify Installation
node --version

# Install npm
sudo apt-get install npm

# Verify Installation
npm --version

At the time of writing this tutorial, these commands install the v8.10.0 of Node.js and 3.5.2 of npm which is available at the official repositories of Ubuntu 18.04 LTS.

Install Using PPA

In several situations, we either need multiple versions of Node.js on the same system or need the most recent version to keep the system updated for active development or as part of project needs. In such situations, we can't go with the default installation as we did in the previous section.

We can use the Personal Package Archive (PPA) to install the most recent version of Node.js. Make sure that you are using the PPA distributed by NodeSource. You can follow the below mentioned commands to add the PPA.

# Install CURL if not done yet
sudo apt-get install curl

# Get the PPA - Replace the version based on actual needs
curl -sL https://deb.nodesource.com/setup_11.x -o nodesource_11.sh

# Check the file contents to make sure that everything seems fine

# Now add the PPA to system repos
sudo bash nodesource_11.sh


# It shows below mentioned messages after successully adding the PPA

## Run `sudo apt-get install -y nodejs` to install Node.js 11.x and npm
## You may also need development tools to build native addons:
sudo apt-get install gcc g++ make
## To install the Yarn package manager, run:
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn

Now we will install Node.js as we did in the previous section using the below-mentioned command. The only difference is that it will also install the associated npm.

# Refresh the packages index
sudo apt-get update

# Install Node.js as regular installation
sudo apt-get install nodejs

# Verify Installation
node --version

npm --version

The above command will remove the previous installation if there is any and install the newer version. While writing this tutorial, the v11.13.0 of Node.js and 6.7.0 of npm were installed.

There are several npm packages which need to compile the code from source. We also need to install the build-essential package if it's not installed. It can be installed using the below-mentioned command.

# Install build-eseentials package
sudo apt-get install build-essential

We also need to clean the system after installing Node.js and npm using below mentioned commands.

# Autoremove
sudo apt-get autoremove

# Autoclean
sudo apt-get autoclean

These are the basic commands required to install Node.js and npm on Ubuntu.

Write a Comment
Click the captcha image to get new code.
Discussion Forum by DISQUS