How to Add Swap Space to a VPS

Create a swap file on a Linux VPS in five commands, size it correctly, set swappiness so it is a safety net rather than a slowdown, and make it survive reboots.

Published
Reading time
2 min

A VPS without swap has a hard edge: when memory runs out, the kernel kills the biggest process — usually the database — and the site goes down. Swap gives the server somewhere to put rarely used memory pages instead, turning a crash into a slowdown. Most VPS images ship without it, and adding it takes two minutes.

Check whether you have any

bash
free -h
swapon --show

If Swap: shows 0B and swapon --show prints nothing, there is none.

Create a swap file

Size guidance: equal to RAM for servers up to 4 GB; half of RAM above that; 2 GB is enough on most web servers regardless. Here, 2 GB:

bash
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

chmod 600 matters: swap can contain fragments of anything that was in memory, including passwords. If fallocate is unavailable on your filesystem, dd does the same job more slowly:

bash
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress

Verify:

bash
free -h

Make it permanent

The swap file is active now but forgotten at reboot. Add it to /etc/fstab:

bash
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Test the line without rebooting: sudo swapoff /swapfile && sudo swapon -a && free -h.

Set swappiness

Swappiness (0–100) is how eagerly the kernel moves pages to swap. The default of 60 is tuned for desktops and makes a server swap out application memory while there is still RAM free, which is slow. For a web server, 10 is right: swap is used only under real pressure.

bash
sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf

A related knob, vm.vfs_cache_pressure=50, makes the kernel hold on to directory and file metadata longer — helpful for PHP applications that stat thousands of files per request.

What swap is not

It is not more RAM. Swap on NVMe is still a hundred times slower than memory; a server that is constantly swapping is a server that needs more RAM or a lighter workload. Check whether that is you:

bash
vmstat 1 5

Look at the si and so columns (swap in/out per second). Occasional small numbers are fine. Sustained hundreds or thousands mean memory is genuinely short. Find the hungry processes:

bash
ps aux --sort=-%mem | head -8

On a PHP server the fix is usually fewer PHP-FPM workers (tuning PHP-FPM pools) or a smaller MySQL buffer pool; on a small VPS it is the next plan up.

Removing or resizing

bash
sudo swapoff /swapfile
sudo rm /swapfile
# then recreate at the new size, and update /etc/fstab if the path changed

zram: the alternative on small VPSs

On a 1 GB server, compressing memory in RAM (zram) can be better than swapping to disk. Ubuntu: sudo apt install zram-tools, then set PERCENT=50 in /etc/default/zramswap. It gives roughly 1.5–2× effective memory for the pages that compress well, at some CPU cost.

VPSPioneer managed VPS plans are provisioned with swap and swappiness set as above, and memory is one of the metrics we alert on — a server that starts swapping heavily gets a ticket from us suggesting what to change before it becomes an outage.

#linux#swap#memory#vps

Keep reading

More from Linux

All guides

Linux

How to Schedule Tasks With Cron (and Know When They Fail)

Cron syntax with examples, where jobs live, running them as the right user, capturing output, and how to get told when a job fails instead of finding out later.

3 min read →

Linux

How to Check Disk Space on Linux and Find What Is Filling It

df, du and ncdu to see where the space went, the usual culprits on a web server — logs, backups, caches, mail — and how to clear them safely.

3 min read →

Linux

systemd Basics: Start, Stop, Enable and Read Logs with journalctl

The systemctl commands that manage services on modern Linux, how to start a service at boot, write a unit for your own app, and read logs with journalctl.

3 min read →