Skip to content
Servers

How to Add Swap Space on an Ubuntu VPS

In this article11 sections

To add swap space on an Ubuntu VPS, create a swap file, lock its permissions, format it, switch it on, and add one line to /etc/fstab so it survives a reboot. That is five commands and about a minute. A 2 GB swap file is the right size for most servers with 2 to 8 GB of RAM. Swap does not make a server faster; it gives the kernel somewhere to put idle memory during a spike, so a busy moment slows the server down instead of killing your database.

This guide is for Ubuntu 22.04 and 24.04 and works the same on Debian 12. It covers checking what you have, adding or resizing a swap file, the one setting worth changing, and how to tell whether the server is leaning on swap too much.

Step 1: Check whether swap already exists

sudo swapon --show
free -h

If swapon --show prints nothing, there is no swap. If it prints a line, you already have some, and the Swap: row of free -h shows how much is in use. Many VPS images ship without any, so an empty answer is normal.

Check the disk has room for the file you are about to create:

df -h /

Step 2: Choose a size

RAM Swap file
1 GB 1 to 2 GB
2 to 8 GB 2 GB
16 GB or more 2 to 4 GB

More is not better. Swap is there to absorb a short spike, not to stand in for memory you do not have. A server that needs 8 GB of swap to stay up needs more RAM.

Step 3: Create and enable the swap file

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

The four commands reserve the space, make the file readable by root only, format it as swap, and switch it on. Confirm:

sudo swapon --show
NAME      TYPE SIZE USED PRIO
/swapfile file   2G   0B   -2

Warning. Do not skip chmod 600. Memory written to swap can contain passwords and keys, and a swap file readable by other users exposes them.

If swapon answers swapfile has holes, the filesystem does not support fallocate for swap. Remove the file and create it the slower way instead:

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

Then repeat the chmod, mkswap and swapon commands.

Step 4: Keep it after a reboot

Swap switched on by hand is gone after the next restart. Add it to /etc/fstab, keeping a copy of the file first:

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

Check that the file still parses, because a broken fstab can stop a server from booting:

sudo findmnt --verify

It should end with 0 parse errors, 0 errors. Warnings about the swap line are harmless.

Step 5: Set swappiness to 10

Swappiness tells the kernel how readily to move memory to swap. The default of 60 suits desktops. On a server you want memory kept in RAM until it is really needed elsewhere:

cat /proc/sys/vm/swappiness
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl --system | grep swappiness

The setting applies immediately and after every reboot. Do not set it to 0: that tells the kernel to avoid swap until it is nearly out of memory, which removes the cushion you just added.

How to tell whether swap is hurting you

Swap that is partly used is fine. Swap that is being read and written constantly is a problem, because disk is hundreds of times slower than RAM. The difference shows in two columns of vmstat:

vmstat 5 5

Look at si (swapped in) and so (swapped out). Zeros, or an occasional small number, mean the swap is doing its job quietly. Hundreds or thousands on every line mean the server is short of memory and working from disk. In that case find out what is using the RAM before you add more swap. Our guide to diagnosing memory pressure on Linux shows how to read the numbers, and sizing PHP-FPM workers is the usual fix on a web server.

Resize the swap file

To change the size later, switch swap off, recreate the file, and switch it back on. The server needs enough free RAM to hold whatever is in swap at that moment, so do it at a quiet time:

sudo swapoff /swapfile
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

The fstab line stays as it is.

Remove swap

sudo swapoff /swapfile
sudo sed -i '\|^/swapfile |d' /etc/fstab
sudo rm /swapfile

Common mistakes to avoid

  • Forgetting the fstab line. Everything works until the first reboot, and the problem that swap was hiding comes back weeks later.
  • Using swap to run software that does not fit in RAM. The site stays up and every page takes seconds.
  • Making the file huge “to be safe”. It uses disk space you pay for and hides a memory problem for longer.
  • Setting swappiness to 0 or 100. Ten is a sound value for a web or database server.
  • Editing fstab without a copy. Keep /etc/fstab.bak and run findmnt --verify after every change.

Swap on a SiteHarbour VPS

Every SiteHarbour VPS is handed over with swap, the timezone and the hostname already configured, alongside the firewall and SSH hardening, so on a new server step 1 will show a swap file in place. Plans run on NVMe storage, which makes the occasional swap access far less painful than on older disks, and you can move to a larger plan without migrating when a server has outgrown its RAM.

Frequently asked questions

Does a VPS with plenty of RAM need swap?

A small amount is still worth having. It costs a little disk space and gives the kernel a way to ride out a spike without stopping a process.

Is a swap file slower than a swap partition?

Not in any way you would notice on a modern kernel. A file is easier to create, resize and remove, which is why it is the usual choice on a VPS.

Does swap wear out an SSD or NVMe drive?

Light use does not matter. Constant heavy swapping writes a lot, but that is a sign the server needs more RAM, and it should be fixed for that reason first.

Why is swap in use when there is free RAM?

The kernel moved idle memory out earlier and has had no reason to bring it back. It is not a problem unless vmstat shows constant swap activity.

Can I have more than one swap file?

Yes. Create a second file with a different name and add a second fstab line. It is a quick way to add swap without switching the first one off.

The short version: fallocate, chmod 600, mkswap, swapon, one line in fstab, and swappiness at 10. Then watch si and so in vmstat: if they stay near zero, you are done.

Share this guide