Node.js is an open-source, cross-platform runtime environment that allows executing JavaScript code on the server-side, enabling users to run JavaScript code on their computers as standalone and free applications.
Node.js is used to develop the backend of server-side applications. Additionally, it is a popular full-stack and front-end solution. npm is the default package manager for Node.js. In the following article, Mytour will guide you on how to install Node.js on Ubuntu 18.04 as well as how to install npm.
1. Installing Node.js on Ubuntu 18.04
If you only need to deploy Node.js applications, the simplest way is to install Node.js packages via apt from the default Ubuntu repository or the NodeSource repository if you need the latest versions of Node.js and npm.
In this article, Mytour will guide you on how to install Node.js on Ubuntu 18.04. The steps are similar for other distributions based on Ubuntu, including Linux Mint and Elementary OS.
2. Requirements
Before starting the steps, ensure you have logged in under a user with sudo privileges.
3. Installing Node.js and npm from the Ubuntu repository
Node.js and npm packages are available in the Ubuntu 18.04 distribution repository. At the time of writing, Node.js and npm version 8.10.0 are the latest versions available in the distribution repository.
First, run the command below to update the list of packages:
sudo apt update
Install Node.js using the apt package manager:
sudo apt install nodejs npm
The Node.js file is executed from the Ubuntu repository named nodejs, not node, to avoid conflicts with other packages.
To verify the installation of Node.js, use the following command:
nodejs --version
The output appears as:
v8.10.0.
To download npm packages, we need to install npm, the Node.js package manager. To do this, run the following command:
sudo apt install npm
To check if npm is installed, use the command:
npm --version
The output is:
v3.5.2
4. Installing Node.js from NodeSource
NodeSource is a company primarily providing enterprise-grade Node support and maintains a repository containing the latest versions of Node.js.
To install Node.js and npm from the NodeSource repository, follow these steps:
Step 1: Activate the NodeSource repository using the curl command below:
curl -sL https://deb.nodesource.com/setup_10.x | sudo bash -
This command adds the NodeSource signing key to your system, creates a repository source file for apt, installs all necessary packages, and refreshes the apt cache.
The current LTS version of Node.js is v10.x, Carbon. If you need to install version 8.x, simply change setup_10.x to setup_8.x.
Step 2: After activating the NodeSource repository, the next step is to install Node.js and npm using the following command:
sudo apt install nodejs
The nodejs package contains both the node binary and npm.
Step 3: To verify the installed versions of Node.js and npm, we will print the versions of Node.js and npm:
node --version
The output appears as: v10.13.0
npm --version
The output appears as: v6.4.1
Installation of Development Tools
To compile and install native addons from npm, we need to install development tools.
Run the command below to install all necessary packages:
sudo apt install gcc g++ make
5. Uninstalling Node.js Installation
Suppose for whatever reason you want to uninstall the Node.js and npm packages. To do this, use the following command:
sudo apt remove nodejs npm
This article by Mytour just guided you on how to install Node.js on Ubuntu 18.04. Additionally, readers can explore other articles on Mytour to learn more about how to create Node.js applications, such as RESTful API in Node.js, for example.