How to add swap space to your VPS

When a server runs out of memory, Linux has two choices: kill a process, or push rarely used data out to disk to make room. That overflow area on disk is called swap. A small swap file will not make a slow server fast, but it can be the difference between a brief slowdown and a crash during a memory spike, such as when you install packages or build an application.

What swap is, and what it is not

Swap is a safety net, not extra memory. Disk is thousands of times slower than RAM, so a server that lives in swap feels sluggish. Use it to absorb short spikes, and fix the cause if you are always using it.

Good reasons for swap:

  • Small servers that occasionally run npm install, a compiler or an update and briefly need more memory.
  • Letting the system move idle pages out of RAM so the file cache has more room.
  • Avoiding the out-of-memory killer ending your database at a bad moment.

Poor reasons: hoping it fixes an application that really needs more RAM. If you are constantly swapping, choose a larger plan. See How to choose the right plan.

Check what you have

free -h
swapon --show

If swapon --show prints nothing, there is no swap. Also check that you have free disk space with df -h, since the swap file will use some.

Create a swap file

A common size is the same as your RAM for small servers, or around 1 to 2 GB for most uses. This example creates 2 GB:

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

What each step does:

  1. fallocate reserves a 2 GB file. If it fails on your filesystem, use sudo dd if=/dev/zero of=/swapfile bs=1M count=2048.
  2. chmod 600 makes the file readable only by root. Swap can contain sensitive data from memory, so this matters.
  3. mkswap formats the file for use as swap.
  4. swapon turns it on immediately.

Confirm with swapon --show and free -h.

Make it permanent

A swap file enabled with swapon disappears at reboot unless you record it. Back up the file system table first, then add a line:

sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Mistakes in /etc/fstab can stop a server booting properly, so take the backup and check the line you added with tail /etc/fstab.

Tune swappiness

The vm.swappiness setting controls how eagerly the kernel uses swap, from 0 to 100. The usual default is 60. On a server with a database or an application you want kept in RAM, a lower number such as 10 is common:

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

The first command applies now, and the second keeps it after a reboot. Lower values mean "use swap only when needed". Do not set it to 0 expecting swap to be disabled, as behaviour varies by kernel.

Removing or resizing

To remove a swap file:

sudo swapoff /swapfile
sudo rm /swapfile

Then delete its line from /etc/fstab. To resize, turn it off, remove it and create a new one of the size you want.

Containers and special cases

  • Some virtualisation types do not allow swap. If swapon fails with an error, your VPS may restrict it. Ask support rather than fighting it.
  • Docker and memory limits. Containers have their own limits, so swap on the host does not stop a container being killed for exceeding its limit. See How to install Docker on Ubuntu.
  • Game servers on a panel. These run in their own limited environment, and the memory they get is set by the plan. See Understanding CPU, RAM and disk usage.

How to tell if you need more RAM

Watch the system for a day or two:

vmstat 5

In the si and so columns, steady non-zero numbers mean the system is moving data in and out of swap continuously, called thrashing. That is a sign to reduce what you run, or upgrade. How to monitor your VPS with a few basic command line tools shows other signs to look for.

Summary

StepCommand
Createfallocate -l 2G /swapfile
Securechmod 600 /swapfile
Formatmkswap /swapfile
Enableswapon /swapfile
Persistadd a line to /etc/fstab
Tunevm.swappiness=10

Still stuck? Open a ticket on Discord - run /ticket in any channel, pick a department, and include your Server ID or Invoice ID so we can jump straight in.