Quick answer
How do I add swap on a Linux VPS?
To add swap on a Linux VPS, create a file with fallocate -l 2G /swapfile, set it to mode 600, format it with mkswap, turn it on with swapon and add it to /etc/fstab so it survives a reboot. On a SaltVPS KVM server, swap works like on any Linux machine, from Grain (2 GB) upward.
In brief
- What swap does: gives the kernel disk space to park idle memory pages when RAM runs short.
- Why on a VPS: it stops the out-of-memory killer from stopping a process during a short peak.
- What it is not: a replacement for RAM; disk is far slower than memory, even NVMe.
- Typical size: 1 to 2 GB on a small server; more only for a reason.
- KVM: each server has its own kernel, so a swap file is allowed.
How do you add a swap file on a Linux VPS?
Run as root. The same commands work on Debian, Ubuntu, AlmaLinux and Rocky Linux with ext4 or XFS.
Check for existing swap
Run
swapon --showandfree -h. No output from swapon means no swap is active.Create the file
Run
fallocate -l 2G /swapfile. If fallocate is refused, usedd if=/dev/zero of=/swapfile bs=1M count=2048.Restrict access
Run
chmod 600 /swapfileso only root can read it. swapon warns about wider permissions.Format and enable
Run
mkswap /swapfile, thenswapon /swapfile. Check again withswapon --show.Keep it after reboot
Add the line
/swapfile none swap sw 0 0to/etc/fstab. Runfindmnt --verifyto catch typos.Tune swappiness
Run
sysctl vm.swappiness=10, and putvm.swappiness=10in/etc/sysctl.d/99-swap.confto keep it.
How much swap fits each server size?
These are starting points, not rules: watch real use with free -h and adjust.
| Plan | RAM | Disk | Starting swap |
|---|---|---|---|
| Grain | 2 GB | 40 GB NVMe | 2 GB |
| Flake | 4 GB | 80 GB NVMe | 2 GB |
| Crystal | 6 GB | 120 GB NVMe | 2 GB |
| Halite | 8 GB | 160 GB NVMe | 1 to 2 GB |
| Rock to Dome | 16 GB to 32 GB | 240 GB NVMe to 640 GB NVMe | 1 GB, or none |
Does swap make a small VPS faster?
No: swap prevents crashes, it does not add speed. When RAM fills, the kernel either moves idle pages to swap or kills a process. A little swap turns a crash into a short slowdown. Constant swapping, though, means the server needs more memory.
SaltVPS checked its plan list on 16 November 2026: RAM is reserved for each server, while vCPU are shared with neighbours on the same host. So adding swap never takes memory from anyone else. If free -h shows swap in heavy use day after day, move up a plan; the pricing page lists RAM per plan, and choosing a VPS explains how to size one.
What does vm.swappiness change?
It sets how eagerly the kernel moves memory to swap, on a scale from 0 to 200. Most distributions default to 60. A value of 10 keeps data in RAM longer and uses swap mainly under pressure, which suits servers with databases or containers.
Check the current value with cat /proc/sys/vm/swappiness. Changes made with sysctl apply at once; the file in /etc/sysctl.d/ reapplies them after a reboot.
When is a swap file on a VPS most useful?
During short peaks: package upgrades, image builds, a backup or a burst of visitors. Building Docker images and running apt upgrade on a 2 GB server are typical cases. The Docker guide shows how to check disk use, since swap files share the same disk. Because the server runs on KVM, no host setting blocks a swap file.
Swap on a VPS: questions
How do I add swap on Ubuntu 24.04?
The steps above work as they are: fallocate, chmod 600, mkswap, swapon and a line in /etc/fstab.
How do I remove a swap file?
Run swapoff /swapfile, delete its line from /etc/fstab, then rm /swapfile.
Can I resize swap later?
Yes. Turn it off with swapoff, recreate the file at the new size, run mkswap and swapon again.
Is zram better than a swap file?
zram compresses memory in RAM instead of writing to disk. It helps on very small servers but uses CPU; a swap file is simpler.
What is the best swap size for a 2 GB VPS?
2 GB is a common start. Increase it only if free -h shows swap filling up during normal peaks.
Verdict
- Adding swap on a Linux VPS takes five commands and one fstab line.
- Use it as a safety net; heavy daily swapping means the plan needs more RAM.
Sources (3)
- SwapFaq — Ubuntu community help wiki (2026-11-16)
- mkswap(8) manual page — man7.org (2026-11-16)
- Documentation for /proc/sys/vm (swappiness) — Linux kernel (2026-11-16)
More RAM instead of more swap
Flake doubles Grain's memory to 4 GB, for $4.50 a month on 12 months.
- Built by a person, 1 hour at most after confirmation
- Lost days credited back if a server cannot be delivered
- Pay in BTC, XMR, USDT, USDC, ETH, SOL or LTC
- DomaineGoat LLC · Panama