Node.js is essential for building server-side and networking applications. This platform runs efficiently on Windows, Linux, FreeBSD and macOS. Npm is the default package manager and is often tagged as the world’s largest software registry.
Install Nodejs on Ubuntu
In this guide, you can install Nodejs on Ubuntu in three different ways. These three ways include:
- Using apt to install Nodejs on Ubuntu
- Using apt with a PPA software repository
- Installing nvm to install and manage different versions of Nodejs on Ubuntu
Option 1: Install Node.js From NodeSource Repository
NodeSource, as a company, focuses on providing enterprise-grade Node support. This installation uses the Node.js repository, which will be used to install this version on Ubuntu. You can follow these steps to install Node Linux from NodeSource.
The first step is to enable the NodeSource repository using the curl command. If curl isn't installed, you can install it on your system using the following commands.
To Install Curl
sudo apt-get install curl -y
To Enable the Repository
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
The above command will add the signing key to your system. To create an apt source repository file you would need to install all the necessary packages and refresh the apt cache.
Install Node.js and Npm
Type in the following code to begin the installation for Node.js and npm.
sudo apt install nodejs
This package (nodejs ubuntu) will contain the binary files for both Node and npm.
Verify Node.js and Npm’s Installation
Check Npm’s Version
The output post installation for both modules will look like:
The version for Nodejs Ubuntu is v12.22.4 while npm's version is 6.14.14, which is the latest version available at the time of writing this guide.
There are more ways to install Nodejs Ubuntu and npm. To install them using Node Version Manager, follow the steps listed below.
Option 2: Install Node.js and Npm With NVM
NVM, more commonly known as Node Version Manager, is a bash script which works on an independent directory instead of the operating system level. This simply means that you can install multiple versions of Node.js without affecting your entire system.
Through NVM, you can control your system’s environment, and even use the newest versions of Node.js, while retaining and managing the previous releases. This is different from the apt utility, and there's a subtle difference in the versions compared with the apt versions.
Install Nvm Ubuntu
To download NVM using the command line, download and install the source code from GitHub’s page:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
This command will clone the repository from GitHub to the ~/.nvm directory. To use this, you first need to source your .bashrc file using the following command:
In the next step, you can check which version of Node is available within NVM.
The output will look like this:
This command will list a lot of available versions, so you can choose the latest release. In this case, the latest version available is 16.6.2, which can be installed using the command:
nvm install v16.6.2
The version name can be adjusted as per the most recent version available within NVM.
Post installation, view the different versions which were installed as a part of the previous installation:
The output will look like this:
The first line will show the currently active version, while some of the other lines show the named aliases and their versions. You can see aliases for the various LTS releases of Node. Basis these aliases, you can install a release as well.
For example, to install one such alias fermium, you can use the following command:
nvm install lts/fermium
Verify if the installation was successful or not by using the -v command.
The output will showcase the latest version which was installed.
Option 3: Installing Node.js Using NodeSource PPA
Another way to install Node.js is to install using PPA (personal package archive), which is maintained and updated by NodeSource. The benefit of using PPA is that it contains more versions of Node.js as compared to Ubuntu's repositories.
As a first step, you need to install PPA to get access to its packages. From the home directory, you can use the curl function retrieve the installation script for your version.
cd ~curl -sL https://deb.nodesource.com/setup_16.x -o nodesource_setup.sh
You can run the script with your favorite editor (like Nano). If you think everything in the script is as per your liking, you can run the commands further.
Exit the editor and run the script with your root access.
sudo bash nodesource_setup.sh
PPA will be added to your configuration list, while the local package cache is updated for you automatically. Install the Node.js package by typing in the following command:
sudo apt install nodejs
You can verify your installation by running node with the -v version flag as follows:
You can rest assured you don’t need to install npm Ubuntu separately, as this is a combined installation for node.js and npm.
Installing Node.js and NPM Successfully
No matter whichever method you adopt, there's always a way to successfully install Node.js and npm on your Ubuntu machine. This method will work for different versions of Ubuntu, although, this process was successfully implemented for Ubuntu 21.04. Depending upon your circumstances, you can pick and choose the option that works best for you.
As mentioned before, using the packaged version is the easiest of these methods; you can use the PPA installation method or the nvm method for more recent options. Either way, all three options will work for your Ubuntu Linux version.
Looking to install Node.js and npm on Windows? You're in luck; the process is even easier than installing them on Linux.