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.
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:
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:
What this does:
- installs
nvmin your home directory - loads it into the current shell
- installs the latest LTS release of Node.js
- installs
npmautomatically as part of Node.js - sets the LTS release as the default for later shells
A quick smoke test confirms the installation:
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:
Ubuntu example:
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:
Then start a new shell or run:
If you use zsh, update ~/.zshrc instead.
Installing Global Packages
Once npm is available, global tools are straightforward:
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
npmcomes with it - On EC2,
nvmis usually the most practical choice for application hosts - Connect over SSH with the right AMI user such as
ec2-userorubuntu - 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
- Installing PostgreSQL Client v10 on AWS Amazon Linux EC2 AMI
- Instance Retirement Instance-stop
- InstanceAgentPluginsCodeDeployPluginCommandPoller Missing credentials
- InsufficientCapabilitiesException CAPABILITY_NAMED_IAM when creating a stack with IAM policies
- invalid ELF header when using the nodejs ref module on AWS Lambda
- Invariant Violation Application AwesomeProject has not been registered When building for iOS device with static jsbundle
- Internal error occurred failed calling webhook mservice.elbv2.k8s.aws
- Invalid Bucket name when creating s3 bucket with AWS SDK

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.