How to Install Node.js on Ubuntu: Complete Guide for Beginners and Developers

目次

1. Introduction: Why Use Node.js on Ubuntu?

Why Ubuntu and Node.js Are a Great Match

Node.js is a platform for running JavaScript on the server side and is widely used for web application and tool development. Meanwhile, Ubuntu is a Linux distribution highly favored by developers and server administrators. By combining the two, you can create a development environment that excels in stability, flexibility, and speed.

Installing Node.js on Ubuntu offers benefits such as:

  • The OS is lightweight with minimal bloat, enabling efficient resource usage
  • Linux-based environments can be unified from development to production
  • Excellent compatibility with Node.js and smooth integration with tools like npm and nvm

For these reasons, the combination of Ubuntu and Node.js is highly attractive for both front-end and back-end development environments.

Who Should Read This Article?

This article is for people who:

  • Are using Node.js on Ubuntu for the first time
  • Want to know the best installation method
  • Want to use the latest version of Node.js but are unsure about the setup

By reading this guide, you’ll be able to compare three different methods of installing Node.js on Ubuntu and choose the best one for your goals and skill level. We’ll also cover how to install related tools like npm and yarn, plus solutions for common errors, so you can confidently manage your Node.js environment.

2. Overview: Comparing Node.js Installation Methods on Ubuntu

Three Main Installation Methods—Each with Its Own Advantages

There are three main ways to install Node.js on Ubuntu:

  1. Standard package installation via APT (Advanced Package Tool)
  2. Installation using the NodeSource PPA (Personal Package Archive)
  3. Flexible version management with nvm (Node Version Manager)

Each method has its pros and cons, and the best choice depends on your goals and system setup. Here is a comparison table summarizing their features:

Comparison Table: Node.js Installation Methods

Installation MethodMain FeaturesProsConsBest For
APT (Standard)Uses Ubuntu’s official repositorySimple & safeMay be outdatedBeginners who want to try it quickly
NodeSource PPALets you manage the latest Node.js via APTSupports relatively new versionsRequires adding a PPADevelopers who want a stable release
nvmAllows switching between multiple versionsFlexible, ideal for global setupsRequires shell configurationRecommended for most learning & development use cases

Why Is Comparison Important?

While Ubuntu is a highly stable OS, software in the APT repository tends to lag behind the latest releases. So, if you want to use the latest Node.js features or try multiple versions, alternatives to APT are better.

On the other hand, if you “just want to try it quickly” or “don’t want to make too many changes to a production server,” the APT method can be good enough.

Which Method Should You Choose?

In conclusion, for developers or anyone planning to use Node.js long-term, installing via nvm is most recommended.
Reasons include:

  • Easily switch between the latest and older versions
  • npm is included automatically
  • Less hassle with permissions (sudo not required)

3. Method 1: Easy Node.js Installation via APT (Official Ubuntu Method)

What Is APT? Standard Package Management for Ubuntu

APT (Advanced Package Tool) is the standard package management system for Ubuntu and other Debian-based Linux distributions. With APT, you can easily install, update, and remove software via command line.

The official Ubuntu repositories include Node.js packages, so you can install Node.js without any special preparation—this is the main advantage of this method.

Installation Steps

  1. First, update the APT package list:
   sudo apt update
  1. Install Node.js and npm:
   sudo apt install nodejs npm
  1. Verify installation:
   node -v
   npm -v

If the version numbers are displayed, the installation was successful.

Pros: Extremely Easy and Safe

  • Highly reliable as it uses Ubuntu’s official repository
  • Simple commands, making it hard for beginners to get lost
  • Since it’s managed by APT, it integrates well with system updates

This is especially useful for “just trying out Node.js” or if you “don’t want to add extra configuration to a production server.”

Cons: Version May Be Outdated

Because APT prioritizes stability, the Node.js version provided can be several releases behind.

For example, on Ubuntu 22.04, the version installed via APT may be Node.js 12 or 14, missing the latest features and security updates.

Also, managing multiple versions is difficult, so this method is not suitable if you want to use different Node.js versions for different projects.

Who Should Use This Method?

  • Beginners who want to try Node.js right away
  • Those with stable business environments where the default version is sufficient
  • When you don’t need to switch between multiple versions

4. Method 2: Install the Latest Version Using NodeSource PPA

What Is NodeSource?

NodeSource is a highly trusted source that provides up-to-date stable and latest versions of Node.js, independently from the official Node.js team. It’s especially useful for Ubuntu and Debian users who want the latest Node.js via APT.

NodeSource is also recommended by the official Node.js website and is widely used in enterprise environments.

Installation Steps (Example: Node.js 18.x)

  1. Update the package list:
   sudo apt update
  1. If curl is not installed, install it first:
   sudo apt install curl
  1. Run the NodeSource setup script:
   curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
  1. Install Node.js:
   sudo apt install -y nodejs
  1. Verify installation:
   node -v
   npm -v

Pros: Get the Latest Version Using Familiar APT Workflow

  • Install the latest stable Node.js easily
  • Still managed by APT, so the workflow remains familiar
  • npm is also installed at the same time

Ideal if you want a more up-to-date environment than what Ubuntu packages provide.

Cons: Requires Adding a PPA

  • Slightly more steps than APT alone, which might feel tricky for some beginners
  • You need to be sure it’s a trusted source for security reasons (NodeSource is safe)

Who Should Use This Method?

  • Developers who want a stable, up-to-date Node.js
  • Users who find the default Ubuntu repository insufficient, but don’t want to use nvm
  • Those who prefer managing everything through APT

5. Method 3: Flexible Version Management with nvm (Recommended)

What Is nvm? Easily Switch Between Node.js Versions

nvm (Node Version Manager) is a command-line tool that lets you manage and switch between multiple versions of Node.js. It’s extremely convenient for developers who need different Node.js versions for different projects, or those who want to try both the latest and LTS versions.

With nvm, you can create a flexible development environment isolated to your user account without installing Node.js system-wide.

How to Install nvm

  1. Run the install script with curl:
   curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
  1. Reload your shell configuration file (varies depending on your shell):
   source ~/.bashrc

Or for Zsh:

   source ~/.zshrc
  1. Verify that nvm is installed:
   command -v nvm

If “nvm” is displayed, it’s set up correctly.

Installing Node.js with nvm

  1. To install the LTS (Long Term Support) version:
   nvm install --lts
  1. To install a specific version:
   nvm install 18
  1. Switch to the desired version:
   nvm use 18
  1. Set the default version:
   nvm alias default 18
  1. Check the version:
   node -v
   npm -v

Pros: Unmatched Flexibility and Control

  • Coexist and instantly switch between multiple Node.js versions
  • npm is installed automatically
  • Since sudo isn’t needed, avoids permissions issues
  • Keeps your system clean—ideal for development environments

Cons: Initial Setup Requires Attention

  • If you forget to reload .bashrc or .zshrc, nvm won’t be available
  • Note: nvm installs Node.js per user, not system-wide

Who Should Use This Method?

  • Developers who want to switch between Node.js versions
  • Anyone who needs different versions for different projects
  • Even beginners can benefit by avoiding permission errors

6. How to Use and Install npm and yarn

What Is npm? Essential for Node.js Development

npm (Node Package Manager) is the package manager for Node.js, allowing you to easily install and manage libraries and tools published by developers worldwide. If you’re developing with Node.js, npm is essential.

When you install Node.js via APT or NodeSource, npm is usually installed alongside it. With nvm, npm is included automatically when you run nvm install.

Check npm Version:
npm -v

Basic npm Usage

ActionExample Command
Install a packagenpm install <package-name>
Install globallynpm install -g <package-name>
Uninstall a packagenpm uninstall <package-name>
Initialize a projectnpm init or npm init -y
List installed packagesnpm list or npm list -g

npm is especially important for managing project-specific dependencies.

What Is yarn? A Popular Alternative to npm

yarn is a package manager developed by Facebook as an alternative to npm, aiming for faster and more reliable package management. The commands are almost fully compatible with npm, so you can do almost everything with yarn that you can do with npm.

How to Install yarn (via npm)

npm install -g yarn

Once installed, check the version:

yarn -v

Basic yarn Usage

ActionExample Command
Install a packageyarn add <package-name>
Install globallyyarn global add <package-name>
Uninstall a packageyarn remove <package-name>
Initialize a projectyarn init
List installed packagesyarn list or yarn global list

npm vs. yarn: Which Should You Use?

Comparisonnpmyarn
Standard InclusionComes standard with Node.jsRequires separate installation
SpeedAverageFaster with caching
Lock Filepackage-lock.jsonyarn.lock
Command CompatibilityMostly compatible (but check docs)

Modern npm (v7 and later) has improved a lot, so there’s little difference between them. It’s generally safest to follow whatever your project or team is already using.

7. Common Errors and Troubleshooting

Common Errors and Solutions

node: command not found

Cause:
Node.js is either not installed properly or the path isn’t set. This often happens if your shell config isn’t updated after installing nvm.

Solution:

  • Reload .bashrc or .zshrc to enable nvm
  source ~/.bashrc
  • If that doesn’t work, try logging out and back in, or rebooting

E: Unable to locate package nodejs

Cause:
The APT package list is outdated, or the correct PPA isn’t added.

Solution:

  • Update the APT list
  sudo apt update
  • If using NodeSource, rerun the setup script

npm ERR! permission denied

Cause:
You tried to install a global npm package without sufficient permissions.

Solution:

  • Install with sudo (not always recommended)
  sudo npm install -g <package-name>
  • Using nvm avoids the need for sudo and is the best practice

nvm: command not found

Cause:
nvm is installed, but the shell configuration isn’t loaded.

Solution:

  • Check if your shell config (like .bashrc) includes nvm setup
  • Load it manually if needed
  export NVM_DIR="$HOME/.nvm"
  [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"

Best Practices for Avoiding Troubles

  • Get into the habit of checking your PATH settings
  • Use nvm to avoid most environment-related problems
  • Be mindful of the Ubuntu and Node.js version combination (older LTS releases may have outdated repositories)

8. Frequently Asked Questions (FAQ)

Q1. How do I check my Node.js version?

A. Just run the following command in your terminal:

node -v

To check your npm version as well, use:

npm -v

Q2. How can I use multiple Node.js versions on Ubuntu?

A. The easiest and safest way is to use nvm (Node Version Manager). After installing, you can switch versions like this:

nvm install 16
nvm use 16

This way, you can easily use different versions for different projects.

Q3. Node.js installed via nvm isn’t active. Why?

A. Usually, your shell configuration file wasn’t loaded after installation. Run:

source ~/.bashrc

Or, if you use zsh: source ~/.zshrc

Q4. What’s the difference between yarn and npm? Which should I use?

A. Both provide nearly the same functionality, but yarn excels in fast installs using caching and has explicit dependency management via yarn.lock. Modern npm (v7+) is much improved, so you can use either. It’s best to stick with what your project or team is already using.

Q5. How is Node.js different from Deno or Bun?

A. Deno and Bun are alternative JavaScript runtimes created to address some of Node.js’s limitations:

  • Deno: Supports TypeScript natively, is more secure, and has a built-in standard library
  • Bun: Much faster runtime and includes a built-in package manager

However, Node.js is currently the most practical choice due to its huge npm ecosystem.

9. Conclusion: Choose the Installation Method That Fits Your Needs

There are several ways to install Node.js on Ubuntu, each with clear pros and cons. This guide introduced three main methods, compared their features, and explained how to choose the right one for your use case.

Here’s a recap of the main points:

APT (Official Ubuntu)

  • Pros: Easiest and safest
  • Cons: May be outdated
  • Recommended for: Beginners who want to try it quickly

NodeSource PPA

  • Pros: Lets you use relatively new Node.js via APT
  • Cons: Requires adding a PPA
  • Recommended for: Developers who want a stable, up-to-date release

nvm (Node Version Manager)

  • Pros: Flexible version switching and high control
  • Cons: Initial setup is a bit more complex
  • Recommended for: Intermediate+ developers or those managing multiple projects

We also covered package managers like npm and yarn, and troubleshooting tips for common errors. Installing Node.js might seem challenging at first, but by choosing the right method, you can easily build a reliable development environment on Ubuntu.

If you want to grow your projects further, consider using nvm as your base, and try applying it to team development or CI/CD workflows.

侍エンジニア塾