Node.js is a popular JavaScript runtime designed to execute JavaScript code on the server side. While it is commonly used for backend applications, it is also widely preferred for full-stack and frontend solutions. npm, the default package manager for Node.js, has the largest software registry in the world, making it a vital tool for developers. This guide will walk you through the process of installing the latest version of Node.js (currently version 21.x) and npm on various Linux distributions, step by step.
Installation Methods
You can install Node.js and npm using the following methods:
- Installing via NodeSource Repository
- Installing via NVM (Node Version Manager)
- Installing via Standard Distribution Repositories
1. Installing via NodeSource Repository
NodeSource is a reliable source for obtaining the latest stable versions of Node.js. By following the steps below, you can install Node.js from the NodeSource repository.
Step 1: Install Required Dependencies
To add a new repository, you need to install some necessary dependencies first. Open your terminal and run the following commands:
sudo apt update
sudo apt install curl gnupg
Step 2: Import the NodeSource GPG Key
Import the NodeSource GPG key using the following command:
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/nodesource.gpg
Step 3: Add NodeSource Repository
Now, you need to add the NodeSource repository to your system. This allows you to install the latest version of Node.js. Run the following command:
echo "deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_21.x $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/nodesource.list
Step 4: Install Node.js and npm
With the repository added, you can now install Node.js and npm using the following commands:
sudo apt update
sudo apt install nodejs
Step 5: Verify the Installation
To verify the installation, run the following commands to check the versions of Node.js and npm:
node --version
npm --version
If the installation is successful, you should see the version numbers of both Node.js and npm.
2. Installing via NVM (Node Version Manager)
NVM is a handy tool for managing multiple versions of Node.js. It provides flexibility for developers, allowing you to switch between different versions easily.
Step 1: Download and Install NVM
To install NVM, run the following command in your terminal:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Note: Do not use sudo
with this command, as it will install NVM for the root user, which is unnecessary.
Step 2: Enable NVM
After installing NVM, either close and reopen your terminal, or run the following command to enable NVM:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
Step 3: Verify NVM Installation
To confirm that NVM was installed correctly, run the following command:
nvm --version
This should display the version of NVM that is installed.
Step 4: Manage Node.js Versions
With NVM, you can easily install and manage different versions of Node.js. To install the latest version of Node.js, use the following command:
nvm install node
Step 5: List Installed Versions
To view the Node.js versions you have installed, use the following command:
nvm ls
This will list all the installed versions of Node.js, and you can easily switch between them using NVM.
3. Installing via Standard Distribution Repositories
Most Linux distributions include Node.js in their default repositories, though the versions available may not be the latest. However, this method is the simplest for those who just need a basic installation.
Ubuntu/Debian
For Ubuntu 20.04 and Debian 10, you can install Node.js and npm using the following commands:
sudo apt update
sudo apt install nodejs npm
After the installation is complete, verify the installed version with the following command:
nodejs --version
Fedora
On Fedora, you can easily install Node.js using the dnf
package manager. Run the following command:
sudo dnf module install nodejs:21
Arch Linux
For Arch Linux users, Node.js can be installed from the AUR (Arch User Repository). To do this, run the following command:
sudo pacman -S nodejs npm
This will install both Node.js and npm on your system.
Conclusion
In this guide, we’ve covered three different methods for installing Node.js and npm on various Linux distributions: through the NodeSource repository, via NVM, and using standard distribution repositories. Depending on your needs, you can choose the method that works best for you.
NodeSource is great for those who want the latest stable version, NVM offers flexibility for managing multiple versions, and standard repositories are the easiest but may not provide the most up-to-date releases. Whatever your choice, Node.js and npm are essential tools for any developer, and understanding how to install them correctly is the first step toward building robust JavaScript applications.