Add swap space on Ubuntu 25.10 by creating a protected 1 GiB swap file and enabling it for the current session and future boots. You can finish in about five to ten minutes, giving a small VPS a disk-backed buffer for short memory spikes without restarting it.
Prerequisites
- Ubuntu Server 25.10 (Questing) on a VPS or dedicated server, with at least 1.5 GiB of free space on
/for the example file. - SSH access as
root, or a regular user with workingsudoprivileges. - The target path
/swapfilemust not be an existing application file or an active swap area. - A VPSLake Linux VPS if you need an Ubuntu server for this procedure.
Ubuntu 25.10 is an interim release. For a new internet-facing server, choose a currently supported Ubuntu release; use this procedure when maintaining an existing 25.10 system.
Swap is slower than RAM, so use it as protection against short-lived pressure rather than as a replacement for memory. No inbound port is opened by this guide, so there is no firewall rule to add.
Step 1: Inspect Ubuntu 25.10, storage, and existing swap
Check the release, root filesystem, free disk space, and current swap before creating anything so you do not duplicate swap or fill the system disk.
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 clean Ubuntu 25.10 VPS, the release identifies 25.10, swapon --show has no data rows, and the final check confirms that the path is available:
PRETTY_NAME="Ubuntu 25.10 (Questing Quokka)"
VERSION_ID="25.10"
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 names and memory values will differ. If any swap device or file is already listed, keep it and decide whether extra capacity is actually needed. If /swapfile exists, choose another unused name instead of overwriting it.
Step 2: Create a protected 1 GiB swap file
Prepare the file and restrict it to root before Linux uses it, which prevents ordinary users from reading memory pages that may be written to disk.
For an ext4, xfs, or other normal root filesystem, run:
sudo fallocate -l 1G /swapfile
sudo chmod 0600 /swapfile
sudo mkswap /swapfile
mkswap should report a 1 GiB area 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 the fallocate block by itself. Mark the empty file as non-copy-on-write before allocating it, then format it as swap:
sudo truncate -s 0 /swapfile
sudo chattr +C /swapfile
sudo fallocate -l 1G /swapfile
sudo chmod 0600 /swapfile
sudo mkswap /swapfile
The Ubuntu Btrfs manual documents why a swap file needs preallocated, non-copy-on-write extents. Run only one creation method, and never replace an existing active file.
Step 3: Enable swap now and at boot
Formatting the file only adds its swap signature; swapon activates it immediately, while the /etc/fstab entry restores it after a reboot.
sudo swapon /swapfile
if ! sudo grep -qE '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab; then
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
fi
sudo systemctl daemon-reload
sudo swapon --show
The last command should show /swapfile as an active file:
NAME TYPE SIZE USED PRIO
/swapfile file 1G 0B -2
The priority can differ when another swap area is already enabled. The guard prevents this command from adding a second identical line to /etc/fstab.
Step 4: Review memory pressure before tuning swappiness
vm.swappiness influences how readily Ubuntu moves inactive memory pages to disk, so inspect real pressure before changing the distribution default.
sysctl vm.swappiness
free -h
vmstat 1 5
You should see the current swappiness value, a nonzero Swap total, and usually zero si/so on an idle server:
vm.swappiness = 60
Swap: 1.0Gi 0B 1.0Gi
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
si so
0 0
The vmstat columns si and so show swap-in and swap-out activity. A quiet server can legitimately show zero swap use; an unused swap file is not a setup failure.
If this VPS should strongly prefer RAM and you have a workload reason to test a lower value, apply 10 for the current boot:
sudo sysctl vm.swappiness=10
If monitoring supports that choice, make it persistent in a separate 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 just to eliminate disk activity. Repeated swapping usually means the server needs more RAM or the application needs tighter memory limits.
Verify the persistent swap setup
Run one final check for activation, capacity, permissions, and the boot entry; together these prove the file works now and is configured for the next restart.
sudo swapon --show
free -h
sudo stat -c '%A %U:%G %s %n' /swapfile
sudo grep -nE '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab
Successful output includes a 1 GiB active file, 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 1073741824 /swapfile
1:/swapfile none swap sw 0 0
For Ubuntu’s explanation of swap files, sizing, and activation, see the Ubuntu Swap FAQ.
Troubleshooting
swapon: /swapfile: swapon failed: Invalid argument
The file is often sparse or a copy-on-write file on Btrfs. Confirm the filesystem with findmnt -no FSTYPE /; if it is Btrfs, remove only an inactive failed file and rerun the Btrfs sequence from Step 2. On a normal filesystem, recreate the inactive file with fallocate, chmod, and mkswap again.
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:
sudo swapon --show
sudo rm /swapfile
sudo fallocate -l 512M /swapfile
sudo chmod 0600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
Run the rm command only when swapon --show confirms that /swapfile is not active. The existing /etc/fstab path does not need to change.
swapon: /swapfile: swapon failed: Device or resource busy
The path is already active or another process has it open. Run sudo swapon --show and ls -l /swapfile; if it is listed as active, keep it and remove any duplicate fstab entry instead of overwriting the file.
swapon: /swapfile: swapon failed: Operation not permitted
Some container-based VPS environments do not allow the guest to manage swap. This is a virtualization policy, not a missing Ubuntu package; ask the provider for host-managed swap or use a plan that permits guest swap files.
FAQ
Is 1 GiB enough swap for Ubuntu 25.10?
It is a reasonable starting point for a small VPS with enough disk space, especially when swap is only meant to absorb brief memory spikes. Size it around the workload and keep free storage for system updates, logs, and application data.
Should I create /swapfile if Ubuntu already has /swap.img?
Usually not. Check the size and usage with swapon --show first; an existing active swap file already provides the feature. Add a second file only when you have a clear capacity requirement, and use a different path.
Can a VPS hibernate with a swap file?
Hibernation requires enough swap for the system’s memory and support from the kernel, boot configuration, and virtualization platform. Most hosted VPS plans do not expose suspend-to-disk, so this guide targets memory headroom rather than hibernation.
Do I need to reboot after adding swap on Ubuntu 25.10?
No. swapon /swapfile enables it immediately, and the fstab record handles future boots. A reboot is only useful if you want to test the boot path during a maintenance window.