Installing NodeJs on Ubuntu 20.04-24.04

NodeJs Ubuntu Tutorial Banner Image

Welcome to my guide to installing Node.js! Node.js is a powerful JavaScript runtime built on Chrome's V8 engine. It allows you to build fast, scalable network applications and run JavaScript code outside of a web browser. It comes bundled with npm (Node Package Manager), a massive ecosystem of open-source libraries that can be easily added to your projects, supercharging your development workflow.

This tutorial will walk you through installing Node.js on any recent version of Ubuntu, from 20.04 LTS to 24.04 LTS. The steps are also compatible with any Ubuntu-based distributions like Linux Mint, Pop!_OS, or Zorin OS. We'll be using the official NodeSource repository, which provides up-to-date versions of Node.js packaged specifically for Debian and Ubuntu systems.


Installing Node.js via NodeSource

NodeSource is a company dedicated to providing enterprise-grade Node.js support and is the trusted distributor for Node.js packages. Using their repository ensures you get the latest, most secure versions without complicated manual setups.

Let's get started.

Step 1: Install Curl

First, we need to ensure curl is installed on your system.

sudo apt-get install -y curl

Step 2: Add the NodeSource Repository

Next, we will run the official NodeSource script. This script will automatically configure the correct repository for your system. The command below is for installing Node.js v22.

As of writing, all Long-Term Support (LTS) versions from 16 to 22 are available. You can easily switch the version by changing the setup_22.x part of the script to your desired version (e.g., setup_20.x for Node.js v20).

curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -

Step 3: Install the Node.js Package

With the repository added, you can now install the Node.js package itself. This single command installs both nodejs and npm.

sudo apt-get install -y nodejs

Step 4: Verify the Installation

To confirm that Node.js has been installed correctly, you can check its version number with the following command:

node -v

This should print the version you chose to install, for example: v22.5.0. You can do the same for npm with npm -v.


Installing Development Tools

While the nodejs package is enough to run JavaScript applications, you'll often need to compile native addons from npm packages. To do this, you need to install a set of development tools, including the GCC compiler suite.

This command installs the build-essential package, which contains all the necessary compilers and libraries:

sudo apt install build-essential

With this, you are fully equipped to handle any Node.js project.


How to Uninstall Node.js

If you no longer need Node.js on your system, you can remove it with one simple command:

sudo apt remove nodejs

Conclusion

And that's it! You have now successfully installed Node.js and npm on your Ubuntu server, along with the build-essential package for compiling native addons. By using the NodeSource repository, you have established a clean and reliable setup that is easy to maintain and update.

Your system is now fully prepared for modern backend development. It's time to start your first project with npm init and bring your application ideas to life with the power of JavaScript on the server. Happy coding!