How to Install Node.js and npm on Ubuntu 22.04

Introduction

Node.js is a popular JavaScript runtime used for building scalable web applications, APIs, and microservices. It is widely used by developers and DevOps engineers to run backend services. Along with npm (Node Package Manager), you can easily manage libraries and tools for your projects.

In this step-by-step guide, you will learn how to install Node.js and npm on Ubuntu 22.04 LTS.

Step 1: Update Your System

Before installing Node.js, update your system packages to the latest version:

sudo apt update && sudo apt upgrade -y

Step 2: Install Node.js from Default Ubuntu Repository

The simplest way is to use Ubuntu’s default repository:

sudo apt install nodejs -y
sudo apt install npm -y

This will install Node.js and npm, but the version may not be the latest.

Step 3: Install Latest Node.js Using NodeSource (Recommended)

For developers who want the latest stable release of Node.js, use NodeSource repository:

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs

This installs the latest Node.js (v18.x at the time of writing) and npm.

Step 4: Verify Installation

Check the installed versions:

node -v
npm -v

Step 5: Install Build Tools (Optional)

Some npm packages require build tools for compiling source code. Install them with:

sudo apt install build-essential -y

Step 6: Manage Node.js Versions with NVM (Optional but Useful)

Using NVM (Node Version Manager) gives you flexibility to switch between different Node.js versions:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
source ~/.bashrc
nvm install 18
nvm use 18

FAQs

  • Q: Can I install multiple versions of Node.js?
    A: Yes, with NVM you can install and switch between different Node.js versions.
  • Q: Do I need npm separately?
    A: No, npm is installed automatically with Node.js.
  • Q: Which method should I use?
    A: Use NodeSource or NVM for the latest features and stability.

Conclusion

You have successfully installed Node.js and npm on Ubuntu 22.04. Whether you are a developer building web apps or a DevOps engineer setting up production servers, Node.js is an essential tool in modern development. Using NodeSource or NVM ensures you stay up-to-date with the latest releases.

Leave a Reply

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