Skip to content

How to Add Swap Space on Ubuntu 24.04 and 26.04

Updated 4 min read

Updated: 22 October 2026 · Applies to: Ubuntu 24.04 LTS (util-linux 2.39) and Ubuntu 26.04 LTS (util-linux 2.41); steps checked in Ubuntu containers and on a Linux 6.12 kernel

To add swap space on Ubuntu, create a swap file and turn it on: on Ubuntu 26.04 run sudo mkswap --file --size 2G /swapfile, on Ubuntu 24.04 run sudo fallocate -l 2G /swapfile, sudo chmod 600 /swapfile and sudo mkswap /swapfile; then sudo swapon /swapfile and add the file to /etc/fstab. This page covers the Ubuntu specifics: where Ubuntu keeps its own swap file, the difference between 24.04 and 26.04, and setting swappiness the Ubuntu way.

Ubuntu 16.04, in the old title of this article, is no longer in standard support; upgrade to a current LTS release. On other distributions, follow How to Create a Swap File in Linux, which also explains each step in more detail.

1. Check whether Ubuntu already has swap

swapon --show

Ubuntu Server's installer normally creates a swap file called /swap.img; according to the installer (curtin) documentation, it sizes it automatically up to 8 GB. If you see /swap.img and only need more, do not create a second file by default: grow it with How to Increase Swap Space in Linux. No output means there is no swap yet. Ubuntu 24.04 and 26.04 do not turn on compressed swap in RAM (zram) by default, unlike Fedora, so nothing else will show up.

Also check the free disk space:

df -h /

2. Create the swap file

On Ubuntu 26.04, mkswap creates the file, sizes it, sets mode 0600 and formats it in one step:

sudo mkswap --file --size 2G /swapfile

On Ubuntu 24.04, that option does not exist yet (mkswap: unrecognized option '--file'), so use three commands:

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

Output of mkswap (lab example with a 64 MB file on Ubuntu 24.04):

Setting up swapspace version 1, size = 64 MiB (67104768 bytes)
no label, UUID=dd98bc64-7d47-4e94-8240-c959a46b3ba2

Pick the size with How Much Swap Should You Use in Linux?; on a VPS, 1 to 2 GB is usually enough as a safety net.

3. Turn it on

sudo swapon /swapfile
swapon --show

Output (lab example with a 1 GB file):

NAME      TYPE  SIZE USED PRIO
/swapfile file 1024M   0B   -2

4. Keep it after a reboot

Append the line in the same style the Ubuntu installer uses for /swap.img:

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

Then reload systemd and test the line without rebooting. swapon --show should list /swapfile again:

sudo systemctl daemon-reload
sudo swapoff /swapfile
sudo swapon -a
swapon --show

5. Set swappiness (optional)

vm.swappiness decides how readily the kernel swaps. According to the kernel documentation, the range is 0 to 200 and the default is 60. A common choice on servers is 10: lower values make the kernel prefer dropping file cache over swapping out programs. Check the current value:

sysctl vm.swappiness

Ubuntu reads its settings from files in /etc/sysctl.d/. Create one for swappiness and load it:

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

The output lists every file it applies and ends with vm.swappiness = 10. Change it only if monitoring shows that the server swaps out programs while it still has plenty of cache; see How to Check Swap Usage in Linux and Find What Uses It.

Ubuntu notes

Official documentation: mkswap(8) and kernel vm sysctl documentation.

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Linux server management

hardening, patching, monitoring and automation for Ubuntu, Debian, RHEL and AlmaLinux.

Was this answer helpful?

Still stuck? Open a ticket