How to Add Swap Space on Ubuntu Server (Stop Out-of-Memory Crashes)

Add swap and stop out-of-memory crashes: For any server under 4 GB.

A server with 1 or 2 GB of RAM — a cheap cloud VM, a Raspberry Pi, an old laptop — will eventually run out of memory, and when Linux runs out it kills the biggest program with no warning. Usually your database. Swap is a chunk of disk the system can spill memory into instead. It's slower than RAM, but slow beats dead.

Quick reference

TaskCommand
Do I have swap?free -h or swapon --show
Make a 2 GB swap filesudo fallocate -l 2G /swapfile
Lock it downsudo chmod 600 /swapfile
Format and enablesudo mkswap /swapfile && sudo swapon /swapfile
Keep it after rebootadd to /etc/fstab
Turn it offsudo swapoff /swapfile

1. Check what you've got

free -h
swapon --show

If the Swap line shows 0B and swapon --show prints nothing, you have none. Many cloud images ship without any.

2. Create the swap file

How big? A simple rule: the same as your RAM, up to about 4 GB. For a 2 GB server:

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

The chmod 600 matters — swap can contain passwords from memory, so nobody else should be able to read it.

Check it's live:

free -h

3. Make it permanent

Without this, swap disappears at the next reboot:

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

4. Tell Linux not to be too keen

Swappiness (0–100) is how readily the system moves things to swap. The default of 60 is tuned for desktops. On a server, lower is better — use RAM first and swap only when genuinely short:

sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

Removing or resizing

sudo swapoff /swapfile
sudo rm /swapfile

Then remove the line from /etc/fstab, and recreate at a new size if you want.

Is swap a replacement for RAM?

No. If free -h shows swap in constant heavy use and the server is sluggish, it needs more memory or fewer things running. Swap is a safety net for occasional spikes, not a place to live.

Troubleshooting

"fallocate failed: Operation not supported." The filesystem doesn't support it (some older or network filesystems). Use sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 instead — it's slower but works anywhere.

"swapon failed: Invalid argument." You skipped mkswap, or the file is on a filesystem that can't hold swap (such as Btrfs with copy-on-write — chattr +C the file first, or use a partition).

Swap exists but the server still gets killed. Look in journalctl -k | grep -i "out of memory" to see what the kernel killed and how much it wanted. If it's one program eating everything, swap just delays the inevitable — limit that program instead.

Raspberry Pi / SD card. Swap wears out SD cards. Keep it small (512 MB to 1 GB), set swappiness low, or put it on a USB SSD.

Wrapping up

Four commands to create it, one line in fstab to keep it, swappiness down to 10 so it's only used when needed. On any server with less than 4 GB of RAM, do this on day one and you'll never meet the out-of-memory killer.

Call us