How to Install Node.js on Ubuntu 22.04?

Relia Software

Relia Software

Thuoc Nguyen

Relia Software

featured

In this guide, we'll explore 3 distinct methods for installing Node.js on Ubuntu system. Each method has its benefits and is suited to different types of users and scenarios.

How to Install Node.js on Ubuntu 22.04?

Table of Contents

Ubuntu, known for its wide adoption and robust features, serves as an excellent platform for running Node.js applications. Its strong package management and security features complement the needs of Node.js developers, making Ubuntu a preferred choice for both development and production environments.

This guide aims to equip Ubuntu users with the necessary steps to install Node.js, thereby enabling them to leverage its full potential in building efficient and powerful web applications. Whether you are new to Node.js or an experienced developer, this guide seeks to facilitate a smooth setup process, allowing you to harness the power of Node.js on the reliable Ubuntu system.

>> Read more:

Prerequisites

Basic Knowledge of the Linux Command Line

The command line, or terminal, is a powerful tool for running commands in Linux-based systems like Ubuntu. A basic understanding of command line usage is crucial for installing software, navigating directories, modifying files, and executing scripts.

Familiarity with fundamental commands such as cd (change directory), ls (list directory contents), mkdir (make directory), and rm (remove files or directories) will be particularly useful. Additionally, understanding permissions and how to use sudo (superuser do) to execute commands with administrative privileges is essential for installing Node.js.

Ubuntu System Access

To install Node.js, you'll need access to a system running Ubuntu. This guide is applicable to various versions of Ubuntu, but it's always a good idea to be on a relatively recent version to ensure compatibility and security. You can check your Ubuntu version by opening a terminal and typing cat /etc/lsb-release. You can see the version of Ubuntu as the information below:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04.3 LTS"

Sudo Privileges

Sudo privileges allow a user to execute commands with the security privileges of another user, by default the superuser or root. Installing software on Ubuntu typically requires these privileges to modify system settings or install packages globally. If you're not the system administrator, you may need to ask the administrator to grant you sudo access or to perform the installation steps that require elevated privileges on your behalf.

Having met these prerequisites ensures that you have the necessary skills and system access to proceed with installing Node.js on your Ubuntu system confidently. This foundational knowledge will also aid in troubleshooting any issues that may arise during the installation process, making for a smoother, more enjoyable development setup experience.

3 Methods for Node.js Installation on Ubuntu

In this guide, we'll explore three distinct methods for installing Node.js on your Ubuntu system. Each method has its advantages and is suited to different types of users and scenarios. Whether you're looking for the simplest approach, need to manage multiple Node.js versions, or require a specific version of Node.js not provided by your package manager, one of these methods will meet your needs.

>> Read more: A Type-Safe Backend with NestJS, Prisma, Pothos, & GraphQL

Method 1: Installing Node.js with Ubuntu Package Manager

The Ubuntu package manager, apt, is the easiest and most straightforward method for installing Node.js. This approach is best suited for users who want a quick setup and don't need to manage multiple Node.js versions. The version of Node.js installed via apt is determined by the Ubuntu package repository, which might not always be the latest. However, it is tested and maintained by Ubuntu developers, ensuring stability and security.

Step 1: Update Package Repository

Before installing any new software, it's a good practice to update your package repository to ensure you have access to the latest versions of software and security updates. This can help prevent potential conflicts and ensure that the installation process goes smoothly.

  • Open your terminal.
  • Run the following command to update your package repository.
sudo apt update

This command contacts the software repositories configured on your system and updates the package lists to reflect the latest available version

Step 2: Install Node.js

Once your package repository is updated, you can proceed to install Node.js.

  • In the terminal, run the following command to install Node.js
sudo apt install nodejs

This command installs the Node.js package from the Ubuntu repository. Note that the version available in the repository may not be the latest version Node.js, but it is tested and maintained for Ubuntu, ensuring stability and security.

Step 3: Verify Installation

After the installation is complete, it is a good idea to verify that Node.js was successfully installed and to check the installed version.

  • To verify the installation and check the version of Node.js, run:
node -v

This command displays the version of Node.js that is currently installed on your system. For example, you might see output similar to v12.22.9, indicating that version v12.22.9 of Node.js is installed.

Method 2: Installing Node.js with NVM (Node Version Manager)

Installing Node.js using NVM (Node Version Manager) offers flexibility in managing multiple Node.js versions on a single system. This method is particularly beneficial for developers who work on various projects requiring different Node.js versions. Here’s how to install Node.js with NVM:

Step 1: Install NVM

First, you'll need to install NVM itself. NVM allows you to install multiple versions of Node.js and switch between them as needed.

  • Open your terminal.
  • Run the following command to download and install NVM.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

This script clones the NVM repository from GitHub to ~/.nvm and adds the script to your profile (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc), so it's available when you open a new terminal session.

Note: Always check the https://github.com/nvm-sh/nvm repository for the latest installation instructions and version.

Step 2: Activate NVM

After installing, you need to activate NVM by sourcing your profile script.

  • If NVM does not automatically active, you might need to close and re-open your terminal; or run the appropriate file for your shell (~/.profile , ~/.zshrc , etc.) or:
source ~/.bashrc

Step 3: Install Node.js Using NVM

Now that NVM is installed and activated, you can install Node.js.

  • To install the latest version of Node.js, run:
nvm install node

If you see that screen on your computer. You have installed successfully Node.js with version v21.6.2:

installed-successful-node.js-with-version-v21-6-2.png

  • To install a specific version of Node.js, specify the version. For example, to install Node.js version 18.12.0, run:
nvm install 18.12.0

Let see the results here to make sure you are successful:

install-nodejs-version-18-12-0.png

Step 4: Switch Between Node.js Versions

NVM makes it easy to switch between installed Node.js versions depending on your project's needs.

  • To switch to a version you have installed, use:
nvm use <version>

For example, to switch to Node.js version 18.12.0, you would run:

nvm use 18.12.0

Here we are if the statement are run successfully:

switch-to-nodejs-version-18-12-0.png

  • To see all installed Node.js versions and which version you're currently using, run:
mvn ls

Here are the Node.js versions which are installed on my machine:

node.js-versions-which-are-installed.png

By following these steps, you’ve not only installed Node.js using NVM but also gained the flexibility to manage multiple Node.js versions easily. This setup is ideal for development environments where projects may require different Node.js versions to run or be tested against.

Method 3: Installing Node.js from Official Node.js Binary Distributions

Step 1: Download Node.js Binaries

First, you'll need to download the Node.js binaries.

  • Visit the official Node.js download page at https://nodejs.org/en/download/.
  • Select the "Linux Binaries (x64)" for the version of Node.js you wish to install.

Step 2: Extract Node.js Archive

Once the download is complete, you'll need to extract the Node.js archive to a directory on your system.

  • Open your terminal.
  • Navigate to the directory where the Node.js archive was downloaded.
  • Use the following command to extract the archive (replace node-vXX.XX.X-linux-x64.tar.xz with the actual file name of the downloaded archive):
tar -xJvf node-vXX.XX.X-linux-x64.tar.xz -C /usr/local/lib/nodejs

This command extracts the Node.js binaries to /usr/local/lib/nodejs.

Step 3: Set Up Environment Variables

Next, you'll need to add Node.js to your PATH to make it accessible from anywhere in your terminal.

  • Open your ~/.bashrc, ~/.profile, or ~/.bash_profile file in a text editor.
  • Add the following lines at the end of the file (replace node-vXX.XX.X with the actual directory name you extracted):
export PATH=/usr/local/lib/nodejs/node-vXX.XX.X-linux-x64/bin:$PATH

This command adds the Node.js binary directory to your PATH.

Step 4: Reload Profile

For the changes to take effect, you'll need to reload your profile.

  • In your terminal, run the appropriate profile file you edited or:
source ~/.bashrc

Step 5: Verify Installation

Finally, verify that Node.js was installed successfully and check the installed version.

  • In your terminal, run:
node -v

This command displays the version of Node.js that is currently installed, confirming that the installation process was successful.

>> Read more: How to Choose the Right NodeJS Development Company?

Conclusion

In this guide, we've explored three different methods for installing Node.js on Ubuntu, each catering to various needs and preferences. Whether you opted for the simplicity of the Ubuntu Package Manager, the versatility of NVM (Node Version Manager), or the precision of installing directly from the official Node.js binary distributions, the key is to select the approach that aligns best with your specific development requirements.

For developers who work on multiple projects or need to test their applications across different versions of Node.js, NVM is an invaluable tool. It provides the flexibility to switch between versions seamlessly, without affecting the global system configuration. This can significantly enhance your workflow and productivity.

Finally, installing Node.js directly from the official binary distributions is perfect for users who require a specific version of Node.js that's not available through the other methods. Although it involves a few more steps, this method gives you complete control over the version and configuration of Node.js on your system.

>>> Follow and Contact Relia Software for more information!

  • development
  • coding