A runtime environment is crucial for running applications or executing tasks on a server or within a web browser. Think of it as a mini-OS for your program, providing all the necessary functions to operate. Node.js is a popular JavaScript runtime environment, enabling JavaScript code execution on servers, which differs from traditional browser-based JavaScript.
Node.js empowers developers to build full-stack applications, encompassing both back-end and front-end, using only JavaScript. Being open-source and cross-platform, Node.js is compatible with Ubuntu 24.04. This guide explores several methods to install it on Ubuntu 24.04.
Ways to Install Node.js on Ubuntu 24.04
- Through Node Version Manager
- Through Node Source
- Through Node Repository
- Through Tar file
- Through Ubuntu Default Repository
How to Install Node.js on Ubuntu 24.04
Node.js uses an event-driven, non-blocking I/O model for efficient asynchronous task handling. It also includes a package manager, simplifying the management of external libraries and modules. Primarily, there are five methods to install Node.js on Ubuntu 24.04.
Through Node Version Manager
If you need to manage multiple Node.js and npm versions, using the Node Version Manager (NVM) is the way to go.
Step 1: Install NVM from GitHub by downloading and running the installation script via bash
.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Step 2: Apply the changes made by NVM by executing the contents of the .bashrc
script file.
source ~/.bashrc
Step 3: Verify the NVM installation by checking its version. Before installing Node.js, list all available versions.
nvm list-remote
Step 4: Install your desired Node.js version using the following syntax, replacing <version-number>
with the specific version.
nvm install <version-number>
For example:
nvm install 18
Step 5: Verify the installation by listing the Node.js versions installed via NVM.
nvm list
Step 6: Install any Node.js version by using its name.
nvm install <version-name>
Step 7: Switch between installed versions. To change the currently active version, execute:
nvm use <node.js-version>
Step 8: Change the default Node.js version.
nvm alias default <node.js-version>
For example:
nvm alias default 20
Through Node Source
To install the latest version of Node.js on Ubuntu 24.04, you can use its repository from GitHub.
Step 1: Download the Node.js repository for the latest version and execute it through bash
.
curl -fsSL https://deb.nodesource.com/setup_21.x | sudo -E bash
Step 2: Update the packages list and install Node.js.
sudo apt-get install nodejs -y
Step 3: Verify the Node.js version.
node --version
Through Node Repository
Installing via source code or manually configuring the repository allows you to install any desired version by adding a mirror link for Node.js.
Step 1: Install necessary utilities (skip if already installed).
sudo apt-get install -y ca-certificates curl gnupg
Step 2: Create the keyring directory if missing (optional).
sudo mkdir -p /etc/apt/keyrings
Step 3: Import the NodeSource repository GPG key:
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
Step 4: Set the Node.js version as an environmental variable. The following adds a Node.js package repository from Node Source Repository 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
Step 5: Update the apt
packages list and install Node.js.
sudo apt install nodejs -y
Through Tar File
You can also install Node.js by downloading its compressed file from the official website.
Step 1: Download the file directly from the website or using wget
with the download link.
wget https://nodejs.org/dist/v21.7.1/node-v21.7.1.tar.gz
Step 2: Extract the downloaded file.
tar xzf node-v21.7.1.tar.gz
Step 3: Install the dependencies required to install Node.js.
sudo apt install zlib1g-dev libncursesw5-dev build-essential libncurses5-dev libffi-dev libbz2-dev libsqlite3-dev libssl-dev -y
Step 4: Navigate to the extracted directory and configure the installation.
cd node-v21.7.1
./configure
Step 5: Compile the configuration files. To reduce the compilation time, run 4 parallel tasks.
make -j 4
Step 6: Install Node.js.
sudo make install
Step 7: Reboot the system and verify the installation.
node --version
Through Ubuntu Default Repository
The standard method for installing applications on Ubuntu 24.04 involves using its default package installer.
Step 1: Install Node.js using apt
.
sudo apt install nodejs
Step 2: Verify the installation by checking the version.
node --version
Step 3: Install the package manager, npm
.
sudo apt install npm
How to Remove Node.js from Ubuntu 24.04
When removing an application from Ubuntu 24.04, it’s important to remove all associated files and unnecessary dependencies.
Step 1: Remove Node.js if installed via Node source, Ubuntu default package manager, or Node repository.
sudo apt remove --autoremove nodejs
Step 2: Delete the Node source file in the sources list directory (if applicable).
sudo rm -r /etc/apt/sources.list.d/nodesource.list
Step 3: Uninstall Node.js if installed through Node Version Manager. First, find the current Node version.
nvm current
Step 4: Deactivate the current Node.js version.
nvm deactivate
Step 5: Uninstall all installed versions. List the installed versions first.
nvm uninstall v20.11.1
nvm uninstall v18.19.1
Step 6: Remove Node Version Manager by unloading it.
nvm unload
Step 7: Remove the following lines 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" # This loads nvm bash_completion
Step 8: Reload the daemon service to apply the changes.
sudo systemctl daemon-reload
Step 9: If you installed Node.js via a tar file or source code, navigate to its extracted directory and uninstall the make file.
sudo make uninstall
Step 10: Remove the tar file and the extracted folder directory.
sudo rm -r node-v21.7.1
sudo rm node-v21.7.1.tar.gz
Note: After removing Node.js, reboot the system to apply the changes.
There are several ways to install Node.js on Ubuntu 24.04, but using NVM is the most flexible because it allows you to install and switch between multiple versions depending on your needs.