Add swap space in Ubuntu

1 minute read

My DigitalOcean Droplet started dropping containers while kswapd ate up CPU cycles like Pacman. Time to add a swap space, or rather a swap file cause I did not want to add a dedicated volume.

# swap usage check - swapon: no output, no use; free: should read "0"
sudo swapon --show
free -h

# as root: create swap (make sure you have enough disk space available)
fallocate -l 4G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

# reboot and check (see first two commands) if swap is available

Optinonally you can also tune swappiness and cache pressure settings. Both is described in this article’s source (DigitalOcean).