Mastodon

How to Add Swap Space on a Ubuntu VPS

  1. Home
  2. Guides
  3. How to Add Swap Space on a Ubuntu VPS

Swap is disk space that the Linux kernel uses as overflow memory once RAM is fully committed. It is slower than RAM by orders of magnitude, so it is not a substitute for sizing your VPS correctly for your workload, but it acts as a safety net: when a process needs a burst of memory it would otherwise be denied, the kernel can page some inactive memory out to swap and grant that request instead of triggering the out-of-memory killer, which is Linux's blunt way of freeing memory by terminating processes outright.

Most fresh VPS images ship with no swap configured at all, on the assumption that you will size your plan to your workload. In practice, a small amount of swap is worth having even on a properly sized server. A package upgrade, a Composer install, or a WordPress admin task that briefly spikes memory usage are exactly the kind of short bursts swap is built for, and having it in place turns those moments into a brief slowdown instead of a killed process.

By the end of this guide you will have a swap file created, enabled, and configured to survive a reboot, sized appropriately for your VPS's RAM. This assumes you have completed the Ubuntu 24.04 initial setup guide and are working from a non-root user with sudo access.

Step 1 - Checking Existing Swap and Disk Space

Before creating anything, confirm no swap is already configured and that you have enough free disk space to spare. Check current memory and swap usage:

free -h

Output:

               total        used        free      shared  buff/cache   available
Mem:           3.8Gi       612Mi       2.9Gi       1.0Mi       280Mi       3.0Gi
Swap:             0B          0B          0B

A Swap line reading 0B across the board confirms there is currently none configured. Next, check available disk space, since the swap file needs to fit on top of everything else already on the disk:

df -h /

With current usage confirmed, the next step decides how large the swap file should be.

Step 2 - Choosing a Size and Creating the Swap File

Swap size scales with RAM, but not linearly. On a VPS with 4GB of RAM or less, matching or doubling your RAM in swap gives a meaningful safety margin. On 8GB and above, a flat 2 to 4GB is usually enough; you are covering short spikes, not trying to run your working set from disk. For Arcadia's Essential plan (4GB RAM), a 2GB swap file is a reasonable default; for the Standard plan (8GB RAM) and above, 2GB still covers most spike scenarios without using disk space you would rather keep for backups and uploads.

Create a 2GB swap file:

sudo fallocate -l 2G /swapfile

If fallocate reports an error (this can happen on some filesystem configurations), create the file with dd instead, which is slower but works everywhere:

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

Either command leaves you with a 2GB file at /swapfile ready to be secured and formatted.

Step 3 - Securing the Swap File

Swap can transiently hold sensitive memory contents, including things like passwords or session keys that happened to be in RAM when a page was written out. Restrict the file to root before it is enabled, so no other user on the system can read it:

sudo chmod 600 /swapfile

With permissions locked down, the file is safe to format as swap space.

Step 4 - Formatting and Enabling the Swap File

Format the file as swap space:

sudo mkswap /swapfile

Output:

Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=3f8e1a2b-9c4d-4e5f-8a1b-2c3d4e5f6a7b

Enable it:

sudo swapon /swapfile

Confirm it is active:

free -h

Output:

               total        used        free      shared  buff/cache   available
Mem:           3.8Gi       615Mi       2.8Gi       1.0Mi       280Mi       3.0Gi
Swap:          2.0Gi          0B       2.0Gi

The Swap line now shows 2.0Gi available. This swap is active for the current session, but it will not survive a reboot until the next step makes it permanent.

Step 5 - Making the Swap Permanent

A malformed entry in /etc/fstab can prevent the server from mounting its filesystems correctly on boot, so back up the file before editing it:

sudo cp /etc/fstab /etc/fstab.bak

Add the swap file to fstab so it is enabled automatically on every boot:

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

Test the fstab entry without rebooting by remounting everything it defines:

sudo mount -a

If this command returns without any output, the entry is valid. If it reports an error, restore the backup with sudo cp /etc/fstab.bak /etc/fstab and check the line you added for typos before trying again. With the entry confirmed, the swap file will be enabled automatically on every future boot.

Step 6 - Tuning Swappiness

vm.swappiness controls how aggressively the kernel moves memory to swap versus reclaiming it from the page cache. The default value of 60 is tuned for desktop workloads and swaps more readily than you want on a server, where you would rather keep application memory in RAM and only fall back to swap under genuine pressure. Lower the value to reduce swap usage under normal conditions:

sudo sysctl vm.swappiness=10

This applies immediately but resets on reboot. Make it permanent:

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

The kernel will now favour keeping active memory in RAM and only reach for swap when it is genuinely needed.

Your VPS Has Swap Configured

You now have a properly sized swap file that is enabled, secured, and set to survive a reboot, with swappiness tuned for a server workload rather than a desktop one. Short memory spikes from package upgrades, builds, or admin tasks will now be absorbed instead of triggering the out-of-memory killer. From here, keep an eye on how much of that swap actually gets used; the VPS resource monitoring guide covers watching memory, CPU, and disk usage over time and knowing when it is time to upgrade the plan itself rather than lean further on swap.

Get a VPS

Follow along with your own server. Plans from £7.98/month.

View VPS Plans

More Guides

How to Migrate WordPress from Shared Hosting to a VPS

Move a live WordPress site from shared hosting to a VPS: database export with my...

How to Set Up a Fresh Ubuntu 24.04 VPS

Step-by-step: configure a fresh Ubuntu 24.04 VPS from first SSH login to a secur...

Securing Your VPS with UFW and Fail2Ban

How to configure UFW and Fail2Ban on Ubuntu to protect your VPS from port scans...