Quick Tip: Install Node.js on Ubuntu

Share this article

Install Node.js on Ubuntu

Node.js has revolutionized the way we build web applications by enabling developers to create scalable network applications using JavaScript on the server side. To harness the full potential of Node.js, you first need to install it on your system. In this guide, we’ll walk you through the process of installation on Ubuntu, a popular Linux distribution, using three different methods. We’ll also cover prerequisites, setting up your Node.js environment, and uninstalling Node.js if needed.

Table of Contents

Key Takeaways

  • 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.

Understanding Node.js

Node.js is a server-side JavaScript runtime environment that allows developers to build scalable web applications and APIs using the same codebase for both server and client sides. Its event-driven architecture and non-blocking input/output API make it perfect for real-time web applications. Installing Node.js on your web server enables you to leverage these features, key to developing state-of-the-art web applications.

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:

  1. You need a compatible Ubuntu version.
  2. You must have sudo privileges, which you can verify by executing the command sudo whoami and entering your password when prompted.
  3. A basic understanding of JavaScript and command-line usage is necessary.

There are three distinct methods for installing Node.js on Ubuntu:

  1. Using the default Ubuntu repositories
  2. Employing the NodeSource repository
  3. 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.

With Node.js and npm installed, you can now start running your JavaScript code and developing web applications.

Verify installation

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 npm -v in the terminal. If the version numbers of both Node.js and npm are displayed, they’ve been installed correctly, and you can start running your JavaScript code.

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.

Confirm installation

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 npm -v in the terminal. If the version numbers of both Node.js and npm are displayed, they have been installed successfully, and you can start running your JavaScript code.

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.

Install NVM

To install NVM, you need to follow these steps:

  1. Open a terminal by pressing Ctrl + Alt + T.
  2. 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.
  3. 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.

Summary

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

How do I install Node.js on Ubuntu 20.04 LTS?

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 node --version.

How can I verify if I have sudo privileges?

To verify sudo privileges, run the command sudo whoami and enter your password when prompted.

How can I switch between different Node.js versions installed using NVM?

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.

How do I uninstall Node.js from my Ubuntu system?

To uninstall Node.js from Ubuntu, you can use either the apt package manager or NVM.

Dianne PenaDianne Pena
View Author

Dianne is SitePoint's newsletter editor. She especiallly loves learning about JavaScript, CSS and frontend technologies.

nvm
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form