How to Install npm on Linux

Node Package Manager (npm) is the default package manager for Node.js, a JavaScript runtime built on Chrome’s V8 JavaScript engine. It is an indispensable tool for JavaScript developers, enabling them to manage project dependencies, share code, and execute scripts. For Linux users, installing and configuring npm is a straightforward process, opening the door to a vast ecosystem of open-source packages and development tools. This guide will walk you through the various methods of installing npm on your Linux distribution, ensuring you have a robust and up-to-date environment for your JavaScript development needs.

Understanding Node.js and npm Installation Methods

Before diving into the installation process, it’s crucial to understand the relationship between Node.js and npm. npm is bundled with Node.js, meaning that when you install Node.js, npm is typically installed alongside it. Consequently, the primary methods for installing npm involve installing Node.js itself. Linux distributions offer several ways to achieve this, each with its own advantages regarding ease of use, version control, and system integration.

The most common methods include:

  • Using your distribution’s package manager: This is often the simplest and most integrated approach, leveraging the familiar tools provided by your Linux distribution (e.g., apt for Debian/Ubuntu, yum/dnf for Fedora/CentOS/RHEL, pacman for Arch Linux).
  • Using a Node Version Manager (NVM): This is a highly recommended method for developers who need to manage multiple Node.js versions simultaneously for different projects. NVM allows you to install, switch between, and uninstall Node.js versions with ease.
  • Downloading the official binary distributions: This method involves manually downloading pre-compiled binaries from the official Node.js website. It provides granular control but requires more manual setup.
  • Compiling from source: This is the most advanced method, offering ultimate control and customization but also being the most time-consuming and complex. It’s generally only necessary for specific use cases or when working with bleeding-edge versions.

Each of these methods has its place, and the best choice often depends on your specific requirements and familiarity with the Linux command line.

Method 1: Installing Node.js and npm via Distribution Package Managers

Leveraging your Linux distribution’s native package manager is often the most convenient way to install Node.js and npm. This method ensures that the software is integrated into your system’s package management framework, making updates and uninstallation simple.

For Debian and Ubuntu-based Systems (apt)

Debian and Ubuntu provide Node.js packages in their default repositories. While these versions are usually stable, they might not always be the latest releases.

  1. Update your package list:

    sudo apt update
    

    This command synchronizes your local package index with the repositories, ensuring you have access to the most current information about available packages.

  2. Install Node.js and npm:

    sudo apt install nodejs npm
    

    This command installs both the nodejs package and the npm package. The nodejs package provides the runtime environment, and the npm package installs the package manager.

  3. Verify the installation:
    After the installation completes, you can verify that Node.js and npm have been installed correctly by checking their versions:
    bash
    node -v
    npm -v

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

Note on NodeSource Repositories:
For access to more recent versions of Node.js than those available in the default repositories, you can use the NodeSource repository. This is a popular third-party repository maintained by NodeSource that provides up-to-date Node.js packages for various Linux distributions.

To install using NodeSource:

  • Add the NodeSource repository for your desired Node.js version (e.g., Node.js 20.x):

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

    Replace 20.x with the major version you wish to install (e.g., 18.x, 22.x).

  • Install Node.js (which includes npm):

    sudo apt install nodejs
    

    Note that with NodeSource, installing the nodejs package is usually sufficient as it includes npm.

  • Verify the installation:
    bash
    node -v
    npm -v

For Fedora, CentOS, and RHEL-based Systems (yum/dnf)

Fedora, CentOS, and RHEL use yum or dnf (for newer versions of Fedora and RHEL) as their package managers.

  1. Update your package list (for dnf):

    sudo dnf check-update
    

    Or for yum:

    sudo yum check-update
    
  2. Install Node.js and npm:
    For Fedora (using dnf):

    sudo dnf install nodejs
    

    For CentOS/RHEL (which might require EPEL repository for newer versions):

    sudo yum install nodejs
    

    In many cases, installing the nodejs package will also install npm.

  3. Verify the installation:
    bash
    node -v
    npm -v

Note on NodeSource Repositories for RPM-based Systems:
Similar to Debian/Ubuntu, NodeSource also provides repositories for RPM-based systems.

  • Add the NodeSource repository for your desired Node.js version (e.g., Node.js 20.x):

    curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -
    

    Replace 20.x with the major version you wish to install.

  • Install Node.js (which includes npm):

    sudo dnf install nodejs
    # or
    sudo yum install nodejs
    
  • Verify the installation:
    bash
    node -v
    npm -v

For Arch Linux (pacman)

Arch Linux users can install Node.js and npm directly from the official repositories.

  1. Update your system:

    sudo pacman -Syu
    
  2. Install Node.js and npm:

    sudo pacman -S nodejs npm
    

    While nodejs often includes npm, explicitly installing npm ensures it’s present.

  3. Verify the installation:
    bash
    node -v
    npm -v

Method 2: Installing Node.js and npm using Node Version Manager (NVM)

For developers who work on multiple projects requiring different Node.js versions, or who want to easily switch between stable and experimental releases, Node Version Manager (NVM) is an indispensable tool. NVM allows you to install and manage multiple Node.js versions without conflicts. It’s a script that runs in your user’s shell environment, meaning you don’t need sudo to install Node.js versions once NVM is set up.

Installing NVM

  1. Download and run the NVM installation script:
    The easiest way to install NVM is by downloading and running the installation script from its official GitHub repository. It’s recommended to check the official NVM GitHub page for the latest installation script version.

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

    (Note: Replace v0.39.7 with the latest version number found on the NVM GitHub page.)

    This script will download NVM and add the necessary source lines to your shell profile (~/.bashrc, ~/.zshrc, or ~/.profile).

  2. Load NVM:
    After the script finishes, you need to close and reopen your terminal, or source your profile file for the changes to take effect:

    source ~/.bashrc
    # or if you use zsh:
    # source ~/.zshrc
    
  3. Verify NVM installation:
    You can check if NVM is installed correctly by running:
    bash
    command -v nvm

    This should output nvm. If it doesn’t, ensure you’ve sourced your profile correctly.

Installing Node.js and npm with NVM

Once NVM is installed and loaded, managing Node.js versions becomes straightforward.

  1. Install the latest stable Node.js version:

    nvm install node
    

    This command will download and install the latest available stable version of Node.js, along with its corresponding npm version.

  2. Install a specific Node.js version:
    If you need a particular version, you can specify it:

    nvm install 18.18.2  # Example: Install Node.js version 18.18.2
    

    You can also install the latest version of a major release line:

    nvm install 20    # Installs the latest 20.x.x version
    
  3. Use a specific Node.js version:
    After installing a version, you need to tell NVM to use it:

    nvm use node   # Use the latest installed version
    # or
    nvm use 18.18.2 # Use a specific installed version
    

    The nvm use command sets the Node.js version for your current shell session. To make a version the default, use:

    nvm alias default node # Sets the latest installed as default
    # or
    nvm alias default 18.18.2 # Sets a specific version as default
    
  4. Verify the installation:
    After setting a version to use, verify Node.js and npm:
    bash
    node -v
    npm -v

Managing Node.js Versions with NVM

  • List installed versions:

    nvm ls
    

    This command shows all Node.js versions you have installed and which one is currently in use.

  • List available remote versions:

    nvm ls-remote
    

    This command displays a comprehensive list of all Node.js versions that can be installed.

  • Uninstall a Node.js version:
    bash
    nvm uninstall 16.17.0 # Example: Uninstall Node.js version 16.17.0

NVM is an essential tool for any serious Node.js developer on Linux, offering flexibility and control over your development environment.

Method 3: Installing from Official Node.js Binary Distributions

This method involves manually downloading pre-compiled binary files directly from the official Node.js website. It offers a high degree of control and doesn’t rely on system package managers or external scripts, which can be beneficial in restricted environments or when you need absolute certainty about the source.

Downloading the Binaries

  1. Visit the Node.js website:
    Navigate to the official Node.js downloads page: https://nodejs.org/en/download/

  2. Select the Linux Binaries:
    On the downloads page, you will find a section for “Linux Binaries (x64)”. Choose the appropriate architecture for your system (most modern systems are 64-bit). You can choose between the LTS (Long-Term Support) version, which is recommended for most users due to its stability, or the Current version, which includes the latest features but might be less stable.

  3. Download the archive:
    Click on the link for the desired version. This will download a compressed archive file (e.g., node-v20.10.0-linux-x64.tar.gz). You can download this file using your web browser or directly on the command line using wget:
    bash
    wget https://nodejs.org/dist/v20.10.0/node-v20.10.0-linux-x64.tar.gz

    (Replace v20.10.0 with the version you are downloading).

Extracting and Installing the Binaries

  1. Create an installation directory:
    It’s good practice to install Node.js in a dedicated location, such as /usr/local. Create a directory for Node.js if it doesn’t exist.

    sudo mkdir -p /usr/local/lib/nodejs
    
  2. Extract the archive:
    Navigate to the directory where you downloaded the archive (e.g., ~/Downloads) and extract its contents into the installation directory.

    cd ~/Downloads
    sudo tar -xzf node-v20.10.0-linux-x64.tar.gz -C /usr/local/lib/nodejs
    

    This extracts the Node.js binaries into /usr/local/lib/nodejs/node-v20.10.0-linux-x64.

  3. Create symbolic links:
    To make node and npm commands accessible from anywhere in your system, you need to create symbolic links to the executable files in a directory that is in your system’s PATH (e.g., /usr/local/bin).

    First, navigate into the extracted Node.js directory:

    cd /usr/local/lib/nodejs/node-v20.10.0-linux-x64/bin
    

    Then, create symbolic links:

    sudo ln -s /usr/local/lib/nodejs/node-v20.10.0-linux-x64/bin/node /usr/local/bin/node
    sudo ln -s /usr/local/lib/nodejs/node-v20.10.0-linux-x64/bin/npm /usr/local/bin/npm
    sudo ln -s /usr/local/lib/nodejs/node-v20.10.0-linux-x64/bin/npx /usr/local/bin/npx
    

    Ensure you replace node-v20.10.0-linux-x64 with the actual directory name created after extraction.

  4. Verify the installation:
    Close and reopen your terminal, or source your PATH if necessary, and then check the versions:
    bash
    node -v
    npm -v

Updating Node.js and npm

To update Node.js using this method, you would repeat the download and extraction process with the newer version, and then update the symbolic links to point to the new binaries. This manual process is one of the main reasons why NVM or distribution package managers are generally preferred.

Conclusion: Choosing the Right Installation Method

The installation of npm on Linux is a gateway to a vast and dynamic JavaScript ecosystem. For most users, Node Version Manager (NVM) stands out as the most flexible and recommended approach. It empowers developers to manage multiple Node.js versions concurrently, essential for working with projects that may have varying dependency requirements or for experimenting with the latest features without disrupting stable environments. NVM’s ability to install and switch versions without requiring sudo privileges further enhances its appeal, offering a cleaner and more secure development workflow.

For those who prefer simplicity and system integration, using the distribution’s package manager is a viable option, especially if you only need a single, stable version of Node.js and npm. Tools like apt, dnf, and pacman offer straightforward installation and update mechanisms, fitting seamlessly into the existing Linux environment. However, be mindful that these packages might not always be the most up-to-date. Utilizing NodeSource repositories can mitigate this by providing more recent versions through the familiar package management interface.

The manual installation of official binary distributions provides the most granular control and is useful in specific scenarios, such as in highly restricted environments or for precise version management. However, it demands more manual effort for updates and system-wide accessibility.

Ultimately, the “best” method is subjective and depends on your individual needs, comfort level with the command line, and project requirements. Regardless of the path you choose, ensuring Node.js and npm are correctly installed and configured is a fundamental step toward building robust and scalable JavaScript 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