On a low-RAM VPS, swap reduces out-of-memory kills. This guide adds a swap file, which is simple and needs no dedicated partition.
Swap is disk space that the kernel uses as overflow when RAM is full. It is much slower than memory, so think of it as a safety net rather than a substitute for RAM. It lets a short spike (a package upgrade, a build, a backup job) finish instead of the kernel's OOM killer terminating a service such as your database or web server.
When to use swap
Swap makes sense on small plans, on servers that run occasional heavy tasks, or after the OOM killer has already stopped a process (look for "Out of memory" in journalctl -k). It is not a fix for a server that swaps all the time: in that case reduce the workload or move to a plan with more RAM. Also note that some container-style virtualization does not allow enabling swap from inside the guest; if swapon answers "Operation not permitted", swap is controlled outside your VPS.
📋 Prerequisites
- Linux VPS with root/sudo
- Free disk space (e.g. 2 GB for a 2 GB swap file)
- A disk that is not almost full: the file takes its whole size immediately
Check RAM and current swap
free -h shows total, used and free memory plus a Swap line. swapon --show lists active swap areas and prints nothing if there is none. df -h / confirms that the disk has enough room.
Bashfree -h swapon --show df -h /
Create a 2 GB swap file
A common rule is 1 to 2 times the RAM on very small servers and about 2 to 4 GB on larger ones; 2 GB is enough for most VPS. Each command has a purpose: fallocate reserves the space instantly, chmod 600 makes the file readable by root only (memory pages can contain secrets, and swapon complains otherwise), mkswap writes the swap signature and swapon activates it immediately, with no reboot.
Bashsudo fallocate -l 2G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile
If fallocate is not supported by your filesystem, or swapon later answers "Invalid argument", build the file with dd instead (then run the last three commands above):
Bashsudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
Enable on boot
This line adds an entry to /etc/fstab: the file, no mount point (none), type swap, option sw, and no dump or check. Without it the swap disappears at the next reboot. Run it only once, otherwise you create duplicate lines.
Bashecho '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Tune swappiness (optional)
vm.swappiness (0 to 100) controls how eagerly the kernel moves idle memory to swap; the default is 60. A value of 10–30 keeps applications in RAM and uses swap only under real pressure, which suits databases and web servers. Do not set it to 0 hoping to disable swap. The file in /etc/sysctl.d/ is read at every boot and sysctl -p applies it right now.
Bashecho 'vm.swappiness=20' | sudo tee /etc/sysctl.d/99-swappiness.conf sudo sysctl -p /etc/sysctl.d/99-swappiness.conf
Verify
swapon --show must list /swapfile with a size of 2G, free -h must show 2.0Gi on the Swap line, and the swappiness file must print 20. The last command turns the swap off and on again from /etc/fstab, which proves the boot entry is valid.
Bashswapon --show free -h cat /proc/sys/vm/swappiness sudo swapoff /swapfile && sudo swapon -a # test the fstab entry
Troubleshooting
insecure permissions 0644, 0600 suggested: runsudo chmod 600 /swapfile, thensudo swapon /swapfileagain.swapon failed: Invalid argumentorfallocate failed: Operation not supported: remove the file and recreate it withdd.No space left on device: free disk space or choose a smaller size, for example 1G.- Swap is gone after a reboot: check that the line in
/etc/fstabis exact, then runsudo swapon -aand read the error. - Swap is always almost full: find the culprit with
ps aux --sort=-%mem | head, then lower its memory settings or upgrade the plan.
The same commands work on AlmaLinux and Rocky Linux.
Remove the swap file
To roll back, turn the swap off, delete the file and remove its line from /etc/fstab.
Bashsudo swapoff /swapfile sudo rm /swapfile sudo sed -i '\|^/swapfile |d' /etc/fstab
More stable server! 🚀