Node.js, a powerful JavaScript runtime built on Chrome’s V8 JavaScript engine, has revolutionized how we think about and build server-side applications. Its non-blocking, event-driven architecture makes it a top choice for scalable and efficient web applications. Here’s a quick look at what makes Node.js stand out:
- Asynchronous and Event-Driven: Node.js uses non-blocking I/O operations, making it lightweight and efficient for data-intensive real-time applications.
- Single Programming Language: Write client and server-side code in JavaScript, providing a unified development experience.
- Vast NPM Registry: Access a massive repository of open-source libraries, enhancing functionality and speeding up development.
- Cross-Platform: Develop applications that run seamlessly on various operating systems.
- Community and Corporate Support: Benefit from a vibrant community and backing from major corporations, ensuring constant updates and innovations.
With Node.js, developers gain a versatile tool for creating various web applications, from simple web servers to complex, real-time communication platforms. Let’s dive into the installation process.
Update Ubuntu Before Node.js Installation
Before installing Node.js, ensuring your Ubuntu system is up-to-date is crucial. This helps prevent potential conflicts and ensures you install the latest version of Node.js. Run the following command to update your system:
sudo apt update && sudo apt upgrade
Method 1: Install Node.js via APT Repository
Installing Node.js on Ubuntu 24.04, 22.04, or 20.04 directly from your Ubuntu APT repository is the first method. This will be satisfactory, depending on what you require from Node.js.
To install the Ubuntu default version, run the following command:
sudo apt install nodejs
Ensure Node.js is installed correctly after installation by verifying its version on your system. Run the following command to check the installed Node.js version:
node --version
You’ll see the installed Node.js version displayed in your terminal if the installation was successful. Now, you’re ready to start using Node.js on your Ubuntu system.
Method 2: Install Node.js via NodeSource
Install Required Initial Packages
First, ensure the following packages are installed below:
sudo apt install curl apt-transport-http ca-certificates
Import NodeSource APT Repository
Next, import the GPG key of the NodeSource repository. This action verifies the authenticity of the packages you’ll be installing. Use the command:
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/nodesource.gpg
Then, select the Node.js version that is appropriate for your Ubuntu system. For example, replace NODE_MAJOR=20
it with your desired version, like NODE_MAJOR=18
. Run this command:
NODE_MAJOR=20
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /usr/share/keyrings/nodesource.gpg
Choose from various Node.js versions such as:
NODE_MAJOR=16
NODE_MAJOR=18
NODE_MAJOR=20
NODE_MAJOR=21
Install Node.js via NodeSource APT Command
After adding the NodeSource repository, install Node.js using the command below. This method ensures you get the latest or a specific version of Node.js, offering an upgrade from the default repository:
sudo apt install nodejs
This command installs Node.js, and all its dependencies align with the version you chose earlier.
Conclude by verifying the Node.js installation:
node --version
The output version number confirms that Node.js has been successfully installed on your Ubuntu system from the NodeSource APT Repository.
Method 3: Install Node.js via Node Version Manager (NVM)
Another way to install Node.js is using the Node Version Manager (NVM). This method enables you to manage multiple Node.js versions on your system, making switching between versions for different projects easier.
Install NVM
To install NVM, run one of the following commands:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
or
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
These commands download and execute the NVM installation script. Once the installation is complete, you need to restart your terminal or run the following command to load NVM:
source ~/.bashrc
Install Node.js via NVM Command
With NVM installed, you can manage multiple Node.js versions on your system, making it convenient to switch between versions for different projects.
List Available Node.js Versions
Before installing a specific version of Node.js, check the available versions by running the following:
nvm ls-remote
This command displays a list of all available Node.js versions, helping you identify the version you want to install, such as the latest LTS release or a specific version number.
Install a Node.js Version
To install the desired version of Node.js, run the following command and replacing <version>
with the specific version you want to install:
nvm install <version>
For example, to install Node.js version v20.0.0, run:
nvm install 20.0.0
This command downloads and installs the specified version of Node.js.
Verify Node.js Installation
To check the installed version of Node.js, run the following:
node --version
This command displays your system’s installed version of Node.js, confirming the successful installation.
Switch Between Installed Node.js Versions via NVM
NVM allows you to switch between different Node.js versions easily. To switch between installed Node.js versions, use the following command, replacing <version>
with the version you want to switch to:
nvm use <version>
For example, to switch to Node.js version 18.16.0, run:
nvm use 18.16.0
This command sets the specified version as the active Node.js version for your current session. To make a specific Node.js version the default for new terminal sessions, use the command:
nvm alias default <version>
Replace <version>
with the desired version number. For example, to set Node.js version 18.16.0 as the default, run:
nvm alias default 18.16.0
Additional Tip: Learning the Methods to Remove Node.js
Remove Node.js Installed via Ubuntu Repository or NodeSource
If you installed Node.js from the Ubuntu repository or a PPA, you can uninstall it using the apt
program. Here’s a breakdown of the command you need to run:
sudo apt remove nodejs
This command will remove Node.js along with its associated configuration files. It will prompt you to confirm the removal, and after confirmation, it will proceed with the uninstallation.
NVM Command to Remove Node.js
If you installed Node.js using the Node Version Manager (NVM), follow these steps to uninstall it:
Check the Current Node.js Version via NVM Command
First, determine the currently installed version of Node.js by running the following command:
nvm current
This command displays the active Node.js version on your system.
Deactivate NVM via NVM Command
Before uninstalling the current version of Node.js, you need to deactivate NVM by running the following:
nvm deactivate
This command unloads the active Node.js version from your current session.
Remove Node.js Version via NVM Command
Now, run the following command to uninstall a specific version of Node.js installed using NVM, replacing <version>
with the version number you want to uninstall:
nvm uninstall <version>
For example, to uninstall Node.js version v20.0.0, run:
nvm uninstall 20.0.0
This command removes the specified Node.js version from your system, completing the uninstallation process.
Closing Thoughts
There you have it! We’ve journeyed through the different ways to install Node.js on Ubuntu Linux – 24.04, 22.04, or 20.04. Whether you chose the straightforward Ubuntu repository, the more up-to-date NodeSource APT, or the versatile NVM, you’re now equipped to handle your Node.js needs like a pro. Remember, the choice of method hinges on what works best for your project.