Guide

Add a swap file to a small VPS

Last updated: 15 November 2026

Swap is disk space the system uses when RAM runs out. On a small VPS it can stop the kernel from killing your app during a short memory peak. It is a safety net, not a replacement for RAM: if the server swaps all the time, it is too small. If you are logged in as root, leave out sudo.

Fast VPS from $7.99 a month

See Fast VPS plansHow ordering works

Check first

free -h
swapon --show

If swapon --show prints nothing, there is no swap yet. If it lists a swap already, stop here.

Create a 2 GB swap file

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

If fallocate fails on your file system, create the file with sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 instead, then continue from chmod. Check with free -h.

Tune it

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

A low swappiness makes the system prefer RAM and use swap only under pressure.

When swap is not the answer

Questions and answers

What is swap?
Disk space the system uses as extra memory when RAM runs out.

How big should the swap file be?
On a small VPS 1 to 2 GB is a common start; it is a safety net, not extra RAM.

How do I make swap permanent?
Add a line for the swap file to /etc/fstab.

When should I add RAM instead?
When the server uses swap constantly and is slow.

Ready to try it? Fast VPS from $7.99 a month

See Fast VPS plansHow ordering works