How to Install Node.js on Ubuntu 22.04: Step-by-Step Guide
Node.js is a popular, free, and open-source runtime environment designed for JavaScript. It plays an important role in the modern web landscape, providing an efficient platform for the development and deployment of web applications.
NPM (Node Package Manager) helps in managing the Node packages and libraries. This open-source repository has a variety of packages specifically designed for Node.js, saving developers from the need to build applications from the ground up. It's a standard practice to install and utilize npm in conjunction with Node.js for seamless development workflows. You can install Node.js on Windows, Linux, and Mac OS.
In this tutorial, we will demonstrate how to install Node.js on Ubuntu 22.04 using different methods. In addition, we will also explore how we can uninstall it from the Linux system.
Prerequisites
Running Ubuntu 22.04 server and appropriate user privileges (either as the root user or sudo with root privileges for administrative commands).
Methods to Install Node.js on Ubuntu 22.04
You can install Node.js on Ubuntu 22.04 using the different ways that are listed below:
1. Install Node.js on Ubuntu using Ubuntu apt repository
2. Install Node.js on Ubuntu using NVM (Node Version Manager)
3. Installing Node.js on Ubuntu from NodeSource
4. Ubuntu Install Node.js using Snap
Now, let’s discuss all Ubuntu install Node.js methods in detail with complete steps:
Method 01: Installing Node.js on Ubuntu Using the Ubuntu Apt Repository
This method allows you to install Node.js using the official Ubuntu apt repository. Installing Node.js using the apt repository is a simple and easy method, but it will install the older Node.js version on your Ubuntu system. If you are installing Node.js using the Ubuntu apt repository, follow the steps below:
Step 1: Update Ubuntu Apt Repositories
It is a best practice to always first update all Ubuntu system apt repositories before installing a new package or application on your system. To update and upgrade “apt” repositories, use the following commands:
$ sudo apt update$ sudo apt upgrade
Step 2: Install Node.js on Ubuntu 22.04
Once you update all repositories on your system, install Node.js on the Ubuntu 22.04 system using the following command:
After executing the command above, you need to confirm the user confirmation prompts, and then it will install Node.js and npm (Node Package Manager) on your system.
Step 3: Check the Node.js Version
Once Node.js is installed on the Ubuntu system, you can verify its installation using the following command:
$ nodejs --version
The above command will display the installed Node.js version on your terminal window. But, using this method, the older Node version will get installed on your machine. To check the installed npm version, use this command:
$ npm --version
Method 02: Install Node.js on Ubuntu Using NVM (Node Version Manager)
Using this method, you can install Node.js on Ubuntu 22.04 using NVM. Node Version Manager is a tool that allows you to manage the Node versions on your system. Using NVM, you can install different Node.js versions and can easily switch between these versions depending on the project requirements.
You can install NVM on all Linux distributions and Macs. It doesn’t support Windows OS. However, using a tool called “nvm-windows” provides the NVM experience in Windows. This management utility is used for the Node.js versions in the Windows system.
In this section, we will guide you on how to install Node.js using NVM on Linux system Ubuntu 22.04. To install the Node.js on Ubuntu using NVM, follow the steps below:
Step 1: Run the NVM Installer
NVM is a bash script that allows you to manage multiple Node versions on your system. Therefore, if you want to install a specific Node.js and NPM version, use the NVM bash script. To download the NVM installer on your system, use the following commands:
$ sudo apt install curl
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Or
$ wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
The above command will download and run the NVM installation bash script.
Step 2: Activate the NVM Script
After completing the installation, restart the terminal or execute the command below to load the NVM environment on your Ubuntu system:
$ source ~/.bashrc
Step 3: List NVM Node Versions
Now, you can install any Node.js version on your Ubuntu system using NVM. To list all available Node versions that you can install using NVM, use the following command:
$ nvm ls-remote
Step 4: Install Node.js on Ubuntu Using NVM
To install the latest LTS Node.js version using NVM, use this command:
$ nvm install --lts
You can also install any specific Node.js version on your Ubuntu system using NVM. To do that, select and specify the Node version that you want to install:
$ nvm install v20.9.0
Step 5: Verify Node.js Installation
Once the installation is complete, you can verify the installation. Check the Node.js and NPM version using the following commands respectively:
$ node --version
$ npm –version
Method 03: Install Node.js on Ubuntu from NodeSource
If you want to install the latest Node version on your Ubuntu system, it is preferred to install Node.js from the NodeSource repository. This repository offers the most recent releases of Node.js and allows you to enjoy the benefits of the latest improvements, bug fixes, and features of Node in your projects. Follow the steps below to install Node.js via the NodeSource repository:
Step 1: Add NodeSource Repository
First, add the NodeSource repository to your system using the following command:
$ curl -fsSL https://deb.nodesource.com/setup_<version>.x | sudo -E bash -
In the above command, replace the <version> with the Node.js version that you want to install on your system.
Step 2: Install Node.js on Ubuntu 22.04
Once you add the repository, you can install Node.js using the apt command:
$ sudo apt install nodejs
Now, display the installed Node version using the below command:
$ node --version
Method 04: Ubuntu Install Node.js using Snap
To install the stable Node.js version on your Ubuntu system, use:
$ sudo snap install node --classic
To install the latest Node.js version using Snap, use the following command:
$ sudo snap install node --channel=latest/edge --classic
Test the Node.js Installation on Ubuntu 22.04
To ensure that Node.js and NPM are functioning correctly, generate a JavaScript test program. For instance, create a file and paste the following code:
console.log(“Hello World”);
Save the file with the “.js” extension. Next, execute this file through the Node.js server. The terminal should display the following output:
$ node testprogram.js
How to Uninstall Node.js from Ubuntu 22.04?
If you don’t want to use Node.js on your Linux system, you can easily uninstall Node.js and NPM using the below-mentioned command:
$ sudo apt remove nodejs npm
To remove all configurations and files, use the following command:
$ sudo apt purge nodejs npm
The above commands will remove the Node.js and NPM packages from your Ubuntu system.
Conclusion
In this tutorial, we learned how to install Node.js on Ubuntu 22.04 distribution through the command line using different methods. Node.js serves as a runtime environment that you can easily set up on a Linux VPS server by following the provided steps, allowing the execution of JavaScript code on the V8 engine.
Additionally, NPM facilitates the straightforward installation of supportive Node packages or modules on your system. If you encounter any challenges during the installation process, feel free to share your feedback or seek assistance by leaving comments.