How to Install Node.js on Ubuntu 24.04


Node.js is a JavaScript framework enabling the developers to execute the JS code on the server side. Node.js is built on the same processing engine as of the Chrome (V8 engine). Thus, providing a fast and scalable code’s execution.

An asynchronous model (suitable for real-time and high-traffic apps), large community support, and unified language features of Node.js outperform its competitors.

Node is open-source and thus liked by the developers working on Ubuntu (or other Linux distributions). With the latest release of Ubuntu 24.04 (Noble Numbat), this post aims to address the installation methods of Node.js on Ubuntu 24.04:

Outline:

How to Install Node.js on Ubuntu 24.04

Node.js is available on default repositories of Noble Numbat, NodeSource repository, source code based, and the binary executable. You may not get the latest versions with all these methods. However, they are authentic/safe enough to use on your new Ubuntu 24.04

Default Repositories

First, update the core libraries to update the packages list in the default repositories:

sudo apt update

Once done, install Node.js using the command:

sudo apt install nodejs

To verify which node version is installed, use the below command:

node --version

The version is “18.19.1”.

Node.js Deb Repository

The Node.js Deb repository (offered by Nodesource) contains the Node.js latest version. The repository needs to be added to the system, i.e., there are two ways to get Node.js from the Node.js Deb repository, i.e., manually add the repo and install the node or use an automated script.

Before starting the methods, let’s install the prerequisites:

Prerequisites: Install curl, gnupg, ca-certificates, apt-transport-https

The curl to download the script, gnupg for key/repository authentication, cacertificates for the authenticity of the source, and apttransporthttps to allow https sources for the apt:

sudo apt install ca-certificates apt-transport-https gnupg curl

Let’s start with the first sub-method:

  • Method 2.1: Manually Add the Repository and Install Node.js

Add the GPG key of the node source repository:

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

Now, add the repository:

echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_21.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list

Note: You can add the specific version by replacing the required version with “node_21.x”.

Update and then install the Node.js:

sudo apt update
sudo apt install nodejs

When verified, the version installed using this method is “21.6.2”:

Let’s get into the second variant of the same method.

  • Method 2.2: Install Node.js Using an Automated Script

The following automated script automatically sets up the environment and configures the repository:

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

The command automatically installs the prerequisites and configures the repository. You can specify any other version in place of “setup_21.x” in the above command:

After that, you can use the “apt install” command to install the Node.js:

sudo apt install nodejs

Note: Both variants of this method install the same version, i.e., 21.6.2.

Source Code

Source codes of packages are converted into Linux-based executables. While building a node from the source code, you need to install a few prerequisites that are stated below:

Prerequisites: Install Compilers, Libraries

The source code based installation requires the compiler and the additional libraries to compile the code and build the source code:

sudo apt install build-essential libbz2-dev libssl-dev libncursesw5-dev zlib1g-dev libreadline-dev libffi-dev libncurses5-dev libgdbm-dev libsqlite3-dev

Now, you can proceed with an error-free installation as follows.

Download the source code package of the node from the official Downloads Page:

wget https://nodejs.org/dist/v20.11.1/node-v20.11.1.tar.gz

Extract the downloaded source code package:

tar -xf <path-of-tar.gz-File>

Navigate to the extracted directory and run the configure script. It looks for the dependencies/libraries and ensures the required compilers/libraries are available before the compilation phase:

cd <Extracted-Directory>
./configure

Compile the code and convert it into binary executables, i.e., the “j” flag when used with a number boosts up the compilation process:

make -j 4

Once the source code is built, use the following command to put the files in the system-defined directories:

sudo make install

Let’s check the version installed via this method:

Binary Executable

Node.js is available in a binary executable form on the Downloads Page. First, download the latest executable:

wget https://nodejs.org/dist/v20.11.1/node-v20.11.1-linux-x64.tar.xz

Extract the tar file:

tar -xf <tar.xz-File-Path>

Navigate into the extracted directory:

cd <Extracted-Directory>

Use the ls command to list down the content and then navigate to the bin folder where you can see the node’s executable. Execute it and you will get the node version and its command line support as shown below:

These are the possible and direct methods to get the Node.js.

Do you know, you can install the Node.js version as per your requirement? Let’s dig into it:

Alternative Approach: How to Install/Manage Node Using NVM

NVM (Node Version Manager), as its name implies, manages node versions on Ubuntu (or other Linux systems). You can install and manage different versions using this manager. NVM fetches the binary executables available on the node website and installs it, i.e., you can get any version (older as well as newer).

Install NVM

Let’s install NVM first before using:

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash

Refresh/Restart the terminal session to start using NVM:

Install Node.js Using NVM

NVM provides extensive support to access and install Node.js on Ubuntu. Before you get into installation methods, list the Node.js versions using the command:

nvm list-remote

Choose (note down) the version you want to install and use the below command to install it:

nvm install <version>

Similarly, you can use the code name of the release as well. For instance, to install the LTS of the iron release, use the command:

nvm install lts/iron

Some generic NVM installation commands are provided below:

nvm install stable #Latest stable
nvm install --lts #Current LTS
nvm install node #Current Latest (not stable)

How to Completely Remove/Uninstall Node.js From Ubuntu 24.04

You may want to remove/uninstall the current installation of Node.js to either get the latest one or move to another required version. For that, you have to follow the complete removal method (depending on your installation method):

An apt-based Node.js

If you have installed the Node.js using Method-1 or Method-2, you can remove it using the command:

sudo apt autoremove nodejs --purge

Source-based Installation

To remove a source-based installation, you need to navigate to the extracted directory and execute the following uninstall script:

sudo make uninstall

Note: Once the uninstall script is run, you can remove the extracted directory too, using the “sudo rm -r <path-to-directory>” command.

NVM-based Installation

If you are using the Node.js installed from the NVM, you need to uninstall it using the command:

nvm uninstall <node-version>

Ensure that the version you are installing is not currently in use by NVM, otherwise, it will throw the following error:

Moreover, if you are planning to remove NVM too, you need to deactivate it first and then unload it. Here are the commands:

nvm deactivate
nvm unload

Note: The deactivate command removes the nvm’s executable from the PATH.

That’s how you can install Node.js on Ubuntu 24.04 Noble Numbat.

Bottom Line

Ubuntu 24.04 has the default repositories support to get Node.js with the command “sudo apt install nodejs”. However, it can be installed from the Node.js deb repository, the source code, or the binary executable.

The Node.js deb repository provides the latest version, i.e., 21.6.2 whereas the NVM offers tens of options to get the specific/latest node version. This post has listed the possible approaches to get Nodejs on Ubuntu 24.04, Noble Numbat.

Print Friendly, PDF & Email
Categories