How to Install Node.js on Ubuntu 24.04

Node.js has a built-in package manager for managing libraries and modules. It’s event-driven, non-blocking I/O model efficiently handles asynchronous tasks. It’s open-source, cross-platform, compatible with Ubuntu 24.04, and lets you run JavaScript on servers as well as in browsers.

In this guide, we’ll demonstrate several ways of installing Node.js on Ubuntu 24.04.

How to Install Node.js on Ubuntu 24.04

Ubuntu 24.04 provides multiple ways to install Node.js depending on your needs, whether you’re a beginner, developer, or system administrator. Each method offers different benefits, such as ease of use, access to the latest versions, or long-term stability.

Let’s explore the most common and effective methods to install Node.js on your Ubuntu system.

Experience HostOnce’s Secure Shared Hosting!

HostOnce’s Shared Hosting offers fast, secure, and scalable NVMe SSD hosting with easy customization, strong uptime, and great value.

Method 1: Install Node Using Ubuntu Default Repository

Ubuntu 24.04 uses the default APT package manager to install applications. However, the version of Node.js available in the APT repository is often outdated.

sudo apt install nodejs
install nodejs apt

Verify it by checking the version:

node --version
verify version nodejs

You can update, install, and share packages using the package manager.

sudo apt install npm
install npm

Method 2: Install Node Using NVM 

Installing Node.js on Ubuntu 24.04 using the Node Version Manager. Download nvm and install it via GitHub before running the script:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
download nvm

Run the contents of the bash script to apply changes made by the node version manager:

source ~/.bashrc
source changes

Before installing Node.js via nvm, list all versions available.

nvm list-remote
list all available versions

You can see that there are several versions. As a demo, I’ll install version 24 the same as the previous method. Install the desired version by using the syntax given below:

nvm install <version-number>
install specific version

List all versions of Node.js that you have installed using nvm to verify the success of the installation.

nvm list
list all nodejs versions

Instead of using the version number, you can install the software by referring to the release name.

nvm install <version-name>
install using release name

The image shows that the version selected in the image is 20, which is the default one.

To change the current version, execute the following command:

nvm use <node.js-version>

Users can also switch the Node.js default version to a particular version via the following command:

nvm alias default
switch to default version

Method 3: Install Node.js Using Node Source 

Node.js version 21 is the most recent. To install it on Ubuntu 24.04, you can use its repository. To download the latest Node.js version, you need to execute the repository using a bash shell.

curl -fsSL https://deb.nodesource.com/setup_21.x | sudo -E bash
download nodejs

Now update the packages list for the default package installer and then execute the command below to install Node.js:

sudo apt-get install nodejs -y
install node js

After the installation, verify the version of Node.js:

node --version
verify installation

Good to Know Looking to register your own domain? Visit HostOnce Domains to find the perfect name for your website at unbeatable prices.

Method 4: Install Node.js Using Node Repository

Through Node.js source code, you can install any particular version. In this method, a mirror link for Node.js is added. 

First, there are some utilities that you need to install, but if they are already installed, then skip them:

sudo apt-get install -y ca-certificates curl gnupg
install ca certificates

Furthermore, create the keyring folder via the following command line:

sudo mkdir -p /etc/apt/keyrings
create keyring folder

The command below will add a new package repository for Node.js from Node Source Repository to the system package source list: 

curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg

The command below adds a package repository to the system for Node.js by adding it to the Node Source Repository:

NODE_MAJOR=21
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_${NODE_MAJOR}.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
add package repo to the system

Install Node.js after updating the package list by running:

sudo apt install nodejs -y
install node js

Method 5: Install Node.js on Ubuntu Using a Tar File

Downloading the compressed file of Node.js from its official website is the last way to install it on Ubuntu 24.04. You can download it in two different ways. The first is to directly download the file from its website. You can utilize the wget tool for downloading the file through the link.

wget https://nodejs.org/dist/v22.17.1/node-v22.17.1-linux-x64.tar.xz
download tar file

After downloading the file, extract it.

tar -xf node-v22.17.1-linux-x64.tar.xz
extract downloaded file

Before you continue, there may be some dependencies that are required to successfully install Node.js on Ubuntu. These include:

  • Zlib1g: It contains header files and libraries required for developing software that uses zlib.
  • libncursesw5: This package contains the development files required for ncurses with wide character support.
  • Build-essential Installs a few development tools, including the gcc compiler.
  • This package contains the development files required for ncurses without wide character support.
  • libffi: This package contains the development files of libffi, which is a foreign function interface that allows you to call functions written in another language.
  • libbz2-dev is a library that can be used to decompress and compress files.
  • libsqlite3: It contains a development file of the SQLite database engine.
  • Ibssl Developer: This package contains the development files of the OpenSSL Library.
sudo apt install zlib1g-dev libncursesw5-dev build-essential libncurses5-dev libffi-dev libbz2-dev libsqlite3-dev libssl-dev -y
install packages

Navigate to the extracted directory and move the generated files, and configure the compilation configuration.

sudo mv node-v22.17.1-linux-x64 /usr/local/nodejs
move generated files

Users can update the shell profile:

echo 'export PATH=/usr/local/nodejs/bin:$PATH' >> ~/.bashrc
export path

After installation, update the shell or restart the system to apply any changes:

source ~/.bashrc
apply changes

Then, verify the installation using the version command.

node --version
confirm installation

How to Remove Node.js from Ubuntu 24.04

When removing an app from Ubuntu 24.04 or any other Linux Distribution, it is important to remove all files and dependencies. They take up space and can also affect other applications. To remove Node.js if it is installed in Ubuntu 24.04, you will need to uninstall the Node source, the Ubuntu default Package Manager, and the Node repository.

sudo apt remove --autoremove nodejs
remove node js

Here, the source directory is the node repository; remove it via the command below:

sudo rm -r /etc/apt/sources.list.d/nodesource.list
remove source directory

If you installed Node.js through the Node Version Manager, then look first for the current version of Node.

nvm current
check current nvm version

Then, disable the active version of Node.js:

nvm deactivate
deactivate nvm

To check which versions are installed, you can list them all first.

nvm uninstall v20.19.4
uninstall nvm

Finally, unloading the node version manager to removing it from Ubuntu 24.04.

nvm unload
unload node version

Remove the following code lines from the bash shell configuration:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # 

Finally, reload the daemon to apply the changes:

sudo systemctl daemon-reload
reload daemon

If you installed Node.js using its source code or tar file, then navigate to the extracted directory of Node.js. Remove the extracted folder and the tar archive.

sudo rm -r node-v22.17.1-linux-x64
remove node js extracted directory and tar archive

Finally, reboot your system after removing Node.js from Ubuntu 24.04, regardless of how you installed or removed it.

Conclusion

Ubuntu 24.04 offers multiple ways to install Node.js. You can use APT for a quick setup or NVM for managing versions. Choose the method that fits your project and then verify the installation using the node -v or npm -v command. In this article, we explored both methods in detail to help you set up Node.js efficiently on your system.

FAQs

You can install a newer version of Node.js using NodeSource or NVM (Node Version Manager). For NodeSource, run curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - to add the repository, then install Node.js with sudo apt install nodejs.

It is highly recommended to use nvm. It makes it easy to switch between different Node.js releases.

Use the terminal to run node v and npmv. If installed correctly, both should return version numbers.

When using apt to install Node.js on Ubuntu 24.04, npm is usually included. If it’s not, you can install it separately with sudo apt install npm.

Author: Anees Asghar

Anees is a seasoned technical writer and WordPress expert with over 5 years of experience building and optimizing WordPress solutions. He also writes on Windows, Linux, MySQL, Java, and other technologies, creating practical content that helps developers and IT professionals.

Post a Comment

Your email address will not be published. Required fields are marked *