AWS EC2
NPM
Installation Guide
Cloud Computing
Node.js

Installing NPM on AWS EC2

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Installing npm on an EC2 instance is really the same job as installing Node.js correctly. npm ships with Node.js, so the reliable path is to choose an installation method that matches how you will operate the instance afterward.

Choose the Installation Strategy

For most application servers, a user-level install with nvm is the safest option. It avoids fighting with old distribution packages, makes upgrades easy, and lets you pin an LTS release per machine or per user.

AWS documents an nvm-based setup for Amazon Linux instances. That is a good default because EC2 images vary, while nvm gives you a consistent Node.js workflow.

Before you install anything, connect to the instance with SSH. The login user depends on the AMI:

  • Amazon Linux: ec2-user
  • Ubuntu: ubuntu

Example:

bash
ssh -i my-key.pem [email protected]

Make sure the instance security group allows SSH on port 22, and confirm the machine already has outbound internet access. Without that, package downloads will fail.

Install Node.js and npm with nvm

On Amazon Linux, an nvm install is a clean starting point:

bash
1curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
2source ~/.bashrc
3nvm install --lts
4nvm alias default --lts
5node -v
6npm -v

What this does:

  • installs nvm in your home directory
  • loads it into the current shell
  • installs the latest LTS release of Node.js
  • installs npm automatically as part of Node.js
  • sets the LTS release as the default for later shells

A quick smoke test confirms the installation:

bash
1mkdir demo-app
2cd demo-app
3npm init -y
4printf 'console.log("npm works on EC2")\n' > app.js
5node app.js

If you see the expected message, the toolchain is working.

Alternative: Use the Operating System Package Manager

If you want a machine-wide installation managed by the OS, use the distribution package manager. This is more common for tightly controlled server images, but the packaged version may lag behind the current LTS release.

Amazon Linux example:

bash
sudo dnf install -y nodejs
node -v
npm -v

Ubuntu example:

bash
1sudo apt update
2sudo apt install -y nodejs npm
3node -v
4npm -v

This approach is simple, but you trade away version flexibility. If your project depends on a specific major release, nvm is usually the better choice.

Persist the Environment for New Sessions

A common surprise on EC2 is that node works immediately after installation, then disappears after reconnecting. That usually means the shell profile was not reloaded or the nvm initialization lines were not sourced.

Add this to your shell startup file if the installer did not do it correctly:

bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"

Then start a new shell or run:

bash
source ~/.bashrc

If you use zsh, update ~/.zshrc instead.

Installing Global Packages

Once npm is available, global tools are straightforward:

bash
npm install -g pm2
pm2 -v

When you use nvm, avoid sudo npm install -g .... Global packages should live inside the selected Node.js version owned by your user account. Adding sudo often creates permission problems later.

Common Pitfalls

The most common issue is installing npm from old distro repositories and then discovering the version is far behind what your project expects. If the application requires a modern runtime, use nvm.

Another frequent mistake is forgetting that the EC2 login user matters. Installing Node.js as ec2-user does not magically make it available for a different account unless you repeat the setup or switch to a system-wide package installation.

A third problem is assuming npm and Node.js are separate packages in all cases. With nvm, installing Node.js already installs npm. If node -v works but npm -v does not, the shell environment is usually misconfigured.

Finally, do not skip the verification step. Run both node -v and npm -v, then create a tiny project. Version output alone does not prove your PATH and write permissions are correct.

Summary

  • Install Node.js correctly and npm comes with it
  • On EC2, nvm is usually the most practical choice for application hosts
  • Connect over SSH with the right AMI user such as ec2-user or ubuntu
  • Verify with node -v, npm -v, and a small test project
  • Use OS packages only when you want system-managed versions and accept older releases

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.