How to Install Node.js on Linux

Node.js has become an indispensable tool for modern web development, powering everything from server-side applications and APIs to command-line utilities and frontend build tools. Its asynchronous, event-driven architecture makes it highly efficient for handling concurrent connections, a crucial feature for scalable applications. For Linux users, installing Node.js is a straightforward process, with several reliable methods available to ensure you have the right version for your project needs. This guide will walk you through the most common and recommended installation techniques, empowering you to get started with Node.js development on your Linux distribution.

Understanding Node.js and npm

Before diving into the installation process, it’s beneficial to have a basic understanding of what Node.js is and its companion package manager, npm.

What is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside of a web browser. It’s built on Chrome’s V8 JavaScript engine, allowing developers to leverage the power of JavaScript for server-side programming. This unification of language across the entire development stack (frontend and backend) has revolutionized web development, enabling teams to work more efficiently and share code more effectively. Node.js is particularly well-suited for building real-time applications, microservices, and APIs due to its non-blocking I/O operations.

The Role of npm

npm (Node Package Manager) is the default package manager for Node.js. It’s an integral part of the Node.js ecosystem and comes bundled with every Node.js installation. npm makes it incredibly easy to:

  • Discover and Install Packages: Access a vast repository of open-source libraries and modules (packages) that extend Node.js’s functionality. From frameworks like Express.js to utility libraries and development tools, npm simplifies the process of incorporating them into your projects.
  • Manage Dependencies: Keep track of all the external packages your project relies on. The package.json file, generated automatically, lists these dependencies, their versions, and helps ensure that your project can be reliably set up on any machine.
  • Share Your Code: npm also allows developers to publish their own packages, contributing to the ever-growing open-source community.

Understanding these fundamental components will make the subsequent installation and usage of Node.js on your Linux system much smoother.

Installation Methods for Node.js on Linux

Linux distributions offer multiple ways to install Node.js, each with its own advantages. The best method for you will depend on your specific needs, such as the required Node.js version, system stability requirements, and your comfort level with different package management systems.

Method 1: Using the NodeSource Repository (Recommended for Specific Versions)

The NodeSource repository is a widely trusted and recommended source for installing up-to-date versions of Node.js on various Linux distributions. This method provides more control over the specific Node.js version you install and ensures you’re using a version actively maintained by NodeSource.

Step 1: Update Your System’s Package Index

Before adding any new repositories or installing software, it’s always a good practice to update your system’s package index to ensure you have access to the latest information about available packages.

sudo apt update  # For Debian/Ubuntu-based systems
sudo yum update  # For RHEL/CentOS/Fedora-based systems
sudo dnf update  # For newer Fedora versions

Step 2: Add the NodeSource Repository

NodeSource provides setup scripts that automate the process of adding their repository to your system. You’ll need to choose the script corresponding to the major Node.js version you wish to install. For instance, to install Node.js 20 (the current LTS – Long Term Support version as of this writing), you would use the following command:

# For Node.js v20.x LTS
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -

# Alternatively, for the latest current release (e.g., Node.js 21.x)
# curl -fsSL https://deb.nodesource.com/setup_21.x | sudo -E bash -
  • Explanation:
    • curl -fsSL: This command downloads the setup script from the specified URL.
      • -f: Fail silently (no output on HTTP errors).
      • -s: Silent mode (don’t show progress meter or error messages).
      • -S: Show error message if it fails.
      • -L: Follow redirects.
    • |: This is a pipe, which sends the output of the curl command (the script content) as input to the next command.
    • sudo -E bash -: This executes the received script with sudo privileges.
      • sudo: Run the command with superuser privileges.
      • -E: Preserves the user’s existing environment variables, which is sometimes necessary for the script to run correctly.
      • bash -: Executes the script using the Bash shell.

The script will automatically detect your Linux distribution and add the appropriate NodeSource repository. It will also perform an apt update (or equivalent) on your behalf to refresh the package list with the new repository.

Step 3: Install Node.js

Once the repository has been added, you can install Node.js and npm using your distribution’s package manager.

sudo apt install nodejs  # For Debian/Ubuntu-based systems
sudo yum install nodejs  # For RHEL/CentOS-based systems
sudo dnf install nodejs  # For newer Fedora versions

This command will install both the node executable and the npm command-line tool.

Step 4: Verify the Installation

After the installation is complete, it’s crucial to verify that Node.js and npm have been installed correctly and to check their versions.

node -v
npm -v

If the installation was successful, these commands will output the installed versions of Node.js and npm, respectively.

Method 2: Using a Package Manager (e.g., apt, yum, dnf) Directly

Most Linux distributions include Node.js in their default repositories. This is often the simplest method for users who want a stable, well-tested version of Node.js that is maintained by their distribution. However, the versions available in default repositories might not always be the latest.

Step 1: Update Your System’s Package Index

As always, start by updating your system’s package list.

sudo apt update  # For Debian/Ubuntu-based systems
sudo yum update  # For RHEL/CentOS/Fedora-based systems
sudo dnf update  # For newer Fedora versions

Step 2: Install Node.js

Now, you can install Node.js directly using your distribution’s package manager.

sudo apt install nodejs npm  # For Debian/Ubuntu-based systems
sudo yum install nodejs npm  # For RHEL/CentOS-based systems
sudo dnf install nodejs npm  # For newer Fedora versions
  • Note: On some distributions, you might need to install npm separately from nodejs. The command above includes both for broader compatibility.

Step 3: Verify the Installation

Check the installed versions to confirm success.

node -v
npm -v

This method is quick and easy but may not give you access to the very latest Node.js features or require you to install a specific version.

Method 3: Using NVM (Node Version Manager)

NVM (Node Version Manager) is a powerful script that allows you to install and manage multiple versions of Node.js on a single machine. This is incredibly useful for developers who work on projects requiring different Node.js versions or who want to easily switch between them.

Step 1: Install NVM

First, you need to download and install the NVM script. You can find the latest installation script on the official NVM GitHub repository. It’s recommended to check the repository for the most current installation command.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
  • Explanation:
    • curl -o- ...: Downloads the script and outputs it to standard output.
    • | bash: Pipes the output to bash for execution.

The installation script will download NVM and add the necessary lines to your shell profile file (e.g., ~/.bashrc, ~/.zshrc).

Step 2: Load NVM

After installation, you need to source your shell profile to make the nvm command available in your current terminal session.

source ~/.bashrc  # Or source ~/.zshrc if you use Zsh

To ensure NVM is available in new terminal sessions, you might need to close and reopen your terminal, or it might be automatically sourced if the script correctly updated your profile.

Step 3: Install a Node.js Version with NVM

Now you can use NVM to install specific Node.js versions.

  • To install the latest LTS (Long Term Support) version:

    nvm install --lts
    
  • To install a specific version (e.g., Node.js 18.17.0):

    nvm install 18.17.0
    
  • To install the latest current version:

    nvm install node
    

Step 4: Use a Specific Node.js Version

Once you’ve installed a version, you need to tell NVM to use it.

nvm use 20.9.0  # Replace with the version you installed

You can also set a default Node.js version that will be used every time you open a new terminal.

nvm alias default 20.9.0  # Replace with the version you want as default

Step 5: Verify the Installation

Confirm that the correct Node.js and npm versions are active.

node -v
npm -v

NVM provides the greatest flexibility for developers who need to manage multiple Node.js environments.

Managing Node.js Versions and Best Practices

Regardless of the installation method you choose, effective management of Node.js versions and related practices is key to a smooth development workflow.

Understanding LTS vs. Current Releases

Node.js has two primary release lines:

  • LTS (Long Term Support): These versions are prioritized for stability and receive long-term support, including security updates and bug fixes. LTS versions are generally recommended for production environments.
  • Current: These versions include the latest features and are released more frequently. They are ideal for testing new features or for projects that can accommodate more frequent updates.

When installing, it’s often best to choose the latest LTS version unless you have a specific need for features in the Current release.

Global vs. Local Package Installation

npm allows you to install packages globally or locally to a project.

  • Global Installation (npm install -g <package_name>): Installs packages that are intended to be used as command-line tools (e.g., nodemon, create-react-app). These are installed in a system-wide location. Be cautious with global installations, as they can sometimes lead to version conflicts.
  • Local Installation (npm install <package_name>): Installs packages directly into your project’s node_modules directory. These are project-specific dependencies and are managed via the package.json file. This is the standard and recommended way to install libraries and frameworks for your applications.

Updating Node.js

  • Via apt/yum/dnf: If you installed Node.js through your distribution’s package manager, you can update it using the same system update commands. For example, sudo apt update && sudo apt upgrade nodejs. However, this will likely only update you to the latest version available in that specific repository, which might not be the absolute latest release.
  • Via NodeSource: If you used the NodeSource repository, you’ll typically update by running the setup script for the new desired version and then reinstalling nodejs. For example, if you want to upgrade from Node.js 18 to 20, you would run the setup_20.x script and then sudo apt install nodejs.
  • Via NVM: This is the easiest method. You simply install the new version (nvm install <new_version>) and then switch to it (nvm use <new_version>). You can then optionally set it as the default.

Uninstalling Node.js

  • Via apt/yum/dnf: Use your package manager’s remove command: sudo apt remove nodejs npm.
  • Via NodeSource: This typically involves removing the package as above and then potentially removing the repository configuration.
  • Via NVM: To uninstall a specific Node.js version installed with NVM, use nvm uninstall <version>. To uninstall NVM itself, you can manually remove the NVM directory (usually ~/.nvm) and remove the sourcing lines from your shell profile.

By understanding these methods and best practices, you can confidently install and manage Node.js on your Linux system, setting yourself up for efficient and robust development.

Leave a Comment

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

FlyingMachineArena.org is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com. Amazon, the Amazon logo, AmazonSupply, and the AmazonSupply logo are trademarks of Amazon.com, Inc. or its affiliates. As an Amazon Associate we earn affiliate commissions from qualifying purchases.
Scroll to Top