How to Install Node.js on Ubuntu 24.04

Anastasios Antoniadis

Node.js is a popular JavaScript runtime that allows developers to build scalable network applications. This guide will walk you through installing Node.js on Ubuntu 24.04 using different methods, including the official repositories, NodeSource, and NVM (Node Version Manager). Additionally, we will cover how to remove Node.js if needed.

Prerequisites

Before proceeding with the installation, ensure that you have:

  • A system running Ubuntu 24.04.
  • A user account with sudo privileges.
  • Access to a terminal or command-line interface.
  • A stable internet connection.

Method 1: Installing Node.js from Ubuntu’s Default Repository

Ubuntu 24.04 provides a stable version of Node.js in its official repositories. This method is simple and ensures a stable and tested version.

Step 1: Update the Package Index

Run the following command to update your package index:

sudo apt update && sudo apt upgrade -y

This ensures that you have the latest package listings and security updates.

Step 2: Install Node.js and npm

sudo apt install nodejs npm -y

This installs both Node.js and npm, which is the package manager for JavaScript.

Step 3: Verify the Installation

After installation, check the installed versions of Node.js and npm:

node -v
npm -v

If Node.js is installed correctly, the version numbers will be displayed.

Method 2: Installing Node.js via NodeSource (Latest Versions)

If you need the latest version of Node.js, NodeSource provides an updated repository.

Step 1: Install Required Dependencies

First, install curl (if not already installed):

sudo apt install curl -y

Step 2: Add the NodeSource Repository

To install a specific version, replace 18.x with the desired version (e.g., 20.x):

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

Step 3: Install Node.js

sudo apt install nodejs -y

This installs the latest stable version available in the NodeSource repository.

Step 4: Verify the Installation

Check the installed version:

node -v
npm -v

To check which Node.js version is installed:

node -v

To check the npm version:

npm -v

Method 3: Installing Node.js via NVM (Node Version Manager)

NVM allows you to manage multiple Node.js versions easily. This is the most flexible method for developers who need to work with different versions.

Step 1: Install NVM

Download and install NVM:

curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash

Then, reload your shell configuration:

source ~/.bashrc

Verify the installation:

command -v nvm

This should return nvm, indicating that it is successfully installed.

Step 2: Install Node.js using NVM

To install the latest LTS (Long-Term Support) version of Node.js:

nvm install --lts

To install a specific version:

nvm install 18.16.0  # Replace with desired version

Step 3: Set Default Node.js Version

After installing, you can set a default version to use:

nvm use --lts
nvm alias default lts

Check the installed version:

node -v
npm -v

Uninstalling Node.js

If you need to remove Node.js, follow the appropriate method based on how it was installed.

Removing Node.js Installed via apt

If you installed Node.js using apt, remove it with:

sudo apt remove nodejs npm -y
sudo apt autoremove -y

Removing Node.js Installed via NodeSource

If you installed Node.js using NodeSource, remove it as follows:

sudo apt remove nodejs -y
sudo rm -rf /etc/apt/sources.list.d/nodesource.list
sudo apt update

Removing Node.js Installed via NVM

To uninstall a specific Node.js version installed with NVM:

nvm uninstall 18.16.0  # Replace with the installed version

To remove NVM completely, delete its folder:

rm -rf ~/.nvm

Then, remove references in your shell configuration file (~/.bashrc, ~/.bash_profile, or ~/.zshrc).

Conclusion

In this guide, we explored three different methods to install Node.js on Ubuntu 24.04:

  • Using Ubuntu’s default repository (stable but might not be the latest version).
  • Using NodeSource for the latest version.
  • Using NVM to manage multiple Node.js versions.

We also covered how to uninstall Node.js when necessary. Depending on your requirements, you can choose the method that best suits your needs. If you need the latest version, NodeSource or NVM is recommended. If you prefer stability, the Ubuntu repository is a good choice.

Now you are ready to start developing with Node.js on Ubuntu 24.04!

Anastasios Antoniadis
Find me on
Latest posts by Anastasios Antoniadis (see all)

Leave a Comment