Installing Node.js on Ubuntu: Two Methods Explained

Installing Node.js on Ubuntu: Two Methods Explained

Introduction

Node.js is a JavaScript runtime for server-side programming, enabling developers to build scalable backend functionality using familiar JavaScript from web development. Let’s begin the Node.js installation on Ubuntu guide.

Prerequisites:

  • Ubuntu 20.04 / 22.04 server set up.
  • Root user or non-root user account with sudo privileges.

Method 1: Installing Node.js with Apt from Default Repositories

Use the apt package manager to install the stable Node.js version.


sudo apt update
sudo apt install nodejs

Verify Node.js installation and check the version number.


node -v

Expected result (version may vary based on Ubuntu’s default software repository):


Output
v10.19.0

Install npm.


sudo apt install npm

Verify npm installation and check the version number.


npm -v

Expected result (version may vary based on Ubuntu’s default software repository):


Output
v6.14.4

Successfully installed Node.js and npm using apt from default Ubuntu software repositories.

Method 2: Installing Node.js Using Node Version Manager (NVM)

Node Version Manager allows installing from a list of versions, providing flexibility.

Install nvm.


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

Verify NVM installation and check the version.


nvm --version

Expected result:


Output
v0.39.3

List available versions of Node.js.


nvm list-remote

Look for the version to install, and copy the version number (e.g., v19.4.0).


nvm install v19.4.0

View different versions installed.


nvm ls

Switch between installed versions with nvm use.


nvm use v18.12.0

Conclusion

Choose apt for simplicity and stability or NVM for versatile Node.js version management. NVM allows easy switching between different Node.js versions, making it a preferred choice for projects with specific version requirements.

That’s it! You have completed the Node.js installation on Ubuntu guide.

Thank you for exploring FastDT. Explore our range of services to enhance your business.

Learn more about our services