Quick Tip: Install Node.js on Ubuntu — SitePoint
Learn how to install Node.js and npm on Ubuntu using the default repository, NodeSourcerepository 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.jshas 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:
There are threedistinct methods for installing Node.js on Ubuntu:
Using the default Ubuntu repositories Employing the NodeSourcerepository 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 firstmethod 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.
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 .
Method 2: Installing Node.js and npm from NodeSource Repository
The secondmethod 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 NodeSourcerepository 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 NodeSourcerepository
To add the NodeSourcerepository 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 NodeSourcerepository 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.
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 NVMrepository’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 LTSversion 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.jsfrom 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 2described 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 threedifferent 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