How to Install Node.js on Ubuntu? The Latest Version

how to install node.js on ubuntu

Node.js is an essential tool for developers working with JavaScript on the server-side. If you’re using Ubuntu, installing Node.js is straightforward and simple. This step-by-step guide will show you how to install Node.js on Ubuntu and set up your environment for development.

Why Install Node.js on Ubuntu?

Node.js is a powerful JavaScript runtime that allows you to run JavaScript on the server side. With its event-driven, non-blocking I/O model, Node.js is ideal for building scalable applications and APIs. By installing Node.js on Ubuntu, you can easily work with backend applications, real-time services, and full-stack JavaScript development.

Why Ubuntu?

Ubuntu is one of the most popular Linux distributions for developers, offering stability, security, and a rich ecosystem of development tools. The process of installing Node.js on Ubuntu is quick and painless, especially with the availability of the NodeSource repository, which provides the latest versions of Node.js.

Step-by-Step Guide to Install Node.js on Ubuntu

There are several ways to install Node.js on Ubuntu. The most common methods involve using the NodeSource repository or installing it through Ubuntu’s default package manager, apt. Below, we’ll go through both methods.

Method 1: Installing Node.js via NodeSource Repository

The NodeSource repository provides the latest stable LTS (Long Term Support) and current versions of Node.js, ensuring you can work with the newest features or rely on the more stable versions.

Step 1: Update Your System

Before installing any software, it’s a good idea to update your system’s package index to make sure all repositories are up to date. Open a terminal and run:

bashCopysudo apt update

Step 2: Install curl (if not already installed)

If curl is not installed on your system, install it with the following command:

bashCopysudo apt install curl

Step 3: Add the NodeSource Repository

The next step is to add the NodeSource repository to your system. You can do this with the following command, which will download and install the setup script for the latest LTS version:

bashCopycurl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -

For the latest current version of Node.js, use setup_current.x instead of setup_lts.x:

bashCopycurl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -

Step 4: Install Node.js

Now that the repository has been added, you can install Node.js using the apt package manager. Run:

bashCopysudo apt install nodejs

This will install both Node.js and npm (Node Package Manager), which is used to manage your project’s dependencies.

Step 5: Verify Installation

Once the installation is complete, verify that Node.js and npm were installed correctly by checking their versions:

bashCopynode -v
npm -v

You should see the version numbers for both Node.js and npm, confirming the installation was successful.

How To Install Node.js on Ubuntu

Method 2: Installing Node.js via Ubuntu Package Manager (apt)

Ubuntu’s default package manager, apt, offers an easy way to install Node.js, but keep in mind that the version in the default Ubuntu repositories might not be the latest.

Step 1: Update Your Package List

As with the previous method, start by updating your package list to ensure you’re installing the latest available version from Ubuntu’s default repository:

bashCopysudo apt update

Step 2: Install Node.js

Install Node.js from the default Ubuntu repositories by running:

bashCopysudo apt install nodejs

Step 3: Verify Installation

Once the installation is finished, verify the installation by checking the version of Node.js:

bashCopynode -v

Keep in mind that the version of Node.js installed using apt might not be the latest one.

Installing npm (Node Package Manager)

In many cases, npm (Node Package Manager) is installed automatically along with Node.js. However, if you need to install it manually or update it, follow these steps.

Step 1: Install npm

If npm isn’t already installed, you can install it manually with:

bashCopysudo apt install npm

Step 2: Verify npm Installation

Check if npm is installed correctly by running:

bashCopynpm -v

Using Node Version Manager (nvm) for Node.js Version Management

If you need to manage multiple versions of Node.js, nvm (Node Version Manager) is a helpful tool. With nvm, you can easily install, switch between, and manage different versions of Node.js.

Step 1: Install nvm

First, you need to install nvm. To do this, run the following command in your terminal:

bashCopycurl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Then, close and reopen your terminal, or run:

bashCopysource ~/.bashrc

Step 2: Install a Specific Version of Node.js

Once nvm is installed, you can install a specific version of Node.js. For example, to install the latest LTS version:

bashCopynvm install --lts

To install a specific version, such as 16.0.0, use:

bashCopynvm install 16.0.0

Step 3: Switch Between Versions

To switch between different versions of Node.js, use the following command:

bashCopynvm use 16.0.0

You can verify the active version with:

bashCopynode -v

Troubleshooting Common Node.js Installation Issues on Ubuntu

1. Permission Issues

If you encounter permission errors, try running the installation commands with sudo. For example:

bashCopysudo apt install nodejs

If you’re using nvm, make sure you don’t need elevated permissions for managing versions.

2. Version Conflicts

Sometimes, the version of Node.js installed via apt may not be the latest. If you need the latest version, it’s best to use the NodeSource repository or nvm to ensure you have access to the newest features.

3. npm Not Installed

If npm is not installed or doesn’t work properly, you can install it separately using:

bashCopysudo apt install npm

Or, if you’re using nvm, you can update npm with:

bashCopynvm install-latest-npm

4. Corrupted node_modules Directory

If you encounter issues related to missing or corrupted dependencies, try deleting the node_modules folder and reinstalling dependencies:

bashCopyrm -rf node_modules
npm install

Conclusion

Install Node.js on Ubuntu is a straightforward process that can be done in several ways—via the NodeSource repository, the default apt manager, or with nvm for managing multiple versions. Once installed, you’ll have access to Node.js and npm, and be able to start building and managing your JavaScript-based applications with ease.

If you run into any issues during installation, don’t hesitate to troubleshoot by checking for permission problems, version conflicts, or corrupted installations. Node.js is a powerful tool, and with it set up on Ubuntu, you’re ready to take your development projects to the next level! Click here to know more about installation guide and tips.

Read Also : How to Install Node Modules on Angular?

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top