A Step-by-Step Guide to Installing Node.js on Ubuntu 24.04 LTS

Step-by-step guide to installing Node.js & npm on Ubuntu 24.04 LTS using NVM. Covers prerequisites, system updates, build tools, NVM setup, and Node installation.

A Step-by-Step Guide to Installing Node.js on Ubuntu 24.04 LTS

Node.js is a powerful open-source, cross-platform JavaScript runtime environment that allows developers to build server-side and networking applications. It executes JavaScript code outside of a web browser. Node.js comes with npm (Node Package Manager), which is the default package manager for Node.js and the largest ecosystem of open-source libraries in the world.

Running Node.js on Ubuntu 24.04 LTS (Long-Term Support) provides a stable and reliable platform for development and deployment. While Node.js can be installed directly from Ubuntu's default repositories, using a version manager like NVM (Node Version Manager) is highly recommended. NVM allows you to easily install and manage multiple versions of Node.js on the same system, which is beneficial for working on different projects with varying version requirements.

This guide provides detailed steps to install Node.js and npm on your Ubuntu 24.04 LTS system using NVM.

System Requirements (Prerequisites)

Before starting the installation, please verify that your system meets these requirements:

  • Ubuntu 24.04 LTS Operating System: You need a working installation of Ubuntu 24.04 LTS.
  • Terminal Access: You need access to the command line interface (Terminal). You can open it by pressing Ctrl+Alt+T or searching for "Terminal" in the applications menu.
  • Sudo Privileges: Your user account must have sudo (administrator) privileges to install software packages.
  • Internet Connection: An active internet connection is required to download NVM and Node.js.
  • Basic Command-Line Familiarity: A basic understanding of how to use the Linux terminal is helpful.

Step 1: Update Your System's Package List

It's always a good practice to update your system's package list before installing new software to ensure you have the latest information about available packages and their dependencies.

  • Open your Terminal.

Run the following command:

sudo apt update

Enter your password when prompted. This command refreshes the list of available packages from the repositories configured on your system.

Step 2: Install Necessary Build Tools

NVM sometimes needs to compile Node.js from source, especially for specific versions. Installing essential build tools ensures this process runs smoothly.

In the same Terminal window, run this command to install build-essential (which includes tools like GCC, G++, make) and libssl-dev (for SSL support), along with curl (for downloading the NVM script):

sudo apt install build-essential libssl-dev curl -y

The -y flag automatically confirms the installation.

Step 3: Install NVM (Node Version Manager)

NVM is a script that manages multiple active Node.js versions. We'll download and execute the official installation script.

Visit the official NVM GitHub repository to find the latest version number for the installation command. As of writing, the command often looks similar to this, but please check the NVM repository for the most current command:

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

(Replace v0.39.7 with the latest version tag found on the NVM GitHub page if necessary).This command uses curl to download the install.sh script and then pipes it directly to bash to execute it. The script installs NVM into the ~/.nvm directory and adds lines to your shell profile (~/.bashrc, ~/.zshrc, etc.) to load NVM automatically.

Step 4: Load NVM into Your Current Session

The installation script modifies your shell configuration file, but these changes won't take effect until you reload the configuration or open a new terminal window.

  • Alternatively, you can simply close your current Terminal window and open a new one.

Verify that NVM was installed correctly by running:

command -v nvm

If the installation was successful, this command should output nvm. If it doesn't output anything or shows an error, double-check the previous steps, especially ensuring the lines were added correctly to your ~/.bashrc file.

To apply the changes immediately in your current terminal session, run the following command (use ~/.zshrc if you use Zsh):

source ~/.bashrc

Step 5: Install Node.js Using NVM

Now that NVM is installed and loaded, you can use it to install Node.js. It's generally recommended to install the latest LTS (Long-Term Support) version for stability.

(Optional) If you need a specific version of Node.js (e.g., version 20.10.0), you can install it using:

nvm install 20.10.0

(Replace 20.10.0 with the desired version number).

To install the latest LTS version of Node.js, run:

nvm install --lts

NVM will download, compile (if necessary), and install the latest LTS release. It will also automatically install the compatible version of npm.

To list all available Node.js versions you can install, run:

nvm ls-remote

This will show a long list of versions.

When you install a Node.js version with NVM, it's automatically used in the current terminal session. However, to make a specific version the default for new terminal sessions, you can set an alias.

If you installed a specific version (e.g., 20.10.0) and want to make that the default, use:

nvm alias default 20.10.0

Now, whenever you open a new terminal, this default version of Node.js will be active. You can switch between installed versions for a single session using nvm use <version> (e.g., nvm use 18.17.1).

To set the newly installed LTS version as the default, run:

nvm alias default lts

Step 7: Verify the Node.js and npm Installation

Finally, let's confirm that Node.js and npm are installed and accessible.

(Optional) Run a simple Node.js command to ensure it executes:

node -e "console.log('Hello from Node.js on Ubuntu 24.04!');"

If successful, you will see the message "Hello from Node.js on Ubuntu 24.04!" printed in your terminal.

Check the installed npm version:

npm -v

This should output the corresponding npm version (e.g., 10.2.4).

Check the installed Node.js version:

node -v

This should output the version number you installed (e.g., v20.11.1).

Installation Complete

You have now successfully installed Node.js and npm on your Ubuntu 24.04 LTS system using NVM. This setup allows you to manage multiple Node.js versions effectively, providing flexibility for your development projects. You are now ready to start building applications with Node.js!

Further Reading & References

For more detailed information, you may find these official resources helpful: