How to Create a Swap File on a Linux VPS
Small VPS plans run out of RAM fast. A swap file gives the kernel breathing room and prevents the OOM killer from killing your app. Here's how to add one.
On a small VPS, running out of RAM means the kernel's OOM (out-of-memory) killer starts terminating processes โ often your database or web app. A swap file gives the system a safety valve: slower disk-backed memory it can fall back on under pressure. This guide adds a swap file the right way and tunes how aggressively it's used.
What swap is (and isn't)
Swap is disk space the kernel uses as overflow when physical RAM fills up. It's much slower than RAM, so it's not a replacement for buying enough memory โ but it prevents hard crashes when you briefly spike over your limit. On modern VPS plans backed by NVMe, swap is fast enough to be a genuine safety net.
Check what you already have
free -h
swapon --show
If swapon --show prints nothing, you have no swap. Many minimal VPS images ship without it.
Step 1: create the swap file
Use fallocate to create a file quickly. A good rule of thumb is 1โ2ร your RAM for small servers (e.g. 2 GB swap on a 1โ2 GB VPS):
sudo fallocate -l 2G /swapfile
If fallocate isn't supported on your filesystem, use dd instead:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
Step 2: secure and format it
Lock down permissions so only root can read it, then mark it as swap:
sudo chmod 600 /swapfile
sudo mkswap /swapfile
Step 3: enable it
sudo swapon /swapfile
Confirm it's active:
free -h
swapon --show
{{SCREENSHOT}}
Step 4: make it persistent
Swap enabled with swapon disappears on reboot. Add it to /etc/fstab so it comes back automatically:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Step 5: tune swappiness
Swappiness (0โ100) controls how eagerly the kernel swaps. On a server, a high default (60) can push active memory to disk unnecessarily and hurt performance. Lower it so swap is used mainly under real pressure:
sudo sysctl vm.swappiness=10
Make it permanent:
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
A value of 10 keeps things in RAM until genuinely needed. For databases you might go even lower (1). See tune Linux performance for more.
When swap isn't the answer
If your server is constantly swapping, that's a symptom โ you need more RAM, not more swap. Constant swapping ("thrashing") makes everything crawl. Use monitoring to see whether swap is an occasional safety net or a daily crutch. If it's the latter, size up your plan. Also make sure you have enough free disk for the swap file.
zram: compressed swap that lives in RAM
On very small plans there's a clever alternative worth knowing: zram creates a compressed block device in RAM and uses it as swap. Because typical application memory compresses well (often 2โ3ร), you effectively get more usable memory without touching the disk at all โ and it's far faster than disk-backed swap. On Ubuntu:
sudo apt install zram-tools -y
Configure the size in /etc/default/zramswap (for example PERCENT=50 to use up to half of RAM as compressed swap), then restart the service. zram and a disk swap file aren't mutually exclusive โ a common pattern is zram as fast primary swap with a small disk swap file as a deeper fallback, giving the disk swap a lower priority. For a 1 GB VPS running a few services, zram alone often eliminates OOM kills without any disk I/O penalty.
Monitoring swap so it stays a safety net
Swap is meant to catch occasional spikes, not carry your workload. Keep an eye on how much is actually in use:
free -h
vmstat 2 5
In vmstat, the si and so columns show swap-in and swap-out activity per interval. Occasional non-zero values are fine; sustained high values mean the system is thrashing โ constantly shuffling data between RAM and disk โ and everything will feel sluggish. That's the signal to reduce memory usage (fewer or lighter services) or, more realistically, move to a plan with more RAM. Swap buys you headroom and prevents crashes, but it is never a substitute for enough physical memory, and treating it as one will always disappoint. Set a monitoring alert on swap usage so you find out before users do.
FAQ
How much swap should I create?
For small VPS plans, 1โ2ร RAM is a sensible starting point (2 GB swap on a 1โ2 GB server). Beyond ~4 GB of swap, more rarely helps โ you likely need more RAM.
Does swap slow down my server?
Only when it's actively used. Idle swap costs nothing. Low swappiness ensures RAM is preferred and swap only kicks in under pressure.
Swap file or swap partition?
A swap file is easier to create, resize, and remove โ ideal for a VPS. Performance is effectively identical on modern kernels.
Can I remove swap later?
Yes: sudo swapoff /swapfile, remove the fstab line, then sudo rm /swapfile. Make sure you have free RAM before disabling it.
The whole process in one block
For a standard disk-backed swap file, here's the complete sequence to create, secure, enable, and persist it:
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl vm.swappiness=10
That gives you 2 GB of persistent swap with server-appropriate swappiness. Confirm it with free -h and swapon --show. Remember the mental model: swap is a safety valve for occasional spikes, not a substitute for RAM. If vmstat shows constant swap activity, that's the signal to lighten your workload or move up a plan rather than adding more swap. Used this way โ as headroom the kernel reaches for only under real pressure โ a swap file quietly prevents the OOM killer from taking down your app at the worst possible moment.
A swap file is cheap insurance against OOM crashes on small plans. If you need genuinely more headroom, our NVMe-backed VPS plans scale RAM easily โ and see the VPS overview for specs.