What you will achieve
Choose between swap partition and swap file on Linux, create each on Ubuntu/Debian, and understand hibernation and SSD implications.
1) Swap partition (traditional)
Created at install time. Visible as TYPE swap in lsblk. Slightly simpler for hibernation (resume from dedicated swap).
2) Swap file (flexible)
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Ubuntu 24.04+ uses swapfile by default on ext4. Works with btrfs if created correctly on supported kernels.
3) swappiness
cat /proc/sys/vm/swappiness
sudo sysctl vm.swappiness=10
Lower values keep more in RAM — common on servers with enough memory.
4) When to skip swap
Containers with memory limits and databases with strict latency may prefer no swap — but the OOM killer becomes the only safety valve.
Verify
swapon --show
free -h
5) Remove swap safely
sudo swapoff /swapfile
sudo sed -i '/swapfile/d' /etc/fstab
sudo rm /swapfile
6) btrfs swap file caveats
Requires documented btrfs layout — swap file must not be copy-on-write. Follow distro wiki (Fedora/Ubuntu) for btrfs swap file creation steps.
7) Monitor swap usage
vmstat 1
grep Swap /proc/meminfo
zram alternative
On 1–2 GB RAM devices, zram often beats disk swap — see zram guide. Traditional swap still helps hibernate and memory overcommit on servers.
8) cgroup v2 memory limits
Containers with memory max hit cgroup OOM before host swap — host swap does not save unbounded container memory leaks.
Prerequisites
Free disk space for swapfile or unpartitioned space for swap partition. Root access. Know RAM size and hibernation requirements. btrfs/ext4 root type affects swapfile support.
Resume from hibernate
Initramfs must know swap location — resume=UUID= kernel parameter and RESUME in /etc/initramfs-tools/conf.d/resume on Debian.
Cloud images default
Many cloud Ubuntu images ship without swap — add swapfile before memory-heavy compile jobs on small instances.
Encrypted swap
LUKS installers create encrypted swap automatically — swapon shows /dev/mapper/cryptswap. Manual swapfile inside encrypted root is plaintext unless using swapfile offset tricks — prefer dedicated LUKS swap partition for hibernate on encrypted systems.
ZFS swap volume
ZFS zvol as swap needs careful size — zvol reservation affects pool free space reporting differently than file swap on ext4. Consult ZFS on Linux docs before creating 32G zvol swap on small pools.