How to access EC2 and install Node.js?
Logging in to an EC2 server and installing Node.js is a common first step when setting up a backend or hosting a simple app. This guide walks through connecting to your instance over SSH and installing Node.js in a clean, repeatable way.
Prerequisites
Before connecting to an EC2 instance, prepare the following:
- An EC2 instance already running (for example, using Ubuntu or another Linux distribution)
- The public IP address or public DNS name of the instance
- The SSH key pair (
.pemfile) downloaded when the instance was created - An SSH client:
- Linux or macOS terminal
- Windows:
- PowerShell or Command Prompt with OpenSSH
- Or an SSH client such as PuTTY
- Basic familiarity with a terminal: running commands, switching directories, editing small files
Keep your private key file safe. It gives direct access to the server that runs your applications.
Step 1: Adjust key permissions
On Linux or macOS:
- Place the
.pemfile in a folder, for example~/.ssh/. - Open a terminal.
- Move to the folder where the key is stored:
Bash
- Change permission of the key:
ReplaceBashmy-key.pemwith the name of your key.
On Windows with PowerShell and OpenSSH, you don’t usually need to run chmod, but storing the key in a user-only folder is recommended, such as C:\Users\YourName\.ssh\.
Step 2: Find your EC2 login username
The default SSH username depends on the instance’s operating system image:
- Ubuntu:
ubuntu - Debian:
adminordebian(varies) - Amazon Linux:
ec2-user - CentOS:
centos - RHEL:
ec2-userorroot(varies)
In most personal setups with Ubuntu images, the login name ubuntu is common. If unsure, check the instance details in the EC2 console or its documentation.
Step 3: Connect to the EC2 server via SSH
You need the public IP address or public DNS name of your EC2 instance. It looks like:
- IP:
54.xx.xx.xx - DNS:
ec2-54-xx-xx-xx.compute-1.amazonaws.com
On Linux or macOS
In the terminal, run:
Bash
or, using a DNS name:
Bash
On first connection, you may see a notice that the authenticity of the host can’t be established. Type yes and press Enter to continue.
On Windows (PowerShell or Command Prompt)
If OpenSSH is available, the same command works:
Powershell
Adjust the path and username as needed.
If using PuTTY, you must convert the .pem file to .ppk using PuTTYgen, then configure PuTTY to use that key and connect to [email protected] on port 22.
Once connected, your prompt changes to something like:
Bash
You are now logged in to your EC2 instance.
Step 4: Update the system packages
Before installing Node.js, refresh package lists and apply updates.
For Ubuntu or Debian:
Bash
For Amazon Linux or RHEL-based distributions:
Bash
Running updates helps avoid package conflicts or missing dependencies later.
Step 5: Choose how to install Node.js
There are several ways to install Node.js on an EC2 instance. Two widely used methods are:
- Using NodeSource repository
- Using Node Version Manager (nvm)
nvm is very convenient if you want multiple Node.js versions or easy upgrades. NodeSource is simple for system-wide installation.
The examples below will assume Ubuntu. Commands for other distributions are similar but may differ slightly in package names.
Step 6: Install Node.js using NodeSource (option 1)
Add NodeSource repository
Pick a Node.js version line, such as 18 or 20. Replace 20 with your desired version:
Bash
This script configures the package repository for Node.js.
Install Node.js
After the repository is added:
Bash
This usually installs both node and npm.
Verify the installation
Check versions:
Bash
You should see version numbers, for example:
Bash
If version numbers appear, Node.js is ready to use.
Step 7: Install Node.js using nvm (option 2)
Using nvm gives flexibility and is helpful on development servers.
Install nvm
Run:
Bash
Then reload your shell configuration:
Bash
These lines might already be added to ~/.bashrc or ~/.profile. Opening a new SSH session also triggers them.
Install a Node.js version with nvm
List available versions:
Bash
Install a specific version, such as 20:
Bash
Set it as default:
Bash
Confirm:
Bash
nvm allows you to switch versions later:
Bash
Step 8: Run a basic Node.js test
Create a simple server.js file:
Bash
Paste:
Javascript
Save and exit (Ctrl+O, Enter, then Ctrl+X in nano).
Run:
Bash
If your security group allows inbound traffic on port 3000, you can access:
Html
You should see the text response from the server.
Step 9: Keep Node.js apps running in the background
Node apps stop when the SSH session closes unless you use a process manager. A popular choice is pm2.
Install:
Bash
Start your app:
Bash
Save the process list and configure startup:
Bash
Follow any extra command shown in the terminal to complete the setup.
Your Node.js application will continue running, and pm2 can restart it if it crashes or after a reboot.
Connecting to an EC2 instance, updating the system, and installing Node.js can be done in a few focused steps: adjust key permissions, use SSH to log in, run system updates, choose a Node.js installation method, then test with a basic server. With pm2 or another process manager, your Node.js apps can run reliably in the background and stay online for your users.












