How to Add Swap Space on Ubuntu 24.04 (Step-by-Step)

Add swap space on Ubuntu 24.04 with a protected 1 GiB file, Btrfs-safe setup, boot persistence, verification, and common VPS fixes for memory headroom.
A glowing swap storage block beside an Ubuntu server with warm orange accents on a midnight-blue background.

Add swap space on Ubuntu 24.04 by creating a protected 1 GiB swap file and enabling it for the current session and future boots. You can normally finish in five to ten minutes, giving a small VPS a disk-backed buffer during short memory spikes.

Prerequisites

  • Ubuntu Server 24.04 LTS (Noble Numbat) on a VPS or dedicated server, with at least 1.3 GiB of free space on the filesystem that will hold /swapfile.
  • An SSH connection as root, or a normal user with working sudo privileges.
  • The path /swapfile must be unused; do not overwrite an existing application file or active swap area.
  • A VPSLake Linux VPS if you need an Ubuntu server for this procedure.

Swap is slower than RAM, so it is a safety margin rather than a substitute for enough memory. This guide uses 1 GiB; choose a different size only when you have measured memory pressure and enough free disk space.

Step 1: Inspect Ubuntu, the root filesystem, and current swap

Check the release, filesystem type, free disk, and existing swap before creating anything so you do not fill the system volume or replace a working configuration.

grep -E '^(PRETTY_NAME|VERSION_ID)=' /etc/os-release
findmnt -no FSTYPE /
free -h
swapon --show
df -h /
if [ -e /swapfile ]; then
  ls -lh /swapfile
else
  echo "/swapfile is available"
fi

On a fresh Ubuntu 24.04 VPS without swap, the version identifies Noble, swapon --show has no data rows, and the final check confirms that the target path is free:

PRETTY_NAME="Ubuntu 24.04.3 LTS"
VERSION_ID="24.04"
ext4
               total        used        free      shared  buff/cache   available
Mem:            2.0Gi       420Mi       1.0Gi        12Mi       580Mi       1.4Gi
Swap:              0B          0B          0B
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        40G  6.4G   32G  17% /
/swapfile is available

Your device names, minor release, and memory values will differ. If swapon --show already lists a partition or file, keep it unless you have a specific reason to add more. If /swapfile exists, stop here and choose another unused filename rather than replacing it.

The filesystem line matters because Btrfs handles swap files differently from ext4 and XFS. Use the matching creation method in the next step.

Step 2: Create and protect the swap file

Prepare a fully allocated file and restrict it to root before enabling it; the permission check prevents ordinary local users from reading memory pages that the kernel writes to disk.

ext4 or XFS

For the common ext4 or XFS root filesystem, allocate 1 GiB with fallocate and give it a swap signature:

sudo fallocate -l 1G /swapfile
sudo chown root:root /swapfile
sudo chmod 0600 /swapfile
sudo mkswap /swapfile

mkswap should report a new UUID and a size close to 1 GiB. The UUID is informational here because the persistent configuration below uses the file path:

Setting up swapspace version 1, size = 1 GiB
no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Btrfs

If Step 1 reported btrfs, use Ubuntu 24.04’s Btrfs-aware helper. It creates the non-copy-on-write, preallocated file that the kernel requires:

sudo apt update
sudo apt install -y btrfs-progs
sudo btrfs filesystem mkswapfile --size 1G /swapfile
sudo chown root:root /swapfile
sudo chmod 0600 /swapfile

Do not run both creation blocks. For the command behavior and swap-file restrictions, see Ubuntu’s mkswap manual and swapon manual.

Step 3: Enable swap now and at boot

Creating the signature does not make the kernel use the file; activate it now and add one guarded /etc/fstab entry for later boots.

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 /swapfile
sudo swapon --show

The final command should include a row similar to this:

NAME      TYPE  SIZE USED PRIO
/swapfile file    1G   0B   -2

The priority can be different when Ubuntu already has another swap area. The important results are that /swapfile appears and its size is close to 1 GiB. The conditional append prevents this procedure from adding the same boot entry twice.

Step 4: Adjust swappiness only for a measured workload

Ubuntu’s vm.swappiness value controls how readily the kernel moves inactive pages from RAM to swap, so inspect it before changing a setting that affects the whole server.

sysctl vm.swappiness

For a VPS that should prefer RAM and use swap mainly when memory pressure rises, test 10 during the current boot:

sudo sysctl vm.swappiness=10

If monitoring shows that value suits your workload, save it in a separate file and apply it immediately:

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. Frequent swapping usually means the server needs more RAM or the application needs tighter memory limits.

Verify the active and persistent configuration

Run these checks together to prove that Ubuntu is using the file, that it is root-only, and that the same path will be enabled after a reboot:

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, mode -rw-------, and one matching boot entry:

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 is often sparse, partly allocated, or a copy-on-write file on Btrfs. First run findmnt -no FSTYPE / and sudo swapon --show. If the failed file is not active, remove it and repeat the Btrfs block in Step 2 for Btrfs; on ext4 or XFS, recreate it fully with dd:

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

Only remove the file after sudo swapon --show confirms that it is not active. After the fallback succeeds, rerun the persistence commands in Step 3 if the /etc/fstab entry was not already added.

fallocate: fallocate failed: No space left on device

The filesystem containing /swapfile cannot provide 1 GiB. Check df -h /, free only data you recognize as unnecessary, or use a smaller inactive file such as 512 MiB with sudo fallocate -l 512M /swapfile; keep enough space for updates, logs, and application data.

swapon: /swapfile: swapon failed: Operation not permitted

The VPS may be running inside a container whose host does not allow guests to manage swap. This is a virtualization policy, not a missing Ubuntu package; ask the provider for host-managed swap or use a virtualization plan that permits guest swap files.

swapon: /swapfile: swapon failed: Device or resource busy

The path is already active or another process has it open. Check sudo swapon --show and ls -l /swapfile; if the file is listed as active, keep it and remove any duplicate /etc/fstab line instead of overwriting it.

FAQ

How much swap should an Ubuntu 24.04 VPS have?

There is no universal RAM-to-swap formula. A 1 GiB file is a reasonable starting point for a small VPS with enough free disk, while a memory-heavy service may need more headroom. Leave space for Ubuntu updates, logs, and application data.

Does swap increase the physical RAM in Ubuntu?

No. Swap is disk-backed virtual memory and is much slower than RAM. It can give a process time to survive a short memory spike, but sustained swap use points to a memory or workload-sizing problem.

Can I use a swap partition instead of /swapfile?

Yes. A dedicated partition can be initialized with mkswap and referenced by UUID in /etc/fstab. A file is usually easier on a VPS because you can resize it without repartitioning the virtual disk.

Can I remove the swap file without rebooting?

Yes, provided the server has enough free RAM to stop using it. Confirm the exact path with sudo swapon --show, run sudo swapoff /swapfile, remove only its line from /etc/fstab, and delete the inactive file; do not remove an active swap file.

Samrat Ghosh
Written by

Samrat Ghosh

Founder & Infrastructure Engineer

Samrat Ghosh is the founder of VPSLake, where he builds and runs the remote desktop and VPS hosting infrastructure the company is built on. He writes hands-on guides about RDP, Windows Server, VPS management and secure remote access - the practical documentation he wishes had…

Previous Article

How to Add Swap Space on Ubuntu 22.04 (Step-by-Step)

Next Article

AdsPower RDP: How Much RAM Per Profile?

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨