How to Install Node.js on Ubuntu

Node.js is a powerful, open-source, cross-platform JavaScript runtime environment that allows developers to build a wide range of applications, from server-side web applications to desktop tools and even mobile apps. Ubuntu, a popular Linux distribution, offers a robust platform for Node.js development. Installing Node.js on Ubuntu is a straightforward process, and this guide will walk you through the most common and recommended methods, ensuring you have a flexible and up-to-date installation for your projects.

This guide focuses on providing the most efficient and secure ways to install Node.js, covering methods that cater to both beginners and experienced developers who need precise control over their Node.js versions. We will explore using the default Ubuntu repositories, the NodeSource PPA for more recent versions, and the Node Version Manager (NVM) for managing multiple Node.js installations.

Method 1: Installing Node.js from Ubuntu’s Default Repository

The most accessible way to install Node.js on Ubuntu is by using the package manager, apt. This method is convenient for quick setups or when the latest features of Node.js are not a strict requirement. Ubuntu’s default repositories often contain a stable, albeit sometimes older, version of Node.js.

Updating Package Lists

Before installing any new software, it’s always a good practice to update your system’s package lists. This ensures that you are downloading the latest available versions of packages from the repositories.

Open your terminal and execute the following command:

sudo apt update

This command fetches the latest package information from all configured sources.

Installing Node.js and npm

Once the package lists are updated, you can install Node.js. The Node.js package in Ubuntu’s default repositories usually includes npm (Node Package Manager) as well, which is essential for managing Node.js libraries and dependencies.

To install Node.js, run:

sudo apt install nodejs

After the installation is complete, you can verify the installed versions of Node.js and npm by running:

node -v
npm -v

This will display the version numbers of Node.js and npm currently installed on your system. If you find that the version is too old for your specific development needs, you might consider alternative installation methods detailed below.

Installing npm Separately (If Needed)

In some rare cases, the nodejs package might not include npm, or you might need a specific version of npm. You can install npm separately using the following command:

sudo apt install npm

Again, verify the installation with npm -v.

Considerations for Default Repository Installation

  • Pros: Simple, quick, and integrates well with the Ubuntu package management system.
  • Cons: The Node.js version available might be outdated compared to the latest stable or LTS (Long-Term Support) releases. This can limit access to new features and security patches.

Method 2: Installing Node.js with NodeSource PPA

For developers who require more recent versions of Node.js than what is available in Ubuntu’s default repositories, using a Personal Package Archive (PPA) maintained by NodeSource is highly recommended. NodeSource provides up-to-date Node.js packages for various Ubuntu releases.

This method ensures you get access to the latest features, performance improvements, and security updates.

Adding the NodeSource PPA

First, you need to add the NodeSource PPA to your system. The specific PPA you add depends on the Node.js version you wish to install. NodeSource typically provides PPAs for current and LTS versions.

For example, to install Node.js version 20.x (a recent LTS version as of late 2023/early 2024), you would use the following commands. Always check the official NodeSource GitHub repository for the most current PPA setup scripts.

  1. Download and execute the NodeSource setup script:

    You’ll typically need curl to fetch the script. If you don’t have it, install it with sudo apt install curl.

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

    Note: Replace setup_20.x with the desired major version (e.g., setup_18.x for Node.js 18, or setup_lts.x for the latest LTS).

    This script will add the NodeSource repository to your system’s software sources and update your package lists automatically.

Installing Node.js from the NodeSource PPA

Once the PPA has been successfully added and your package lists are updated, you can install Node.js using apt:

sudo apt install nodejs

This command will install the Node.js package from the newly added NodeSource repository, ensuring you get the version specified by the setup script.

Verifying the Installation

After the installation completes, verify the installed versions:

node -v
npm -v

You should see the version number corresponding to the NodeSource repository you chose.

Installing Development Tools (Optional but Recommended)

To compile native add-ons from npm, you might need to install build essentials:

sudo apt install build-essential

This package group includes compilers and development libraries necessary for compiling C/C++ code, which is often required by some npm packages.

Considerations for NodeSource PPA Installation

  • Pros: Provides access to recent and LTS versions of Node.js. Integrates with apt for easy updates.
  • Cons: Requires adding a third-party repository, which should be done with caution and by trusting the source.

Method 3: Installing Node.js with Node Version Manager (NVM)

For developers who work on multiple projects that may require different Node.js versions, or who frequently need to switch between versions, the Node Version Manager (NVM) is an indispensable tool. NVM allows you to install, manage, and switch between multiple Node.js versions easily without interfering with system-wide installations.

NVM is a script that installs Node.js to your user directory, making it a user-specific installation rather than a system-wide one.

Installing NVM

NVM is typically installed via a curl or wget script. It’s recommended to check the official NVM GitHub repository for the most up-to-date installation instructions, as script URLs can change.

  1. Download and run the NVM installation script:

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

    Note: Replace v0.39.7 with the latest stable version of NVM. Always check the NVM GitHub page for the current version.

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

  2. Source your shell profile:

    After the installation script finishes, you need to reload your shell configuration for NVM to be available. You can do this by closing and reopening your terminal, or by running:

    source ~/.bashrc  # Or source ~/.zshrc, depending on your shell
    
  3. Verify NVM installation:

    To confirm that NVM has been installed correctly, run:

    command -v nvm
    

    This command should output nvm if it’s installed and recognized by your shell.

Installing Node.js Versions with NVM

Now that NVM is installed, you can use it to install different Node.js versions.

  1. List available Node.js versions:

    To see all the versions of Node.js available for installation, use:

    nvm ls-remote
    
  2. Install a specific Node.js version:

    To install a specific version (e.g., Node.js 18.17.0), use the install command:

    nvm install 18.17.0
    

    NVM will download, compile (if necessary), and install the specified Node.js version. It will also install the corresponding npm version.

  3. Install the latest LTS version:

    A common practice is to install the latest Long-Term Support (LTS) version:

    nvm install --lts
    
  4. Install the latest current version:

    If you need the very latest features, you can install the latest release:

    nvm install node
    

Managing Node.js Versions with NVM

Once you have multiple Node.js versions installed, NVM makes switching between them simple.

  1. List installed Node.js versions:

    To see which versions you have installed locally:

    nvm ls
    
  2. Use a specific Node.js version:

    To switch to a particular version for your current shell session:

    nvm use 18.17.0
    
  3. Set a default Node.js version:

    To ensure a specific version is used whenever you open a new terminal, set it as the default:

    nvm alias default 18.17.0 # Or nvm alias default node for the latest installed
    

Verifying Node.js and npm with NVM

After selecting a version with nvm use, you can verify the installed Node.js and npm versions:

node -v
npm -v

Considerations for NVM Installation

  • Pros: Highly flexible for managing multiple Node.js environments. Allows easy switching between versions for different projects. Doesn’t require root privileges for installation or switching.
  • Cons: Requires a bit more initial setup compared to apt. Each project might need to have its Node.js version explicitly selected or managed.

Conclusion

Installing Node.js on Ubuntu can be achieved through several reliable methods, each offering distinct advantages. For a quick setup with potentially older versions, the default Ubuntu repository (apt install nodejs) is a straightforward option. When you need more recent features and security updates, the NodeSource PPA provides a robust way to keep your Node.js installation current and integrated with apt. For developers who require the utmost flexibility to manage and switch between multiple Node.js versions across different projects, NVM stands out as the most powerful and recommended solution.

Choosing the right installation method depends on your specific project requirements, your familiarity with the command line, and your need for version control. By understanding these options, you can confidently set up Node.js on your Ubuntu system and begin building your next innovative application. Remember to always refer to the official documentation for the most current commands and version information.

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