VPS guide
How to Add a Swap File on Linux
Short answer
Run sudo swapon --show to check for existing swap. If there's none, create a file with fallocate, set its permissions to 600, format it with mkswap, turn it on with swapon, and add it to /etc/fstab so it survives a reboot.
Swap is disk space Linux uses when RAM runs out. It's far slower than memory, but on a small VPS it stops a short spike from triggering the out-of-memory killer, which ends a process such as MySQL to free RAM. If a server uses swap all the time, it needs more memory, not more swap.
Check what you have
sudo swapon --show
free -h
df -h /If swapon prints nothing, there's no swap. Some VPS templates include a swap file or partition already, so check before adding another. df shows how much disk space is free for it.
How much swap to add
| RAM | Swap |
|---|---|
| 2 GB | 2 GB |
| 4 GB | 2 GB |
| 8 GB or more | 2 to 4 GB |
Treat these as a starting point. Servers don't hibernate, so there's no need for swap equal to RAM.
Create the swap file
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfileIf swapon reports that the file has holes, create it with dd instead, then repeat the last three commands:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048Make it permanent
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
sudo findmnt --verifyA mistake in /etc/fstab can stop the server booting, so check that findmnt --verify reports no errors. If it does, open the browser console for your VPS in Virtualizor after a reboot to fix the file.
Tune swappiness
Swappiness, from 0 to 100, sets how readily Linux moves memory out to swap. It's 60 by default on Ubuntu and Debian. For a server, 10 keeps working data in RAM and uses swap for memory that's sitting idle:
sudo sysctl vm.swappiness=10
echo 'vm.swappiness = 10' | sudo tee /etc/sysctl.d/99-swappiness.confSigns you need more RAM instead
Check whether the out-of-memory killer has ended any processes:
journalctl -k | grep -i 'out of memory'Then watch swap activity with vmstat 5. Steady non-zero numbers in the si and so columns mean the server is swapping all the time, which slows everything down. The fix is a larger plan.
Remove a swap file
sudo swapoff /swapfile
sudo rm /swapfileThen delete the /swapfile line from /etc/fstab.
UK VPS hosting with full root access
Guaranteed AMD EPYC cores, SSD RAID storage and DDoS protection on every plan, from £5.99 a month with no VAT to add. Need faster cores? Upgrade to Ryzen.
See VPS plansQuestions
Does swap slow my server down?
Having swap doesn't. Using it heavily does, because disk is far slower than RAM. Occasional use for idle memory is normal.
Does this work on AlmaLinux and Rocky?
Yes, the same commands work. If swapon complains that the file has holes, create it with dd instead of fallocate.
Should I use zram instead?
zram compresses memory in RAM instead of writing to disk, which suits machines with little memory and spare CPU. A swap file is simpler and works everywhere, and you can use both.
Related guides
Sources
Commands are for Ubuntu 24.04, Debian 12 and AlmaLinux 9, as of 16 October 2026. If something here no longer matches what you see, tell us and we'll update it.