How to Install Node.js on Ubuntu: Step-by-Step Tutorial for Beginners

Written by

in

TL;DR: Install the NodeSource setup script to add the official Node.js repository to your system. Then, run the standard APT package manager commands to download and install the latest stable version of Node.js and npm.

Introduction

Node.js has become the backbone of modern server-side JavaScript development. For developers using Ubuntu, installing Node.js is a straightforward process, but using the official repositories ensures you always have access to the latest stable features and security patches. This guide walks you through the most reliable method for installation, avoiding common pitfalls associated with older package versions found in the default Ubuntu repositories.

If you want to dig deeper, check out our guide on Personalized Longevity Clinics: Preventative Aging Intervent.

Prerequisites

Before you begin, ensure you have a standard Ubuntu installation. You will need access to the terminal. If you are not using the root user, you will need to use the sudo command to execute administrative tasks. It is also recommended that your system is up to date. Open your terminal and run sudo apt update followed by sudo apt upgrade -y to ensure all existing packages are current. This minimizes the risk of dependency conflicts during the installation process.

Step 1: Add the NodeSource Repository

The default Ubuntu repositories often contain outdated versions of Node.js. To get the latest version, we use the NodeSource setup script. This script handles the complex work of adding the correct repository keys and sources for your specific Ubuntu version. Open your terminal and paste the following command. Replace 18.x with the major version number you wish to install, such as 20.x for Node 20. The command is: curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -. This command downloads the setup script and executes it immediately. It may take a few moments to complete. You will see various logs scrolling by as the system configures the new repository.

Step 2: Install Node.js

Once the setup script has finished executing successfully, your system is now ready to install Node.js from the new repository. Simply type the following command into your terminal: sudo apt install nodejs -y. The package manager will download the Node.js binary files and any necessary dependencies. The -y flag automatically confirms the installation prompt, allowing the process to proceed without manual intervention. This step usually takes less than a minute, depending on your internet connection speed.

Step 3: Verify the Installation

After the installation completes, you must verify that Node.js is correctly installed and accessible from your command line. Type node -v and press Enter. This command will output the installed version number, such as v18.17.0. Similarly, verify that npm, the Node Package Manager, is installed by typing npm -v. If both commands return version numbers, you have successfully installed Node.js. If you receive a “command not found” error, restart your terminal session to refresh the environment variables.

Pro Tips for Beginners

First, always check which version of Node.js your project requires. You can view the required version in the package.json file or by checking the project’s README. If you need multiple versions of Node.js for different projects, consider installing nvm (Node Version Manager) instead of the system-wide installation. nvm allows you to switch between versions easily without affecting the global system configuration. Second, keep your system updated. Regularly running sudo apt update and sudo apt upgrade ensures that your Node.js installation remains secure and bug-free.

Common Troubleshooting

If the installation fails, check your internet connection and ensure your terminal has the necessary permissions. If you encounter permission errors, try running the terminal as a root user using

Related Articles

Comments

One response to “How to Install Node.js on Ubuntu: Step-by-Step Tutorial for Beginners”

  1. […] If you want to dig deeper, check out our guide on How to Install Node.js on Ubuntu: Step-by-Step Tutorial for . […]

Leave a Reply

Your email address will not be published. Required fields are marked *