Add swap space on Ubuntu 22.04 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 extra disk-backed headroom during short memory spikes.
Prerequisites
- Ubuntu Server 22.04 LTS (Jammy Jellyfish) on a KVM VPS or dedicated server.
- At least 1.2 GiB of free space on the filesystem that contains
/swapfile; leave additional room for updates, logs, and application data. - An SSH connection as
root, or a regular user with workingsudoprivileges. - The path
/swapfilemust be unused. Do not overwrite an existing file or active swap area. - A VPSLake Linux VPS if you need an Ubuntu server for this procedure.
Swap is much slower than physical RAM. Treat it as a buffer for occasional pressure, not as a replacement for choosing a VPS with enough memory.
Step 1: Inspect Ubuntu, storage, and existing swap
Check the release, root filesystem, free space, and active swap first so you do not create a duplicate area or fill the system disk.
grep -E '^(PRETTY_NAME|VERSION_ID)=' /etc/os-release
findmnt -no FSTYPE /
df -h /
free -h
swapon --show
if [ -e /swapfile ]; then ls -lh /swapfile; else echo "/swapfile is available"; fi
On an Ubuntu 22.04 VPS with no existing swap, the output will resemble this:
PRETTY_NAME="Ubuntu 22.04.5 LTS"
VERSION_ID="22.04"
ext4
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 6.5G 34G 17% /
total used free shared buff/cache available
Mem: 2.0Gi 410Mi 1.1Gi 12Mi 480Mi 1.4Gi
Swap: 0B 0B 0B
/swapfile is available
The minor release, device name, memory values, and filesystem type can differ. If swapon --show already lists a partition or file, keep it unless you have a specific reason to add more. If /swapfile exists, choose another unused path such as /swapfile-1g instead of replacing it.
Ubuntu’s usual VPS filesystem is ext4, for which the standard method below is appropriate. If findmnt reports btrfs, use the Btrfs-specific creation block in Step 2 rather than fallocate.
Step 2: Create and protect a 1 GiB swap file
Allocate the file and restrict it to root before adding the swap signature; this prevents ordinary local users from reading memory pages that the kernel writes to disk.
For an ext4 or XFS root filesystem, run:
sudo fallocate -l 1G /swapfile
sudo chown root:root /swapfile
sudo chmod 0600 /swapfile
sudo mkswap /swapfile
mkswap should report a new swapspace UUID and a size close to 1 GiB:
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. Ubuntu 22.04’s Btrfs tools can create the preallocated, non-copy-on-write file that the kernel requires:
sudo apt update
sudo apt install -y btrfs-progs
sudo btrfs filesystem mkswapfile -s 1G /swapfile
sudo chmod 0600 /swapfile
The Btrfs command creates and formats the file in one operation. The Ubuntu Btrfs filesystem manual documents this helper, while the Ubuntu swap FAQ explains the permissions and activation sequence.
Step 3: Activate swap for the current boot
Creating the swap signature does not turn it on; swapon registers the finished file with the running kernel without a reboot.
sudo swapon /swapfile
sudo swapon --show
You should see a row similar to this:
NAME TYPE SIZE USED PRIO
/swapfile file 1G 0B -2
The priority can differ if Ubuntu already has another swap area. The important checks are that /swapfile appears and its size is close to 1 GiB. If activation fails, leave the file in place and use the matching troubleshooting entry rather than creating a second one.
Step 4: Persist the swap file across reboots
Add one guarded /etc/fstab entry so Ubuntu enables the same file during boot, while avoiding duplicate lines if the command is run twice.
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 grep -nE '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab
The final command should show one active entry:
/swapfile none swap sw 0 0
The swapon command from Step 3 handles the current boot. The fstab record is what makes the file available after a restart; systemctl daemon-reload refreshes systemd’s generated configuration after the file changes.
Step 5: Review swappiness before changing it
The vm.swappiness setting influences how readily Ubuntu moves inactive pages to swap, so inspect the current value and change it only when the workload justifies it.
sysctl vm.swappiness
If the VPS should prefer RAM and use swap mainly during pressure, test a value of 10 for the current boot:
sudo sysctl vm.swappiness=10
sysctl vm.swappiness
If monitoring shows that setting suits the workload, persist it 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
Expected output is:
vm.swappiness = 10
Do not set swappiness to zero just to eliminate disk activity. Constant swapping means the server needs more RAM or the application needs tighter memory limits; it is not solved by hiding the symptom.
Verify the persistent Ubuntu 22.04 swap setup
Run one final check for activation, capacity, permissions, and the boot entry. Together, these checks prove the file is usable now and configured for the next restart.
sudo swapon --show
free -h
sudo stat -c '%A %U:%G %n' /swapfile
sudo grep -E '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab
Successful output includes an active 1 GiB 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 sw 0 0
Troubleshooting
swapon: /swapfile: swapon failed: Invalid argument
The file may be sparse, partly allocated, or a copy-on-write file on Btrfs. Check the filesystem with findmnt -no FSTYPE /. On Btrfs, remove only an inactive failed file and recreate it with the Btrfs command in Step 2. On ext4 or XFS, recreate an inactive file with a fully written dd file:
sudo swapon --show
sudo rm /swapfile
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024 status=progress
sudo chown root:root /swapfile
sudo chmod 0600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
Run the rm line only when swapon --show does not list /swapfile. The dd fallback writes every block instead of relying on filesystem allocation behavior.
fallocate: fallocate failed: No space left on device
The filesystem holding /swapfile cannot provide 1 GiB. Check df -h /, preserve room for Ubuntu updates and logs, and use a smaller 512 MiB file only after confirming that a failed file is inactive:
sudo swapon --show
sudo rm /swapfile
sudo fallocate -l 512M /swapfile
sudo chown root:root /swapfile
sudo chmod 0600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
The /etc/fstab path remains valid because it does not store the file size. Never remove the file while it appears in swapon --show.
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 already listed, keep the active file and remove any duplicate fstab entry instead of overwriting it.
swapon: /swapfile: swapon failed: Operation not permitted
Some container-based VPS platforms do not let the guest manage swap. This is a virtualization policy, not a missing Ubuntu package, so ask the provider for host-managed swap or use a virtualization plan that permits guest swap files.
FAQ
How much swap should an Ubuntu 22.04 VPS have?
There is no universal RAM-to-swap formula. A 1 GiB file is a reasonable starting point for a small VPS when the disk has room; size it around short memory spikes and leave storage for the OS, logs, and application data.
Does swap increase physical RAM?
No. Swap is disk-backed virtual memory and is much slower than RAM. It can delay an out-of-memory failure during a brief spike, but sustained swap activity means the workload needs more memory or tighter limits.
Do I need to reboot after adding swap on Ubuntu 22.04?
No. swapon /swapfile activates it immediately, and the /etc/fstab entry handles future boots. Reboot only if you want to test the boot configuration during a maintenance window.
Can Ubuntu 22.04 use a swap file on Btrfs?
Yes, but a normal sparse or copy-on-write file can be rejected. Use btrfs filesystem mkswapfile as shown in Step 2, and remember that an active Btrfs swap file can affect snapshots of its containing subvolume.