Add swap space on Ubuntu 26.04 with a root-only 1 GiB file that the kernel can use when RAM briefly runs short. You can finish in about five to ten minutes, without rebooting the server during setup.
Prerequisites
- An Ubuntu 26.04 LTS VPS or dedicated server with at least 1.3 GiB of free space on the filesystem mounted at
/; keep additional room for updates and logs. - SSH access as
root, or a regular user with workingsudoprivileges. - The
/swapfilepath must be unused. Ubuntu images may already have/swap.imgor another swap device, so check before creating anything. - A VPSLake Linux VPS if you need an Ubuntu server for this procedure.
Swap is slower than RAM and does not increase physical memory. It is a safety margin for temporary pressure, not a fix for a workload that continuously exhausts the server.
Step 1: Inspect Ubuntu 26.04 and the current storage state
Check the release, root filesystem, free disk space, and active swap first so you do not overwrite an existing file or fill the root volume.
grep -E '^(PRETTY_NAME|VERSION_ID)=' /etc/os-release
findmnt -no FSTYPE /
df -h /
free -h
swapon --show
test -e /swapfile && ls -lh /swapfile || echo "/swapfile is available"
On a typical fresh Ubuntu 26.04 VPS without an active swap file, you will see VERSION_ID="26.04", a filesystem such as ext4, an empty swapon --show table, and this final message. Canonical identifies this LTS release as Resolute Raccoon in the Ubuntu 26.04 release notes:
PRETTY_NAME="Ubuntu 26.04 LTS"
VERSION_ID="26.04"
ext4
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 6.4G 32G 17% /
total used free shared buff/cache available
Mem: 2.0Gi 420Mi 1.0Gi 12Mi 580Mi 1.4Gi
Swap: 0B 0B 0B
/swapfile is available
Your device name, memory values, and filesystem can differ. If swapon --show already lists /swap.img or a partition, you already have swap; add another file only if the workload needs more. If /swapfile exists, choose another unused path rather than replacing it.
Step 2: Create and secure a 1 GiB swap file
Allocate the file and restrict it to root before writing the swap signature; this prevents ordinary users from reading pages that the kernel moves to disk.
For an ext4 or xfs root filesystem, run this block:
sudo fallocate -l 1G /swapfile
sudo chown root:root /swapfile
sudo chmod 0600 /swapfile
sudo mkswap /swapfile
mkswap should report a swap area close to 1 GiB and print a generated UUID:
Setting up swapspace version 1, size = 1 GiB
no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
If Step 1 reported btrfs, do not use fallocate alone. Install the Btrfs tools from Ubuntu’s repositories and let their helper create a preallocated swap file that Btrfs can use:
sudo apt update
sudo apt install -y btrfs-progs
sudo btrfs filesystem mkswapfile --size 1G /swapfile
sudo chmod 0600 /swapfile
Run only one of the two creation blocks. Ubuntu’s swapon documentation describes the kernel requirements for swap files, including why filesystem layout matters.
Step 3: Enable the new swap immediately
Formatting a file prepares it but does not make the running kernel use it; swapon activates it without a restart.
sudo swapon /swapfile
sudo swapon --show
free -h
The new file should appear as an active row and the Swap total should be close to 1 GiB:
NAME TYPE SIZE USED PRIO
/swapfile file 1G 0B -2
Swap: 1.0Gi 0B 1.0Gi
The priority may differ when another swap area is already enabled. The important checks are that /swapfile is listed and that its size is nonzero.
Step 4: Persist swap across reboots
Add one /etc/fstab record so Ubuntu enables this same file during boot, while the guard avoids adding a duplicate entry if you rerun the command.
if ! sudo grep -qE '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab; then
echo '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab
fi
sudo systemctl daemon-reload
sudo grep -nE '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab
The last command should show one active line:
/swapfile none swap defaults 0 0
Swap is already active for this session from Step 3. The fstab entry is what restores it after a reboot; systemctl daemon-reload refreshes systemd’s generated view of the changed file.
Step 5: Review swappiness before tuning it
The vm.swappiness value influences how readily Ubuntu moves inactive pages to swap, so inspect it before changing the kernel’s default behavior.
sysctl vm.swappiness
For a small VPS where swap should mainly absorb brief memory pressure, you can test a value of 10 for the current boot:
sudo sysctl vm.swappiness=10
If monitoring shows that value suits the workload, make it persistent in a dedicated file:
printf '%s\n' 'vm.swappiness = 10' | sudo tee /etc/sysctl.d/60-swap.conf
sudo sysctl -p /etc/sysctl.d/60-swap.conf
sysctl vm.swappiness
Expected output is:
vm.swappiness = 10
Do not set swappiness to zero simply to hide swap activity. If the server swaps constantly, reduce the application’s memory use or move to a plan with more RAM.
Verify the persistent Ubuntu 26.04 swap setup
Run these checks together to prove that the kernel is using the file, that its permissions are safe, and that Ubuntu has a boot-time entry for it.
sudo swapon --show
free -h
sudo stat -c '%A %U:%G %n' /swapfile
sudo grep -E '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab
Successful output contains an active file, a nonzero swap total, root-only permissions, and one matching fstab line:
NAME TYPE SIZE USED PRIO
/swapfile file 1G 0B -2
Swap: 1.0Gi 0B 1.0Gi
-rw------- root:root /swapfile
/swapfile none swap defaults 0 0
Troubleshooting
swapon: /swapfile: swapon failed: Invalid argument
The file may be sparse, partially allocated, or on Btrfs without the required swap-file layout. Confirm the filesystem with findmnt -no FSTYPE /; if it is Btrfs, recreate the inactive file with btrfs filesystem mkswapfile from Step 2. On ext4 or xfs, recreate an inactive file with a fully written allocation:
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024 status=progress
sudo chmod 0600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
Before running that block, confirm /swapfile is not listed by sudo swapon --show. If it is active, do not overwrite it.
fallocate: fallocate failed: No space left on device
The root filesystem cannot provide 1 GiB. Check df -h /, preserve room for Ubuntu updates and logs, and use a smaller inactive file such as 512 MiB. Remove a failed file only after sudo swapon --show confirms it is not active, then repeat Step 2 with 512M and count=512 where applicable.
mount -a reports a bad /etc/fstab entry
An incorrectly edited fstab line can prevent the boot-time activation from being parsed. Check the line with sudo grep -nE '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab, keep exactly /swapfile none swap defaults 0 0, and run sudo systemctl daemon-reload before testing sudo swapon --all --verbose.
swapon: /swapfile: swapon failed: Operation not permitted
The virtualization layer may prohibit guest-managed swap, which cannot be fixed by reinstalling Ubuntu packages. Keep the file inactive and contact VPSLake support or use a VPS configuration that permits swap files.
FAQ
Why does Ubuntu 26.04 show /swap.img instead of /swapfile?
The filename is chosen by the image or installer, and the name does not change how swap works. If /swap.img appears in swapon --show, it is already active; keep it and size any additional swap based on actual memory pressure.
How large should the Ubuntu 26.04 swap file be?
Choose a size for the workload’s short-lived memory peaks rather than applying a fixed RAM multiplier. One GiB is a sensible starting point on a small VPS if the root filesystem has room; leave storage for updates, logs, and application data.
Will swap increase my VPS’s physical RAM?
No. Swap uses storage as slower virtual memory and can postpone an out-of-memory failure during a brief spike. Sustained swapping usually means the workload needs more RAM or stricter memory limits.
Can I remove the swap file later?
Yes, but first confirm its exact path with sudo swapon --show, run sudo swapoff /swapfile, and remove only its /etc/fstab line. Delete the file after it is inactive; never remove an active swap file.