- Learn how to install Node.js and npm on Ubuntu using the default repository, NodeSource repository or NVM (Node Version Manager).
- Set up your environment by creating a project directory, initializing a project and running simple programs.
- Uninstall Node.js from Ubuntu with either apt package manager or NVM depending on installation method used.
Moreover, Node.js offers numerous benefits, such as scalability, rapid development rate, and a robust community. With an ever-growing number of packages and modules available, Node.js has become an indispensable tool for web developers worldwide.
Prerequisites for Installing Node.js on Ubuntu
Ensure you meet the prerequisites for installing Node.js on Ubuntu before initiating the installation process. Here are the prerequisites:
- You need a compatible Ubuntu version.
- You must have sudo privileges, which you can verify by executing the command
sudo whoamiand entering your password when prompted.
There are three distinct methods for installing Node.js on Ubuntu:
- Using the default Ubuntu repositories
- Employing the NodeSource repository
- Implementing NVM (Node Version Manager)
NVM is recommended for users who require frequent switching between Node.js versions, as it allows you to manage multiple versions easily.
Method 1: Installing Node.js and npm from Default Ubuntu Repositories
The first method to install Node.js and npm involves using the default Ubuntu repositories. This approach is straightforward and suitable for most users. However, it may provide an older version of Node.js compared to other methods.
The following sections will guide you through the steps to update the package lists, install Node.js and npm, and verify the installation.
Update package lists
Make sure to update the package lists in the terminal prior to installing Node.js and npm. This ensures that you get the latest JS versions installed on your system. To update the package lists on Ubuntu, execute the following command in the terminal:
sudo apt update. Once the package lists are updated, you can proceed with the installation process.
Install Node.js and npm
To install Node.js and npm using the apt package manager, execute the command
sudo apt install nodejs in the terminal. Press Y when prompted to confirm the installation. This command will install Node.js from the default Ubuntu repositories, making the process straightforward and secure.
Next, you need to install npm, the package manager for Node.js, which allows you to easily manage and share your code. To install npm, first install node by executing the command
sudo apt install npm.
Once Node.js and npm are installed, it’s crucial to confirm the success of the installation. To check if Node.js and npm are installed on the system, execute the commands
node -v and
Method 2: Installing Node.js and npm from NodeSource Repository
The second method involves installing Node.js and npm from the NodeSource repository. This method is suitable for users who want access to the latest version of Node.js and npm or need specific releases.
The NodeSource repository is a Personal Package Archive (PPA) that offers more versions of Node.js than the default Ubuntu repository. The following sections will guide you through the steps to add the NodeSource repository, install Node.js and npm, and confirm the installation.
Add the NodeSource repository
To add the NodeSource repository to your system, you can use curl or wget. First, open a terminal by pressing Ctrl + Alt + T. Then, execute the appropriate command for your Ubuntu version, which can be found on the NodeSource repository’s GitHub page. This will download and run the installation script, incorporating the NodeSource repository into your system.
Once the repository is added, you can proceed with the installation of Node.js and npm.
Install Node.js and npm
With the NodeSource repository added to your system, you can now install Node.js and npm. To do this, execute the command
sudo apt install nodejs in the terminal. Press Y when prompted to confirm the installation. This command will install Node.js and npm from the NodeSource repository, providing you with the latest version or a specific release, depending on your needs.
Upon installing Node.js and npm from the NodeSource repository, be sure to validate the installation. To do this, execute the commands
node -v and
Method 3: Installing Node.js and npm using NVM (Node Version Manager)
NVM is a versatile tool for Node.js version management, allowing you to install and manage multiple Node.js versions easily. This method is ideal for users who need to switch between Node.js versions often or want to test their applications with different versions. We have a complete guide to using NVM here.
The following sections will guide you through the steps to install NVM, select and install a Node.js version, and switch between installed versions.
To install NVM, you need to follow these steps:
- Open a terminal by pressing Ctrl + Alt + T.
- Download the NVM installation script using either curl or wget. You can find the appropriate command in the NVM repository’s README file on GitHub.
- Run the NVM installation script. This will incorporate NVM into your system.
After installing NVM, you need to update your shell’s configuration file (such as `.bashrc`) to load NVM automatically when opening a new terminal session. To do this, add the following lines to the end of your configuration file, or create a new file if it doesn’t exist:
export NVM_DIR=”$HOME/.nvm” [ -s “$NVM_DIR/nvm.sh” ] && . “$NVM_DIR/nvm.sh”
Save the file and restart your terminal. You can now use NVM to manage your Node.js versions.
Select and install Node.js version
With NVM installed, you can now select and install a specific Node.js version. To install the latest LTS version of Node.js, execute the command
nvm install –lts in the terminal.
To set the default version of Node.js for new terminal sessions, run the command
nvm alias default <version>. This ensures that the desired version is used whenever you open a new terminal session.
Switch between installed versions
NVM allows you to switch between different installed Node.js versions easily. To switch to a specific Node.js version, execute the command
nvm use <version> in the terminal.
For example, to switch to version 14.19.0, type
nvm use 14.19.0. This command will activate the specified version, allowing you to test your applications with different Node.js versions or use the features of a specific version.
Uninstalling Node.js from Ubuntu
If you ever need to remove Node.js from your Ubuntu system, the process will depend on the installation method you used. If you installed Node.js using the apt package manager (using Method 1 or Method 2 described above), you can uninstall it by executing the command
sudo apt remove nodejs in the terminal. This command will effectively remove the Node.js package from your system.
If you installed Node.js using NVM (Method 3), you can uninstall a specific version by executing the command
nvm uninstall <version>. Before uninstalling the currently active version, you need to deactivate it by executing the command
nvm deactivate. This will allow you to uninstall the active version without any issues.
In this guide, we covered three different methods for installing Node.js on Ubuntu: using the default Ubuntu repositories, the NodeSource repository, and NVM (Node Version Manager). Each method has its advantages and is suitable for different scenarios.
FAQs About Installing Node on Ubuntu
To install Node.js 20 on Ubuntu 20.04 LTS, run the following commands in a terminal window:
sudo apt update,
sudo apt upgrade,
sudo apt install -y curl,
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -, and
sudo apt install -y nodejs. Verify the version with
To verify sudo privileges, run the command
sudo whoami and enter your password when prompted.
To switch between Node.js versions installed using NVM, simply execute the command
nvm use <version> in the terminal. See our guide to using NVM here.
To uninstall Node.js from Ubuntu, you can use either the apt package manager or NVM.