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).
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.
Amazon EC2
Walkthrough
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.
SSH into the instance as ubuntu (or your AMI user).
Run the update command below.
Terminal (EC2)
sudo apt update
How It Works
apt update refreshes package lists; it does not upgrade installed packages yet.
Network access to Ubuntu mirrors is required from the instance.
sudo elevates privileges for system package operations.
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.
Install both packages in one command (names may vary slightly by Ubuntu release):
Terminal (EC2)
sudo apt install -y nodejs npm
How It Works
-y answers yes to apt prompts so the install can finish non-interactively where possible.
Distro Node is stable for the release, but often several majors behind upstream.
npm usually ships with the nodejs package set on Ubuntu.
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.
If a dialog states that daemons are using outdated libraries, press Enter or choose OK on an interactive SSH session.
Wait until apt finishes without errors.
Ubuntu
How It Works
needrestart warns that long-running services may still link old libraries after upgrades.
On a fresh lab instance, accepting the default is usually fine.
Unrelated to Node—the prompt is about system libraries, not the Node binary itself.
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.
Check versions withoutsudo:
Terminal (EC2)
node -v
npm -v
Ubuntu
How It Works
No sudo for version checks—you want the same user that will run the app.
Old majors here are expected from apt; upgrade next if your framework needs a newer line.
command not found means PATH or install failed; re-run the apt install and check for errors.
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).
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.
Ubuntu
Open a new SSH session (or run hash -r) and verify again with node -v and npm -v.
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
n downloads official Node binaries and places them under /usr/local.
sudo n lts tracks the current Long Term Support line.
hash -r / new shell clears a cached path to the old apt binary.
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).
Remember
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.