โ† Back to Tutorials
29 April 2026ยท4 min read

How to Add Swap Space on Ubuntu (Swap File)

Create and enable a swap file on Ubuntu 24.04, make it persistent in fstab, and tune swappiness so servers handle memory pressure gracefully.

Swap space is disk-backed memory the Linux kernel uses when physical RAM is exhausted. It is not a replacement for real memory โ€” disk is orders of magnitude slower than RAM โ€” but a correctly sized swap file is a cheap safety net that keeps a server from killing processes outright the moment it runs short on memory. On modern Ubuntu, a swap file is preferred over a swap partition because you can create, resize, or remove it without touching your disk layout.

This tutorial walks through creating a swap file on Ubuntu 24.04 LTS, enabling it, making it survive reboots via /etc/fstab, and tuning swappiness for server workloads. Every command is copy-pasteable and includes the output you should expect.

Prerequisites

  • A server or desktop running Ubuntu 24.04 LTS.
  • A user with sudo privileges.
  • Enough free disk space on the root filesystem for the swap file you plan to create.

Check whether you already have swap before adding more:

sudo swapon --show
free -h

If swapon --show prints nothing, you currently have no swap. The free -h command shows a Swap: row with totals; a line of zeros confirms the same.

Step 1: Choose a swap size and check free space

A common rule of thumb is 1โ€“2ร— RAM for small machines and a fixed 2โ€“4 GiB for larger servers, where swap is only an overflow buffer rather than primary memory. Confirm you have room first:

df -h /

Look at the Avail column for /. The example below creates a 2 GiB swap file, which is plenty for a typical VM.

Step 2: Create the swap file

On modern filesystems, allocate the file with fallocate, which is instant:

sudo fallocate -l 2G /swapfile

If your root filesystem is Btrfs, or fallocate produces a file that mkswap rejects, fall back to dd, which writes real zero blocks:

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

A swap file must be readable only by root, otherwise mkswap warns about insecure permissions:

sudo chmod 600 /swapfile

Step 3: Format and enable the swap file

Mark the file as swap space, then turn it on:

sudo mkswap /swapfile
sudo swapon /swapfile

The mkswap command prints something like:

Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=4f9d2c1a-8b6e-4f2a-9c3d-1e2f3a4b5c6d

Step 4: Verify the swap is active

Confirm the kernel now sees the swap file:

sudo swapon --show
free -h

You should see a row referencing /swapfile with TYPE of file, and the Swap: total in free -h should now reflect 2 GiB. This swap is live immediately โ€” but it will disappear on the next reboot until you complete the next step.

Step 5: Make the swap file permanent

To re-enable the swap automatically at boot, add an entry to /etc/fstab. Always back up that file first:

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

Validate the new entry without rebooting by reloading mounts and re-reading swap targets:

sudo swapoff /swapfile
sudo swapon -a
sudo swapon --show

If swapon -a brings the file back online using only the fstab entry, your configuration is correct and will survive reboots.

Step 6: Tune swappiness

swappiness controls how aggressively the kernel moves data out of RAM into swap, on a scale of 0โ€“100. The default is 60, which is tuned for desktops. On a server you usually want the kernel to keep data in RAM and only swap under genuine pressure, so a value of 10 is a common choice:

cat /proc/sys/vm/swappiness

Change it live for testing:

sudo sysctl vm.swappiness=10

Make the change persistent across reboots by adding it to a sysctl drop-in file:

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

When swap helps and when it hurts

Swap is valuable as a buffer: it lets the kernel reclaim cold pages and avoid invoking the OOM killer during a transient memory spike. It is harmful when a latency-sensitive workload (a database, a busy web app) is actively swapping hot data, because every swapped page fault hits the disk. The fix for sustained swapping is more RAM, not more swap. Watch for trouble with:

vmstat 1 5

Sustained non-zero values in the si (swap-in) and so (swap-out) columns mean the machine is genuinely short on memory and is paging actively โ€” a signal to right-size the instance.

Troubleshooting and common pitfalls

  • swapon: /swapfile: insecure permissions 0644 โ€” you skipped chmod 600. Fix the permissions and re-run mkswap.
  • fallocate file refused by mkswap on Btrfs โ€” use the dd method instead; Btrfs does not support swap files created with fallocate holes.
  • Swap gone after reboot โ€” the /etc/fstab entry is missing or mistyped. Re-check it and test with sudo swapon -a.
  • Out of disk space โ€” the swap file counts against your root filesystem. Confirm with df -h / before creating large files.

Where to go next

Swap is one piece of capacity planning. If your storage layout is rigid, consider moving to flexible volumes โ€” see our guide to creating and resizing LVM volumes on Ubuntu. In a cloud context, the same right-sizing discipline applies when you launch your first OpenStack instance and pick a flavor.

Conclusion

You created a swap file, enabled it, made it persistent in /etc/fstab, and tuned swappiness for a server profile. Swap is a safety net, not a substitute for adequate RAM โ€” if you find yourself constantly adding swap to stay alive, the real answer is more memory or a better-sized instance. clouditiv handles that capacity planning for you on a sovereign, ISOย 27001 / BSIย C5 private cloud running Ubuntu 24.04 + OpenStack 2025.2, with data kept in Germany. Compare options on our pricing page.