How to Install Node.js on Linux?

how to install node.js on linux

Node.js has become one of the most popular platforms for building server-side applications, and if you’re using Linux, you’re in luck—installing Node.js is straightforward. Whether you’re running Ubuntu, Debian, Fedora, or CentOS, you can get Node.js up and running in no time. This step-by-step guide will walk you through the process of install Node.js on Linux, troubleshooting common issues, and verifying your installation.

Why Install Node.js on Linux?

Before diving into the installation process, it’s important to understand the significance of Node.js and why it’s a great fit for Linux environments.

What is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime built on Chrome’s V8 JavaScript engine. It allows developers to run JavaScript code on the server-side, making it possible to build scalable, high-performance applications. Node.js is especially favored for applications that require real-time interactions, such as chat apps, online gaming, and collaborative tools.

With its event-driven, non-blocking I/O model, Node.js excels at handling a large number of simultaneous connections, making it perfect for scalable applications.

Why Linux?

Linux provides a solid foundation for server-side applications, and it’s often the operating system of choice for developers and system administrators. The Linux environment is lightweight, secure, and developer-friendly, making it ideal for running Node.js applications.

Additionally, Linux’s package managers and flexibility allow for smooth installation and version control for Node.js and npm (Node Package Manager), which makes it even more appealing to developers.

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

Now, let’s break down the installation process for different Linux distributions. Whether you’re on Ubuntu, Debian, Fedora, or CentOS, you’ll find the relevant instructions here.

Installing Node.js on Ubuntu/Debian

Step 1: Update Your Package Index

Before you start installing new software, it’s a good idea to update your system’s package index. Open a terminal and run the following command:

sudo apt update

Step 2: Install curl (if not already installed)

If you don’t have curl installed on your system, you’ll need it to download the installation script. Install curl using the following command:

sudo apt install curl

Step 3: Add NodeSource Repository

To ensure that you’re installing the latest version of Node.js, it’s best to use the NodeSource repository, which provides up-to-date versions. Run the following command to add the repository:

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

This script will add the NodeSource repository for the LTS (Long Term Support) version of Node.js. If you prefer the latest current version, replace setup_lts.x with setup_current.x.

Step 4: Install Node.js

Once the repository is added, you can install Node.js by running the following command:

sudo apt install -y nodejs

This command will install both Node.js and npm (Node Package Manager) automatically.

Step 5: Verify Installation

Once the installation is complete, you can verify that Node.js and npm have been successfully installed by checking their versions:

node -v
npm -v

If you see version numbers for both Node.js and npm, the installation was successful.

How to install Node.js on Linux

Install Node.js on Fedora

Step 1: Update Your System

Before starting the installation, ensure that your system is up to date by running the following command:

sudo dnf update

Step 2: Install Node.js

You can install Node.js directly from the Fedora repository using the following command:

sudo dnf install nodejs

This will install the latest stable version of Node.js and npm available in the Fedora package repository.

Step 3: Verify Installation

After the installation is complete, verify the installation by checking the versions of Node.js and npm:

node -v
npm -v

How to Install Node.js on CentOS

Step 1: Install EPEL Repository

CentOS doesn’t include the latest version of Node.js by default, so we need to install the EPEL (Extra Packages for Enterprise Linux) repository first. Run the following command:

sudo yum install epel-release

Step 2: Install Node.js

Once the repository is added, you can install Node.js with the following command:

sudo yum install nodejs

This will install the latest version of Node.js available in the CentOS repositories.

Step 3: Verify Installation

Finally, verify that Node.js and npm were installed successfully by checking their versions:

node -v
npm -v

Install Node.js on Other Linux Distributions

If you’re using a Linux distribution other than Ubuntu, Debian, Fedora, or CentOS, you can follow the same general process:

  1. Download and Install Node.js: You can download Node.js directly from the official Node.js website, or use your package manager to install it. For example, on Arch Linux, you can use pacman: sudo pacman -S nodejs npm
  2. Verify Installation: After installation, check the version of Node.js and npm using: node -v npm -v

Troubleshooting Common Node.js Installation Issues on Linux

Even though the installation process is usually smooth, you might encounter some issues. Here are some common problems and how to solve them.

1. Permission Errors

If you run into permission errors while installing Node.js, try using sudo to run the installation commands as a superuser.

For example:

sudo apt install nodejs

2. Version Conflicts

Sometimes, the version of Node.js available in the default repositories might not be the latest one. If you need the latest version, consider using the NodeSource repository (as shown in the Ubuntu/Debian installation section) to ensure you’re installing the most up-to-date version.

3. Missing Dependencies

Node.js requires certain dependencies to run correctly. If you encounter issues related to missing dependencies, you can install them manually using your package manager. For example, if you’re missing build-essential on Ubuntu, you can install it with:

sudo apt install build-essential

4. Using nvm (Node Version Manager)

If you need to manage multiple versions of Node.js on your system, you can use nvm (Node Version Manager). This is particularly helpful if you work on different projects requiring different versions of Node.js.

To install nvm, follow these steps:

  1. Install nvm by running the following command: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
  2. Close and reopen your terminal, or source your shell configuration file: source ~/.bashrc
  3. Install a specific version of Node.js using nvm: nvm install 16
  4. Use a specific version of Node.js: nvm use 16

Conclusion

Now that you know how to install Node.js on Linux, you’re all set to start building powerful applications. Whether you’re using Ubuntu, Debian, Fedora, or CentOS, the installation process is quick and simple. Once installed, you can leverage npm to manage packages, develop server-side applications, and start your Node.js journey.

If you encounter any installation issues, refer to the troubleshooting section or consider using nvm for easier version management. Node.js offers immense flexibility and power, and with your Linux setup complete, you’re ready to dive into JavaScript server-side development!

Read Also : How to Install Node.js on Your Machine?

Leave a Comment

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

Scroll to Top