Techtrekking

How to setup Ubuntu server

By Pravin

Once you have created ubuntu instance or EC2 file, you will need to connect to instance using SSH

SSH to instance

cmod 400 ssh-2026-01-01.key # connect to instance ssh -i ssh-key-2026-01-01.key ubuntu@<server-ipv4-address>

ensure OS is up to date

sudo apt update sudo apt upgrade -y

create swap file:

A swap file (or swap partition) acts as overflow memory when your physical RAM fills up. If you are using instance with RAM of just 1 or 2 GM, you might have issues while installing node modules however if you create swap file, this process will be smooth.

Here's how to create swap memory on Ubuntu:

# Create a 2GB swap file sudo fallocate -l 2G /swapfile # Set correct permissions sudo chmod 600 /swapfile # Set up the swap space sudo mkswap /swapfile # Enable the swap sudo swapon /swapfile # Verify it's working sudo swapon --show free -h

This will create a swap file but it is temporary and it will be lost when you reboot, to make it permanent, Add this line to /etc/fstab:

# Edit fstab sudo nano /etc/fstab # Add this line at the end: /swapfile none swap sw 0 0

Recommended swap sizes:

  • 2GB RAM → 2-4GB swap
  • 4GB RAM → 2GB swap
  • 8GB+ RAM → 2-4GB swap (or less if you have plenty of RAM)

Optional: Adjust swappiness (how aggressively system uses swap)

# Check current value (default is usually 60) cat /proc/sys/vm/swappiness # Set to 10 (use swap less aggressively - good for systems with decent RAM) sudo sysctl vm.swappiness=10 # Make permanent echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

To check your current swap:

free -h swapon --show
Comments
No comments yet. Be the first to comment!
Leave a Comment
Your comment will be visible after approval.