How to Install Node.js in Linux

Node.js has become an indispensable tool for developers across various domains, from web application backends to command-line interfaces and even embedded systems. Its asynchronous, event-driven architecture makes it highly efficient for building scalable network applications. For Linux users, installing and managing Node.js versions can be a streamlined process, allowing for rapid development and deployment of cutting-edge technologies. This guide will walk you through the most common and recommended methods for installing Node.js on your Linux distribution, ensuring you have a robust and flexible development environment.

Understanding Node.js Installation Methods

Before diving into the installation process, it’s beneficial to understand the different approaches available. Each method offers distinct advantages depending on your needs, such as requiring specific versions, managing multiple projects with different Node.js dependencies, or prioritizing ease of use. The primary methods include using your distribution’s package manager, utilizing a version manager like NVM (Node Version Manager), or downloading pre-compiled binaries.

Package Manager Installation

Most Linux distributions offer Node.js through their native package managers (e.g., apt for Debian/Ubuntu, dnf/yum for Fedora/CentOS/RHEL, pacman for Arch Linux). This method is often the simplest for users who need a readily available, stable version. However, it’s important to note that the versions available in distribution repositories might not always be the latest.

  • Debian/Ubuntu (using apt):
    The apt package manager is the standard for Debian-based systems. To install Node.js, you’ll typically first update your package list and then install the nodejs and npm (Node Package Manager) packages.

    sudo apt update
    sudo apt install nodejs npm
    

    After installation, you can verify the versions:

    node -v
    npm -v
    

    It’s crucial to be aware that some older Debian/Ubuntu versions might ship with a very outdated Node.js version. In such cases, using a third-party repository or a version manager is highly recommended.

  • Fedora/CentOS/RHEL (using dnf/yum):
    For Fedora, CentOS, and RHEL-based systems, dnf (or yum on older versions) is the package manager. Similar to apt, you’ll update your package index before installing Node.js.

    sudo dnf update -y # or sudo yum update -y
    sudo dnf install nodejs -y # or sudo yum install nodejs -y
    

    Note that in some distributions, npm is installed as a separate package or is included with nodejs. If npm isn’t installed by default, you might need to install it explicitly:

    sudo dnf install npm -y # or sudo yum install npm -y
    

    Verify the installation:

    node -v
    npm -v
    
  • Arch Linux (using pacman):
    Arch Linux users can leverage pacman, its powerful package manager. The process is straightforward: update the system and then install Node.js.

    sudo pacman -Syu
    sudo pacman -S nodejs npm
    

    Confirm the installation:

    node -v
    npm -v
    

    The advantage of using a package manager is the integration with your system’s update mechanisms. However, the primary drawback is often the lag in available versions.

Using Node Version Manager (NVM)

For developers who need to work with multiple Node.js projects that might require different versions, or if you need the absolute latest releases, Node Version Manager (NVM) is the de facto standard. NVM allows you to install, manage, and switch between various Node.js versions seamlessly.

  • Installing NVM:
    NVM is typically installed via a script downloaded from its GitHub repository. It’s recommended to check the official NVM repository for the most up-to-date installation script.

    First, update your package list and install necessary development tools (like curl or wget, and build essentials) which are often required for compiling Node.js from source if a pre-compiled binary isn’t available for your architecture.

    sudo apt update && sudo apt install curl build-essential -y # For Debian/Ubuntu
    sudo dnf update -y && sudo dnf install curl gcc-c++ make -y # For Fedora/CentOS/RHEL
    sudo pacman -Syu --needed base-devel curl # For Arch Linux
    

    Then, download and run the NVM installation script. The command below will fetch the latest stable version of NVM. Always refer to the official NVM GitHub page for the exact and latest installation command.

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

    After running the script, you’ll need to close and reopen your terminal, or source your shell’s configuration file (e.g., ~/.bashrc, ~/.zshrc) for NVM to be recognized.

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

    Verify NVM installation:

    command -v nvm
    

    This command should output nvm if it’s installed correctly.

  • Installing Node.js with NVM:
    Once NVM is installed, you can install Node.js versions.

    To install the latest stable version:

    nvm install node
    

    To install a specific version (e.g., Node.js 20.11.0):

    nvm install 20.11.0
    

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

    nvm install --lts
    
  • Using Installed Node.js Versions with NVM:
    After installing one or more versions, you need to tell NVM which version to use.

    To use the most recently installed version:

```bash
nvm use node
```

To use a specific installed version:

```bash
nvm use 20.11.0
```

You can set a default Node.js version that will be used automatically when you open a new terminal session:

```bash
nvm alias default node # Sets the latest installed version as default
# or
nvm alias default 20.11.0 # Sets a specific version as default
```

To list all installed Node.js versions:

```bash
nvm ls
```

To uninstall a Node.js version:

```bash
nvm uninstall 20.11.0
```

NVM is incredibly powerful for managing complex development workflows involving multiple Node.js projects with varying dependency requirements.

Installing from Pre-compiled Binaries

Another method, less common for general development but useful in specific scenarios (like embedded systems or environments where package managers or NVM are restricted), is to download pre-compiled binaries directly from the official Node.js website.

  • Downloading Binaries:
    Visit the official Node.js downloads page (https://nodejs.org/en/download/). You’ll find links to download pre-compiled binaries for Linux, often in .tar.gz or .tar.xz formats. Choose the appropriate architecture (e.g., x64 for most modern desktops/servers).

    Alternatively, you can use wget or curl directly on your Linux terminal. For example, to download the latest LTS version for Linux x64:

    wget https://nodejs.org/dist/v20.11.1/node-v20.11.1-linux-x64.tar.xz
    
  • Extracting and Installing Binaries:
    Once downloaded, extract the archive. For example, if you downloaded node-v20.11.1-linux-x64.tar.xz:

    tar -xJf node-v20.11.1-linux-x64.tar.xz
    

    This will create a directory named node-v20.11.1-linux-x64. You can then move this directory to a suitable location, such as /usr/local/lib/nodejs or a custom directory in your home folder.

    sudo mv node-v20.11.1-linux-x64 /usr/local/lib/nodejs
    
  • Configuring Environment Variables:
    To run Node.js and npm from any terminal location, you need to add their binaries to your system’s PATH. Edit your shell’s configuration file (e.g., ~/.bashrc, ~/.zshrc):

    nano ~/.bashrc
    

    Add the following lines to the end of the file:

    export NODEJS_HOME=/usr/local/lib/nodejs
    export PATH=$NODEJS_HOME/bin:$PATH
    

    Save the file and then source it to apply the changes:

    source ~/.bashrc
    

    Verify the installation:

    node -v
    npm -v
    

    This method provides complete control over the installation location but requires manual management of environment variables and updates.

Verifying Your Node.js Installation

Regardless of the installation method you choose, it’s crucial to verify that Node.js and npm have been installed correctly. Open your terminal and run the following commands:

  • Check Node.js version:

    node -v
    

    This command should output the installed Node.js version number (e.g., v20.11.1).

  • Check npm version:

    npm -v
    

    This command should output the installed npm version number (e.g., 10.2.4).

If these commands return version numbers, your Node.js installation is successful. If you encounter “command not found” errors, it usually indicates an issue with the installation process or that the PATH environment variable has not been set correctly.

Best Practices and Maintenance

Maintaining your Node.js environment is as important as installing it. Here are some best practices to ensure a smooth development experience:

  • Use NVM for Flexibility: For most developers, NVM is the recommended approach. It allows you to easily switch between Node.js versions for different projects, avoiding compatibility issues and enabling you to test your applications with various Node.js runtimes.

  • Keep npm Updated: npm is updated more frequently than Node.js itself. It’s good practice to keep npm updated to the latest version. You can do this with:

    npm install -g npm@latest
    

    If you are using NVM, this command will update npm for the currently active Node.js version.

  • Understand LTS vs. Current Releases: Node.js has two main release lines: LTS (Long Term Support) and Current. LTS versions are recommended for production environments due to their stability and extended support. Current versions include the latest features but may be less stable and have shorter support cycles.

  • Security Updates: Always keep your Node.js installation updated, especially for security patches. NVM makes it easy to install newer versions and migrate your projects.

By following these installation and maintenance guidelines, you can establish a robust and efficient Node.js development environment on your Linux system, ready to tackle any project with confidence.

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