How to Add Swap Space on EC2 Ubuntu 22.04

3 min read

EC2 instances don’t come with swap space configured. On small instances (t2.micro, t3.small), this means your applications can get killed by the Linux OOM (out-of-memory) killer when RAM runs out. Adding swap gives the system overflow space on disk so it can handle temporary memory spikes instead of crashing. This guide shows you how to add swap space on EC2 Ubuntu 22.04.

Prerequisites

How Much Swap Do You Need?

Instance RAMRecommended Swap
512 MB – 1 GB1 GB – 2 GB
2 GB2 GB
4 GB2 GB – 4 GB
8 GB+2 GB – 4 GB (swap is rarely needed)

For most EC2 workloads, 1–2 GB of swap is enough. Swap on EBS-backed storage is much slower than RAM, so it’s meant as a safety net — not a replacement for adequate memory. If your instance is consistently using swap, upgrade to a larger instance type instead.

Step 1: Check for Existing Swap

Verify that no swap is currently configured:

sudo swapon --show

If the command returns no output, there’s no swap configured. You can also check with free -h — the Swap row will show all zeros.

Step 2: Create the Swap File

Create a 2 GB swap file:

sudo fallocate -l 2G /swapfile

Change 2G to whatever size you need (1G, 4G, etc.).

Set the correct permissions so only root can read and write the file:

sudo chmod 600 /swapfile

Step 3: Enable the Swap File

Format the file as swap space and turn it on:

sudo mkswap /swapfile
sudo swapon /swapfile

Verify it’s active:

sudo swapon --show

You should see output like:

NAME      TYPE  SIZE USED PRIO
/swapfile file    2G   0B   -2

Step 4: Make Swap Persistent Across Reboots

The swap is active now, but it won’t survive a reboot unless you add it to /etc/fstab:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Verify the entry was added:

sudo cat /etc/fstab

You should see a line with /swapfile none swap sw 0 0 at the end.

Step 5: Adjust Swappiness

Swappiness controls how aggressively the kernel moves data from RAM to swap. The default value is 60, but for a server you typically want a lower value so the system prefers RAM and only uses swap when necessary.

Check the current value:

cat /proc/sys/vm/swappiness

Set it to 10 (takes effect immediately):

sudo sysctl vm.swappiness=10

Make it persistent across reboots:

echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
ValueBehavior
0Only swap to avoid out-of-memory
10Swap when RAM is mostly full (recommended for servers)
60Default — swaps more aggressively
100Swap as much as possible

Verify Everything

Run free -h to confirm swap is active and the correct size:

free -h
               total        used        free      shared  buff/cache   available
Mem:           976Mi       512Mi       112Mi        1Mi       351Mi       312Mi
Swap:          2.0Gi          0B       2.0Gi

Removing Swap (If Needed)

To remove the swap file and free up disk space:

sudo swapoff /swapfile
sudo rm /swapfile

Then remove the /swapfile none swap sw 0 0 line from /etc/fstab:

sudo nano /etc/fstab

Delete the swap line, save, and exit.

Conclusion

Adding swap on EC2 takes a few commands: create the file, format it, enable it, add it to /etc/fstab, and set swappiness to 10. This prevents OOM crashes on small instances without requiring an instance upgrade.

If you’re running database workloads on your instance, swap is especially useful — see How to Install MySQL Database on EC2 Ubuntu 22.04 LTS. To monitor memory and swap usage over time, check out How to Install Datadog Agent with Apache2 on EC2 Ubuntu 22.04.