Ubuntu

How to install Node.js on Ubuntu 24.04

nodejs in ubuntu 24.04

A runtime environment is used to run an application or execute certain tasks on a server or web browser. This acts as a small operating system for a program, as it contains all the necessary functionality to run. Usually, the run time environment is based on JavaScript language and Node.js is the JavaScript runtime environment. This allows the JavaScript code on servers but is different from the traditional JavaScript in the browsers.

Node.js enables the user to develop or create full-stack applications which include its back-end and front-end only in JavaScript. Since Node.js is open-source and supports cross-platform, it is compatible with the newly released Ubuntu 24.04, to install it on Ubuntu 24.04 there are multiple ways which will be discussed in this guide.

Outline:

How to Install Node.js on Ubuntu 24.04

Node.js is an event-driven and non-blocking I/O model that allows its users to handle asynchronous tasks efficiently. Furthermore, it comes with a package manager, which simplifies managing the external libraries and modules. To install Node.js on Ubuntu 24.04 there are primarily five ways which are:

1: Through Ubuntu Default Repository

The default method to install any application on Ubuntu 24.04 is by using its default package installer but sometimes the apt package installer has an older version in its repository. The same is the case for Node.js:

sudo apt install nodejs

Verify it by checking the version:

node --version

If you see in the screenshot for Node.js installation the compiler suggests installing the package manager as it will help in updating, installing, and sharing packages:

sudo apt install npm

2: Through Node Version Manager

If you are looking forward to installing multiple versions of Node.js and npm then use the node version manager to install Node.js on Ubuntu 24.04. First, you need to install the node version manager using GitHub, to download nvm and run its script via bash shell execute:

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

Now execute the contents of the bash script file to apply the changes made by the node version manager:

source ~/.bashrc

Now you can verify the node version manager installation by looking for its version. Before installing Node.js via nvm, first, list all the available versions:

nvm list-remote

Here as you can see there is a huge list of versions available so for a demonstration I am going to install version 18 same as was installed in the previous method:

To install the desired version just use the below-given syntax:

nvm install <version-number>

Once the installation is complete now verify it by listing the node.js versions installed via nvm:

nvm list

Here is the list you can see that each version has its name so instead of writing the version number you can install any node.js version by using its name:

nvm install <version-name>

Now in the image below the selected version that is currently active is 20 however the default version is still 18:

You can change the versions if there are any compatibility or any other similar issues, to change the current version execute:

nvm use <node.js-version>

Further, to change the default version of Node.js to the desired one execute:

nvm alias default

3: Through Node Source

In the previous method, you can observe that the latest version available in the node version manager was 20 but currently, the latest version is 21. To install the latest version of node.js on Ubuntu 24.04 you can use its repository from GitHub. To download the node.js repository for the latest version and to execute it through bash shell execute:

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

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

sudo apt-get install nodejs -y

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

node --version

4: Through Node Repository

Installing an application through its source code or by manually configuring the repository is a slightly difficult process. However, using this method you can install any desired version as in this method a mirror link for the node.js will be added. First, there are some utilities that you need to install, but if they are already installed then skip it:

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

Next, if your keyring directory is missing then to create execute the below command, again this is an optional step:

sudo mkdir -p /etc/apt/keyrings

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

Now set the version you want for node.js by making it an environmental variable, next get the GPG key for the node.js and then add the node source URL which as result will be added to the sources list. The command below sets up a new package repository for node.js from Node Source Repository and adds it to the system package source list:

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

Now update the packages list of apt and then install node.js by executing:

sudo apt install nodejs -y

5: Through Tar File

The last method for installing Node.js on Ubuntu 24.04 is by downloading its compressed file from the download section of its official website. There are two ways to download it one is by downloading its directly from the website:

Or using the wget utility along with the download link:

wget https://nodejs.org/dist/v21.7.1/node-v21.7.1.tar.gz

Once the file is downloaded extract it:

tar xzf node-v21.7.1.tar.gz

Now before moving forward some dependencies might be needed to install Node.js successfully on Ubnutu which include:

  • Zlib1g-dev: This package contains the header files and libraries necessary for developing software that use zlib.
  • libncursesw5-dev: This package provides the necessary development files for ncurses library with wide character support.
  • build-essential: This package installs some development tools including gcc compiler and make utility.
  • libncurses5-dev: This package provides the necessary development files for ncurses library without wide character support
  • libffi-dev: This package contains development files for libffi which provides a foreign function interface for calling functions written in other languages.
  • libbz2-dev: This is a compression library used to compress and decompress files
  • libsqlite3-dev: This package provides a development file for SQLite database engine.
  • ibssl-dev: This package comes with the development files for the OpenSSL library.
sudo apt install zlib1g-dev libncursesw5-dev build-essential libncurses5-dev libffi-dev libbz2-dev libsqlite3-dev libssl-dev -y

Now navigate to the extracted file directory and execute the ./configure file to check for dependencies make file generation and configure compilation configuration:

cd node-v21.7.1

./configure

Now compile the configuration files using the make command. Here the process takes a lot of time so I have added the instruction to run 4 parallel tasks as this would lessen the compilation time:

make -j 4

Now install Node.js by installing the make file created as a result of compilation:

sudo make install

After the installation reboot the system to apply the changes and then verify the installation by executing the version command:

node --version

How to Remove Node.js from Ubuntu 24.04

While removing an application from Ubuntu 24.04 or any other Linux distribution it is necessary to remove all the files and unnecessary dependencies. This is because they carry up some space and also might affect other installed applications. So to remove Node.js from Ubuntu 24.04 if installed though, node source, Ubuntu default package manager, and node repository then execute:

sudo apt remove --autoremove nodejs

Next in the case of the node repository or node source delete the node source file in the sources list directory:

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

To uninstall Node.js if installed through node version manager then first look for the current node version:

nvm current

Next, deactivate the current active node.js version:

nvm deactivate

Afterward, uninstall all the installed versions, to check the installed versions you can first list all the installed versions:

nvm uninstall v20.11.1

nvm uninstall v18.19.1

To remove node version manager from Ubuntu 24.04 first you need to deactivate it by unloading it:

nvm unload

Then remove the following lines of code from the bash shell configuration file:

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" #

To apply the changes reload the daemon service:

sudo systemctl daemon-reload

Similarly, if you have installed Node.js via its tar file or source code then in that case navigate to its extracted directory and uninstall the make file for Node.js:

sudo make uninstall

After that remove the tar file and the extractor folder directory:

sudo rm -r node-v21.7.1

sudo rm node-v21.7.1.tar.gz

Note: Perform a system reboot once you have removed Node.js from Ubuntu 24.04 regardless of the method followed for installation or removal.

Conclusion

To install Node.js on Ubuntu 24.04 there are five different ways: Through Ubuntu Default Repository Node Version Manager, Node source repository, Node repository, and through tar file. Among these, the recommended way to install Node.js is by using the NVM. This is because one can install multiple versions of it which can be switched depending on the need.

Similar Posts