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:

Server
$ swapon --show

free -h says the same: Swap: is 0B:

Server
$ free -h               total        used        free      shared  buff/cache   availableMem:           1.9Gi       343Mi       1.3Gi       5.0Mi       455Mi       1.6GiSwap:             0B          0B          0B
Output hidden (3 lines)

If swapon --show lists something, you already have swap; see Change the size instead. Check that the disk has room for the file under Avail:

Server
$ df -h /Filesystem      Size  Used Avail Use% Mounted on/dev/vda1        24G  2.0G   22G   9% /
Output hidden (2 lines)

Step 2: Create the swap file

Make a 2 GB file. fallocate reserves the space at once and prints nothing:

Server
$ sudo fallocate -l 2G /swapfile

Only root may read it. Swap holds whatever was in memory, passwords included:

Server
$ sudo chmod 600 /swapfile
Server
$ ls -lh /swapfile-rw------- 1 root root 2.0G Nov 18 11:52 /swapfile
Output hidden (1 line)

Format it as swap:

Server
$ sudo mkswap /swapfileSetting up swapspace version 1, size = 2 GiB (2147479552 bytes)no label, UUID=35439019-a25a-435b-aeaf-cdeec730d061
Output hidden (2 lines)

Turn it on. It prints nothing when it works:

Server
$ sudo swapon /swapfile
Server
$ swapon --showNAME      TYPE SIZE USED PRIO/swapfile file   2G   0B   -1
Output hidden (2 lines)
Server
$ free -h               total        used        free      shared  buff/cache   availableMem:           1.9Gi       347Mi       1.3Gi       5.0Mi       456Mi       1.6GiSwap:          2.0Gi          0B       2.0Gi
Output hidden (3 lines)

The 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:

Server
$ sudo cp /etc/fstab /etc/fstab.bak

Add the swap line. tee prints the line it added:

Server
$ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab/swapfile none swap sw 0 0
Output hidden (1 line)

Check the whole file for mistakes. The last line must say 0 parse errors, 0 errors:

Server
$ 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 warnings
Output hidden (6 lines)

The 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:

Server
$ sudo systemctl daemon-reload
Server
$ sudo findmnt --verifynone   [W] non-bind mount source /swapfile is a directory or regular file 0 parse errors, 0 errors, 1 warning
Output hidden (4 lines)

Now test the line itself without a reboot: turn the swap off, then let swapon -a turn on everything listed in /etc/fstab:

Server
$ sudo swapoff /swapfile
Server
$ sudo swapon -a
Server
$ swapon --showNAME      TYPE SIZE USED PRIO/swapfile file   2G   0B   -1
Output hidden (2 lines)

It 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:

Server
$ cat /proc/sys/vm/swappiness60
Output hidden (1 line)

On 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:

Server
$ echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.confvm.swappiness=10
Output hidden (1 line)

Apply it now (the file applies it at every boot):

Server
$ sudo sysctl -p /etc/sysctl.d/99-swappiness.confvm.swappiness = 10
Output hidden (1 line)

Step 5: Reboot and check

Server
$ sudo reboot

Log in again. The swap file is on and swappiness is still 10:

Server
$ swapon --showNAME      TYPE SIZE USED PRIO/swapfile file   2G   0B   -1
Output hidden (2 lines)
Server
$ cat /proc/sys/vm/swappiness10
Output hidden (1 line)

When 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:

Server
$ swapon --showNAME      TYPE SIZE USED PRIO/swapfile file   2G 428K   -1
Output hidden (2 lines)

Two errors you may see

“swapon failed: Device or resource busy”

The file is already on. Nothing is wrong; check with swapon --show:

Server
$ sudo swapon /swapfileswapon: /swapfile: swapon failed: Device or resource busy
Output hidden (1 line)

“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:

Server
$ sudo swapon /swapfileswapon: /swapfile: insecure permissions 0644, 0600 suggested.
Output hidden (1 line)

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:

Server
$ sudo swapoff /swapfile
Server
$ sudo fallocate -l 4G /swapfile

mkswap warns that it is wiping the old swap signature. That is expected:

Server
$ 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
Output hidden (3 lines)
Server
$ sudo swapon /swapfile
Server
$ swapon --showNAME      TYPE SIZE USED PRIO/swapfile file   4G   0B   -1
Output hidden (2 lines)

The /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

WhatUbuntu 26.04Ubuntu 24.04
SystemUbuntu 26.04.1 LTS7.0.0-43-genericUbuntu 24.04.5 LTS6.8.0-147-generic
util-linux2.41.3-3ubuntu2.22.39.3-9ubuntu6.6
mount2.41.3-3ubuntu2.22.39.3-9ubuntu6.6
procps2:4.0.4-9ubuntu12:4.0.4-4ubuntu3.3
linux-image-virtual7.0.0-43.436.8.0-147.147
The swap file’s priority in swapon --show (only matters with several swap areas)-1-2
ProofRun log, 37 checks passedRun log, 37 checks passed

Undo: remove the swap file

Turn it off, delete its line from /etc/fstab, and delete the file:

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

If you set swappiness, remove its file and put the default back:

Server
$ sudo rm /etc/sysctl.d/99-swappiness.conf
Server
$ sudo sysctl vm.swappiness=60vm.swappiness = 60
Output hidden (1 line)
Server
$ swapon --show