Short answer
Create the file with sudo fallocate -l 2G /swapfile, then sudo chmod 600 /swapfile, sudo mkswap /swapfile and sudo swapon /swapfile. Add /swapfile none swap sw 0 0 to /etc/fstab so it comes back after a reboot. Check it with swapon --show.
Tested on Ubuntu 26.04 and 24.04, two separate runs. On 24.04, every step is the same. Everything that differed
Before you start
- An Ubuntu 26.04 or 24.04 server and a user with sudo (create one).
- Swap is disk space the kernel uses when RAM runs out. It is much slower than RAM, but a small server with swap slows down under load instead of having programs killed.
- This was tested on a server with 2 GB of RAM. 2 GB of swap is a good size for a server with 1 to 4 GB of RAM.
Step 1: Check whether you already have swap
A new server from Ubuntu’s cloud image has no swap at all. swapon --show prints nothing:
$ swapon --showfree -h says the same: Swap: is 0B:
$ free -h total used free shared buff/cache availableMem: 1.9Gi 343Mi 1.3Gi 5.0Mi 455Mi 1.6GiSwap: 0B 0B 0BIf swapon --show lists something, you already have swap; see Change the size instead. Check that the disk has room for the file under Avail:
$ df -h /Filesystem Size Used Avail Use% Mounted on/dev/vda1 24G 2.0G 22G 9% /Step 2: Create the swap file
Make a 2 GB file. fallocate reserves the space at once and prints nothing:
$ sudo fallocate -l 2G /swapfileOnly root may read it. Swap holds whatever was in memory, passwords included:
$ sudo chmod 600 /swapfile$ ls -lh /swapfile-rw------- 1 root root 2.0G Nov 18 11:52 /swapfileFormat it as swap:
$ sudo mkswap /swapfileSetting up swapspace version 1, size = 2 GiB (2147479552 bytes)no label, UUID=35439019-a25a-435b-aeaf-cdeec730d061Turn it on. It prints nothing when it works:
$ sudo swapon /swapfile$ swapon --showNAME TYPE SIZE USED PRIO/swapfile file 2G 0B -1$ free -h total used free shared buff/cache availableMem: 1.9Gi 347Mi 1.3Gi 5.0Mi 456Mi 1.6GiSwap: 2.0Gi 0B 2.0GiThe server now has 2 GB of swap. USED is 0B because nothing needs it yet. This lasts only until the next reboot.
Step 3: Keep it after a reboot
Back up /etc/fstab first. A broken line in it can stop the server from booting:
$ sudo cp /etc/fstab /etc/fstab.bakAdd the swap line. tee prints the line it added:
$ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab/swapfile none swap sw 0 0Check the whole file for mistakes. The last line must say 0 parse errors, 0 errors:
$ sudo findmnt --verifynone [W] non-bind mount source /swapfile is a directory or regular file [W] your fstab has been modified, but systemd still uses the old version; use 'systemctl daemon-reload' to reload 0 parse errors, 0 errors, 2 warningsThe two warnings are not errors. The first only says the swap is a file, not a disk partition, which is what you want. The second says systemd still has the old copy of /etc/fstab. Reload it:
$ sudo systemctl daemon-reload$ sudo findmnt --verifynone [W] non-bind mount source /swapfile is a directory or regular file 0 parse errors, 0 errors, 1 warningNow test the line itself without a reboot: turn the swap off, then let swapon -a turn on everything listed in /etc/fstab:
$ sudo swapoff /swapfile$ sudo swapon -a$ swapon --showNAME TYPE SIZE USED PRIO/swapfile file 2G 0B -1It is back, from /etc/fstab alone, so it will come back after a reboot too.
Step 4: Set swappiness (optional)
Swappiness tells the kernel how readily to use swap, from 0 to 200. Ubuntu’s default is 60:
$ cat /proc/sys/vm/swappiness60On a server, 10 is common: the kernel keeps programs in RAM longer and frees file cache first. Put the setting in its own file in /etc/sysctl.d/. Many guides append it to /etc/sysctl.conf instead, which adds a second copy each time you run the command:
$ echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.confvm.swappiness=10Apply it now (the file applies it at every boot):
$ sudo sysctl -p /etc/sysctl.d/99-swappiness.confvm.swappiness = 10Step 5: Reboot and check
$ sudo rebootLog in again. The swap file is on and swappiness is still 10:
$ swapon --showNAME TYPE SIZE USED PRIO/swapfile file 2G 0B -1$ cat /proc/sys/vm/swappiness10When a program needs more memory than the server has, the kernel moves pages it has not used lately into the file. In the test a program took more than the 2 GB of RAM: the kernel wrote about 700 MB to swap, and most of it was freed when the program ended. A little stays in swap until it is needed again, so a small USED value is normal:
$ swapon --showNAME TYPE SIZE USED PRIO/swapfile file 2G 428K -1Two errors you may see
“swapon failed: Device or resource busy”
The file is already on. Nothing is wrong; check with swapon --show:
$ sudo swapon /swapfileswapon: /swapfile: swapon failed: Device or resource busy“insecure permissions 0644, 0600 suggested”
You skipped chmod 600, so every user on the server can read what the kernel writes to swap. swapon still turns it on, so this warning is the only sign:
$ sudo swapon /swapfileswapon: /swapfile: insecure permissions 0644, 0600 suggested.Fix it with sudo chmod 600 /swapfile. The swap can stay on.
Change the size
Turn the file off, make it bigger, format it again and turn it on. Turning it off moves what is in it back into RAM, so do it when the server is not busy:
$ sudo swapoff /swapfile$ sudo fallocate -l 4G /swapfilemkswap warns that it is wiping the old swap signature. That is expected:
$ sudo mkswap /swapfilemkswap: /swapfile: warning: wiping old swap signature.Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)no label, UUID=745b783b-f8d4-48e5-acc6-f54b1bc92243$ sudo swapon /swapfile$ swapon --showNAME TYPE SIZE USED PRIO/swapfile file 4G 0B -1The /etc/fstab line names the file, not its size, so it needs no change.
What these steps change on the server
Both runs recorded the server before the first step and after the last (see the Tested on box below): /etc/fstab changed, /etc/fstab.bak and /etc/sysctl.d/99-swappiness.conf were added, and the disk holds about 4 GB more, the swap file after the resize. No package, service or open port changed.
Ubuntu 26.04 vs 24.04: what changed
| What | Ubuntu 26.04 | Ubuntu 24.04 |
|---|---|---|
| System | Ubuntu 26.04.1 LTS7.0.0-43-generic | Ubuntu 24.04.5 LTS6.8.0-147-generic |
| util-linux | 2.41.3-3ubuntu2.2 | 2.39.3-9ubuntu6.6 |
| mount | 2.41.3-3ubuntu2.2 | 2.39.3-9ubuntu6.6 |
| procps | 2:4.0.4-9ubuntu1 | 2:4.0.4-4ubuntu3.3 |
| linux-image-virtual | 7.0.0-43.43 | 6.8.0-147.147 |
The swap file’s priority in swapon --show (only matters with several swap areas) | -1 | -2 |
| Proof | Run log, 37 checks passed | Run log, 37 checks passed |
Undo: remove the swap file
Turn it off, delete its line from /etc/fstab, and delete the file:
$ sudo swapoff /swapfile$ sudo sed -i '\|^/swapfile |d' /etc/fstab$ sudo rm /swapfileIf you set swappiness, remove its file and put the default back:
$ sudo rm /etc/sysctl.d/99-swappiness.conf$ sudo sysctl vm.swappiness=60vm.swappiness = 60$ swapon --show