Node.js & npm on Ubuntu EC2

Beginner
8 min read
Updated: Nov 2026
5 examples
Ubuntu / apt / n

Definition and Usage

On a fresh Ubuntu instance in Amazon EC2, you install Node.js and npm before deploying an app. This guide starts with Ubuntu’s apt packages, then upgrades to a supported release line with the n version manager.

What does installing Node on EC2 mean?

You SSH into the instance, refresh package metadata, install the runtime and package manager, verify node -v / npm -v, and optionally replace the distro binary with a current LTS build under /usr/local.

When should you use apt plus n?

Use this path for a single-app Ubuntu lab or small production host. Teams that need per-user installs often prefer nvm; some shops pin packages with a NodeSource apt repository instead of n.

What you will configure

An updated apt index, the nodejs and npm packages, a clean response to the optional outdated-libraries dialog, verified versions, and an LTS switch via sudo n lts (or a pinned major).

After Node works

Upload project files with FileZilla (or git), then follow Deploy Node.js on EC2 for process managers and ports.

Pick a supported line.

Check nodejs.org for the current Active LTS. Prefer sudo n lts unless you must pin a major for compatibility testing.

One installer per instance.

Mixing apt Node, n, nvm, and NodeSource on the same VM often leaves conflicting binaries on PATH. Pick one approach and document it.

npm ships with Node.

After n installs a newer line, run hash -r or open a new shell if which node still points at an old binary.

Core Concepts

Terms you will use while installing the runtime:

ConceptMeaning
aptUbuntu package manager; installs distribution Node/npm builds
nodejs / npmPackage names for the runtime and Node package manager
nCLI that downloads Node binaries and switches the active version under /usr/local
LTSLong-term support Node line recommended for most servers
needrestartDialog warning that daemons may still use outdated libraries after apt
hash -rClears the shell’s cached command paths after switching binaries
/usr/bin vs /usr/local/binapt vs n install locations; PATH order decides which node wins
ubuntu userDefault SSH user on official Ubuntu AMIs; runs sudo for installs

Prerequisites

Have these ready before you run apt:

  • An AWS account with access to EC2 and a running Ubuntu EC2 instance.
  • SSH access—for example with PuTTY on Windows or ssh from macOS/Linux—as a user that can run sudo (often ubuntu).
  • Outbound HTTPS from the instance so apt and n can download packages and Node binaries.
EC2 instances list with instance in running state
Amazon EC2

Examples

Update apt, install Node and npm, clear the libraries dialog, verify versions, then upgrade with n.

Example 1 — Update Package Metadata

Refresh the local apt index so installs resolve to current repository metadata.

  1. SSH into the instance as ubuntu (or your AMI user).
  2. Run the update command below.
Terminal (EC2)
sudo apt update

How It Works

  1. apt update refreshes package lists; it does not upgrade installed packages yet.
  2. Network access to Ubuntu mirrors is required from the instance.
  3. sudo elevates privileges for system package operations.
  4. Run this first so later installs do not pull stale metadata.

Example 2 — Install Node.js and npm

Install the Ubuntu distribution packages for the runtime and package manager.

  1. Install both packages in one command (names may vary slightly by Ubuntu release):
Terminal (EC2)
sudo apt install -y nodejs npm

How It Works

  1. -y answers yes to apt prompts so the install can finish non-interactively where possible.
  2. Distro Node is stable for the release, but often several majors behind upstream.
  3. npm usually ships with the nodejs package set on Ubuntu.
  4. Next step may show a libraries dialog—continue to Example 3.

Example 3 — Handle Outdated Libraries Dialog

Confirm or dismiss the needrestart-style prompt if it appears during dependency installation.

  1. If a dialog states that daemons are using outdated libraries, press Enter or choose OK on an interactive SSH session.
  2. Wait until apt finishes without errors.
Ubuntu dialog about daemons using outdated libraries during apt
Ubuntu

How It Works

  1. needrestart warns that long-running services may still link old libraries after upgrades.
  2. On a fresh lab instance, accepting the default is usually fine.
  3. Unrelated to Node—the prompt is about system libraries, not the Node binary itself.
  4. Unattended hosts may need needrestart config so apt does not hang on a TTY prompt.

Example 4 — Verify node and npm Versions

Confirm the login user’s PATH resolves the newly installed binaries.

  1. Check versions without sudo:
Terminal (EC2)
node -v
npm -v
Terminal showing node and npm versions after apt install
Ubuntu

How It Works

  1. No sudo for version checks—you want the same user that will run the app.
  2. Old majors here are expected from apt; upgrade next if your framework needs a newer line.
  3. command not found means PATH or install failed; re-run the apt install and check for errors.
  4. Record the versions before upgrading so you can compare after n.

Example 5 — Upgrade to Current Node with n

Install n globally, then switch to LTS (or pin a major).

  1. Install n, then install a newer Node line. Prefer LTS:
Terminal (EC2)
sudo npm install -g n
sudo n lts

To pin a major instead (for example Node 22.x), run sudo n 22 after checking nodejs.org.

Terminal showing upgraded Node.js version after running n
Ubuntu
  1. Open a new SSH session (or run hash -r) and verify again with node -v and npm -v.
  2. Next: upload files with FileZilla, or deploy with Deploy Node.js on EC2. Prefer one installer per host; see also fnm if you want a fast per-user binary manager instead of n.

How It Works

  1. n downloads official Node binaries and places them under /usr/local.
  2. sudo n lts tracks the current Long Term Support line.
  3. hash -r / new shell clears a cached path to the old apt binary.
  4. Avoid mixing tools—do not also install nvm or NodeSource on the same PATH without a clear plan.

Edge Cases & Pitfalls

Most install surprises are PATH conflicts, old apt majors, or hung apt dialogs.

path

Still seeing the apt Node after n

Run hash -r or reconnect SSH. Check which node—/usr/local/bin/node should win over /usr/bin/node.

version

Framework rejects the apt major

Upgrade with n (or your chosen manager) to an LTS that matches the framework docs before npm install.

dialog

apt hangs on outdated libraries

You need an interactive TTY to dismiss the prompt, or configure needrestart for non-interactive upgrades.

mix

Multiple Node managers installed

nvm, n, and NodeSource together create confusing PATH winners. Uninstall extras or use a clean instance.

sudo

Using sudo node -v for checks

Root’s PATH can differ from the deploy user. Check versions as the same user that will run PM2 or systemd.

network

n cannot download binaries

Confirm security group / NACL / proxy allow outbound HTTPS to nodejs.org (or the CDN n uses).

Key Takeaways

  • apt first: stable baseline; often an older major than nodejs.org.
  • Upgrade with n: sudo n lts (or a pinned major) for frameworks that need a current line.
  • One approach per host: avoid mixing n, nvm, and NodeSource on the same PATH.
  • Next: FileZilla (or git) for files, then Deploy Node.js on EC2 for the app process.

One line: apt install → verify → n lts → new shell → deploy.

Frequently Asked Questions

Ubuntu and Debian freeze package versions for stability and security backports. That Node build is supported for the distribution, but it often lags the current Node.js release line. Tools like n, NodeSource setup scripts, or nvm install newer majors when your app needs them.
No. Version checks should run as your normal user: node -v and npm -v. Use sudo only when installing system-wide packages with apt or when n writes under /usr/local.
During apt upgrades, services linked to old library versions may need a restart. On an interactive SSH session you can press Enter or OK to continue; on unattended systems consider needrestart configuration. It is unrelated to Node itself.
n installs system-wide under /usr/local, which is simple for a single-app EC2. nvm is user-scoped and popular for development machines. Either is fine if you stay consistent and document which user runs your process manager.
Prefer the current Active LTS from nodejs.org unless your framework documents a required major. Pinning a major (for example sudo n 22) is useful for reproducible lab images; sudo n lts tracks the latest LTS.
Your shell may have cached the old path. Run hash -r or open a new SSH session, then check node -v and which node again. Confirm /usr/local/bin appears before /usr/bin in PATH.

Next: SFTP with FileZilla

With Node installed, copy application files from your laptop using the same SSH endpoint and key.

FileZilla and EC2 →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful