AskHandle Blog

How to access EC2 and install Node.js?

November 19, 2025Dustin Collins3 min read

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 (.pem file) 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:

  1. Place the .pem file in a folder, for example ~/.ssh/.
  2. Open a terminal.
  3. Move to the folder where the key is stored:
    bash
    1cd ~/.ssh
  4. Change permission of the key:
    bash
    1chmod 400 my-key.pem
    Replace my-key.pem with 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: admin or debian (varies)
  • Amazon Linux: ec2-user
  • CentOS: centos
  • RHEL: ec2-user or root (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
1ssh -i ~/.ssh/my-key.pem ubuntu@54.xx.xx.xx

or, using a DNS name:

bash
1ssh -i ~/.ssh/my-key.pem ubuntu@ec2-54-xx-xx-xx.compute-1.amazonaws.com

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
1ssh -i C:\Users\YourName\.ssh\my-key.pem ubuntu@54.xx.xx.xx

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 ubuntu@54.xx.xx.xx on port 22.

Once connected, your prompt changes to something like:

bash
1ubuntu@ip-172-31-xx-xx:~$

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
1sudo apt-get update
2sudo apt-get upgrade -y

For Amazon Linux or RHEL-based distributions:

bash
1sudo yum update -y

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:

  1. Using NodeSource repository
  2. 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
1curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -

This script configures the package repository for Node.js.

Install Node.js

After the repository is added:

bash
1sudo apt-get install -y nodejs

This usually installs both node and npm.

Verify the installation

Check versions:

bash
1node -v
2npm -v

You should see version numbers, for example:

bash
1v20.12.0
210.8.2

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
1curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

Then reload your shell configuration:

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

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
1nvm list-remote

Install a specific version, such as 20:

bash
1nvm install 20

Set it as default:

bash
1nvm alias default 20
2nvm use 20

Confirm:

bash
1node -v
2npm -v

nvm allows you to switch versions later:

bash
1nvm install 18
2nvm use 18

Step 8: Run a basic Node.js test

Create a simple server.js file:

bash
1nano server.js

Paste:

javascript
1const http = require('http');
2const port = 3000;
3
4const server = http.createServer((req, res) => {
5  res.statusCode = 200;
6  res.setHeader('Content-Type', 'text/plain');
7  res.end('Hello from EC2 and Node.js!\n');
8});
9
10server.listen(port, () => {
11  console.log(`Server running on port ${port}`);
12});

Save and exit (Ctrl+O, Enter, then Ctrl+X in nano).

Run:

bash
1node server.js

If your security group allows inbound traffic on port 3000, you can access:

text
1http://your-ec2-public-ip:3000

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
1sudo npm install -g pm2

Start your app:

bash
1pm2 start server.js

Save the process list and configure startup:

bash
1pm2 save
2pm2 startup

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.