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 build tools and even some desktop applications. Its non-blocking, event-driven architecture makes it highly efficient for handling concurrent requests, which is crucial for scalable and responsive applications. For Linux users, installing and managing Node.js is a straightforward process, with several methods available to suit different needs and preferences. This guide will walk you through the most common and effective ways to get Node.js up and running on your Linux distribution, ensuring you’re ready to harness its full potential.

Understanding Node.js Installation Methods

Before diving into the specific commands, it’s helpful to understand the different approaches to installing Node.js on Linux. Each method offers distinct advantages:

Package Managers (APT, YUM/DNF)

Most Linux distributions come with a built-in package manager (like APT for Debian/Ubuntu or YUM/DNF for Fedora/CentOS/RHEL). These managers simplify software installation by handling dependencies and updates automatically. While convenient, the Node.js versions available through default repositories might not always be the latest.

NodeSource Repository

NodeSource provides up-to-date Node.js packages for various Linux distributions. This method offers a good balance between ease of use and access to recent Node.js versions. It involves adding a custom repository to your system’s package manager.

Node Version Manager (NVM)

For developers who need to switch between different Node.js versions for various projects, NVM is an invaluable tool. It allows you to install, manage, and switch between multiple Node.js versions seamlessly without system-wide conflicts. This is the recommended approach for most developers due to its flexibility.

Manual Installation from Source

While less common for general use, compiling Node.js from its source code offers the ultimate control and access to the very latest development builds. This method is more involved and typically reserved for advanced users or specific testing scenarios.

Installing Node.js with Package Managers

This is often the quickest way to get Node.js installed, especially for users who don’t require the absolute latest version or the flexibility of managing multiple versions.

For Debian and Ubuntu-based Systems (APT)

  1. Update Package Lists:
    Before installing any new software, it’s always a good practice to update your local package index to ensure you’re fetching the latest information about available packages.

    sudo apt update
    
  2. Install Node.js and npm:
    Node.js is typically distributed with its package manager, npm (Node Package Manager). You can install both simultaneously.

    sudo apt install nodejs npm
    

    Note: On older Ubuntu versions, the package might be named nodejs-legacy if nodejs conflicts with another package. However, for most modern systems, nodejs is the correct package name.

  3. Verify Installation:
    After the installation completes, you can verify the installed versions of Node.js and npm.
    bash
    node -v
    npm -v

    The output will display the version numbers, confirming a successful installation.

For Fedora, CentOS, and RHEL-based Systems (YUM/DNF)

  1. Update Package Lists:
    Similar to APT, update your system’s package cache.

    sudo yum update  # or sudo dnf update on newer Fedora/RHEL
    
  2. Install Node.js and npm:
    Install Node.js using your distribution’s package manager.

    sudo yum install nodejs npm # or sudo dnf install nodejs npm
    
  3. Verify Installation:
    Check the installed versions.
    bash
    node -v
    npm -v

Limitations of Package Managers: The primary drawback here is that the versions of Node.js available through default repositories are often outdated compared to the latest stable or LTS (Long Term Support) releases. If you need a newer version or need to manage multiple versions, consider the following methods.

Installing Node.js via NodeSource Repository

NodeSource maintains a repository that offers more recent versions of Node.js, including the latest stable and LTS releases. This method integrates seamlessly with your system’s package manager.

Adding the NodeSource Repository (for Debian/Ubuntu)

  1. Download and Execute the Setup Script:
    NodeSource provides convenient setup scripts. You need to specify the major Node.js version you want to install (e.g., setup_18.x for Node.js 18, setup_20.x for Node.js 20). Replace X.x with the desired major version.

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

    This script will add the NodeSource repository to your system’s sources list and update your package cache.

  2. Install Node.js:
    Once the repository is added, you can install Node.js just like you would with the default repositories, but now it will pull from NodeSource.

    sudo apt install nodejs
    

    Note: You don’t typically need to install npm separately, as it’s included with the nodejs package from NodeSource.

  3. Verify Installation:
    Confirm the installation by checking the versions.
    bash
    node -v
    npm -v

Adding the NodeSource Repository (for Fedora/CentOS/RHEL)

  1. Download and Execute the Setup Script:
    Similar to Debian/Ubuntu, NodeSource provides setup scripts for RPM-based systems. Replace X.x with the desired major version.

    # For Node.js 20.x
    curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -
    
    # For Node.js 18.x
    curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
    
  2. Install Node.js:
    After running the script, install Node.js using YUM or DNF.

    sudo yum install nodejs # or sudo dnf install nodejs
    
  3. Verify Installation:
    Check the installed versions.
    bash
    node -v
    npm -v

Advantages of NodeSource: This method provides access to more recent and specific Node.js versions, including LTS releases, which are crucial for production environments.

Installing Node.js with Node Version Manager (NVM)

NVM is the most flexible and developer-friendly way to manage Node.js installations. It allows you to install multiple Node.js versions side-by-side and switch between them easily.

Installing NVM

  1. Download and Run the Installation Script:
    NVM is installed via a shell script. You can find the latest installation command on the official NVM GitHub repository, but a typical command looks like this:

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
    

    Note: Always check the NVM GitHub page for the most current version number to ensure you’re installing the latest release.

  2. Source Your Shell Profile:
    The installation script modifies your shell profile (.bashrc, .zshrc, etc.) to load NVM. You’ll need to either close and reopen your terminal or source your profile file to make NVM available in your current session.

    source ~/.bashrc  # or source ~/.zshrc if you use Zsh
    
  3. Verify NVM Installation:
    Check if NVM is installed and working correctly.
    bash
    command -v nvm

    This command should output nvm if it’s successfully installed and recognized.

Using NVM to Install Node.js

  1. List Available Node.js Versions:
    To see which versions of Node.js are available for installation, use the ls-remote command.

    nvm ls-remote
    

    This will display a long list of all available Node.js versions, including the latest releases and older ones.

  2. Install a Specific Node.js Version:
    You can install any version you need. It’s common to install the latest LTS version or the latest stable version.

    # Install the latest LTS version
    nvm install --lts
    
    # Install the latest stable version
    nvm install node
    
    # Install a specific version, e.g., Node.js 18.17.1
    nvm install 18.17.1
    

    When you install a version, NVM automatically sets it as the currently active version for your shell session.

  3. Install Multiple Versions:
    You can install several versions and switch between them.

    nvm install 16.20.2
    nvm install 18.17.1
    nvm install 20.5.0
    
  4. Switch Between Node.js Versions:
    To use a different installed version, use the use command.

    nvm use 18.17.1
    

    This command makes Node.js version 18.17.1 the active one for the current terminal session.

  5. Set a Default Node.js Version:
    To have a specific version automatically used whenever you open a new terminal, set it as the default.

    nvm alias default 18.17.1
    

    This will ensure that Node.js version 18.17.1 is active by default.

  6. List Installed Versions:
    To see all the Node.js versions you have installed with NVM, use:

    nvm ls
    

    The currently active version will be marked with an arrow (->).

  7. Uninstall Node.js Versions:
    If you no longer need a specific version, you can uninstall it.
    bash
    nvm uninstall 16.20.2

Benefits of NVM: NVM is ideal for developers working on multiple projects that might require different Node.js versions. It simplifies dependency management and prevents version conflicts, making your development workflow much smoother.

Post-Installation: Understanding npm and Global Packages

Once Node.js is installed, you’ll have access to npm, the Node Package Manager. npm is used to install external libraries and tools that extend Node.js functionality.

npm Basics

  • Installing Packages:
    To install a package for your current project (locally, within the node_modules directory of your project), navigate to your project’s root directory and run:

    npm install <package-name>
    

    This will add the package to your node_modules folder and list it in your project’s package.json file.

  • Global Packages:
    Some npm packages are command-line tools that you might want to use from anywhere on your system, not just within a specific project. These are installed globally.

    npm install -g <package-name>
    

    Common examples of globally installed packages include tools like nodemon (for automatically restarting Node.js applications during development) or pm2 (a process manager for Node.js applications).

  • Managing Global Packages:
    To see a list of your globally installed packages:
    bash
    npm list -g --depth=0

    Note: On some systems, especially when using package managers like APT or YUM, installing global packages might require sudo. With NVM, global packages are installed on a per-Node.js version basis, and you typically don’t need sudo for global installs with NVM itself.

Updating npm

npm is updated independently of Node.js. To update npm to the latest version:

npm install -g npm@latest

Conclusion

Installing Node.js on Linux is a foundational step for any developer looking to leverage its powerful JavaScript runtime environment. Whether you opt for the simplicity of package managers, the up-to-date convenience of NodeSource, or the unparalleled flexibility of NVM, each method provides a reliable path to integrating Node.js into your development workflow. For most developers, NVM stands out as the most robust solution, offering granular control over Node.js versions, which is essential for managing diverse project requirements and ensuring compatibility. By following these steps, you’ll be well-equipped to start building and deploying Node.js applications on your Linux system.

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