How to Add Swap on Ubuntu: Size and Swappiness
Add swap on Ubuntu 24.04 with a swap file: size it from Ubuntu's guidance, make it permanent in fstab safely, tune swappiness and know when to add RAM.
This guide shows how to add swap on Ubuntu 24.04 LTS with a swap file, for a small VPS that runs short of memory during builds or database spikes, or where the OOM killer has stopped a process. You will check the server, size and create the file, make it permanent without risking the next boot and tune swappiness; the commands are the same on Debian 12.
What swap does and what it does not do
Swap is disk space that the kernel uses as an overflow for RAM. When active processes need memory and too little is free, inactive pages move to swap, which frees that RAM for other uses. Swap is slower than RAM, and Ubuntu's SwapFaq warns not to treat it as a complete replacement for physical memory. [1]
It helps in two cases:
- Idle memory. Pages that large programs use only at start-up, and memory reserved but not really used, can move to swap and leave the RAM to other programs and the disk cache. [1]
- Unexpected peaks. When a program goes wrong or a job needs far more memory than planned, swap gives you time to find out what happened. [1]
It does not help when the work itself needs more memory than the server has. Heavy swap use slows a system down, because swap competes with normal work for the same disk, and even solid-state storage is slower than RAM. [1] When memory runs out completely, the kernel's OOM killer kills a task it picks by heuristics, normally a memory-hungry process. [2]
Check memory, swap and disk space first
Three read-only commands show what the server has today:
free -h
sudo swapon --show
findmnt -D /free -h shows used and free RAM and swap in human-readable units; its available column estimates the memory left for starting new applications without swapping. [3] swapon --show prints a table of swap areas [4]; empty output means no swap is active, and a swap file that already exists can be resized instead. findmnt -D / shows the type (FSTYPE) and free space (AVAIL) of the root filesystem. [5] The type decides how you create the file. On Debian 12, run the commands as root if your account has no sudo.
How big should the swap file be?
For a system with more than 1 GB of RAM that does not hibernate, Ubuntu's SwapFaq recommends at least the square root of the RAM size, rounded, and at most twice the RAM. Below 1 GB, use at least as much swap as RAM and at most twice that. [1] These rows come from the FAQ's table: [1]
| RAM | No hibernation | With hibernation | Maximum |
|---|---|---|---|
| 1 GB | 1 GB | 2 GB | 2 GB |
| 2 GB | 1 GB | 3 GB | 4 GB |
| 4 GB | 2 GB | 6 GB | 8 GB |
| 8 GB | 3 GB | 11 GB | 16 GB |
| 16 GB | 4 GB | 20 GB | 32 GB |
Hibernation needs more because it writes the contents of RAM to swap before the machine powers off. [1] You do not normally hibernate a VPS, so the "No hibernation" column is your minimum. Unused swap only costs disk space, but if you would need more than twice your RAM, the FAQ says to add RAM instead. [1] The 2 GiB used below suits a 2 GB or 4 GB server.
How to add swap on Ubuntu with a swap file
Create the file, restrict it to root, format and enable it, then list it in /etc/fstab and test the entry. You do not need to change any partitions, because swap can live in an ordinary file. [1]
1. Create the swap file
fallocate is the quick way: on filesystems that support it, it allocates the blocks without writing any data, which is much faster than filling a file with zeros. -l 2G means 2 GiB. [6]
sudo fallocate -l 2G /swapfileThe catch: swapon rejects files with holes, because the kernel writes to a swap file directly, and a preallocated file may count as one on some filesystems. The swapon manual calls dd with /dev/zero the most portable method [4], and the Ubuntu 24.04 mkswap manual uses it too. [7] It is slower, because it fills the file with zeros. [6]
sudo dd if=/dev/zero of=/swapfile bs=1MiB count=$((2*1024))If swapon refuses a file made with fallocate in step 3, delete it and create it again with dd.
On Btrfs, swap files work since Linux 5.0 only on files with the nocow attribute, and the swapon manual points to btrfs(5) for how to create one. [4]
2. Restrict the file to root
Set the permissions to 600 so other users cannot read the file, because swap can hold sensitive information. [1]
sudo chmod 600 /swapfile3. Format and enable the swap file
mkswap sets up a Linux swap area in the file, and swapon starts using it. [7]
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon --show
free -hThe file should appear in the swapon --show table, and the Swap line of free -h should show its size. Swap areas are enabled at boot when they are listed in /etc/fstab, which is the next step. [7]
4. Add the swap file to /etc/fstab
An fstab line has six fields. For a swap file, they are the path of the file, none, swap and the options, usually defaults; the last two fields count as zero when they are missing. [8]
/etc/fstab also describes the filesystems the server can mount, not only swap. [8] A typo, or tee without -a, which overwrites the whole file, can stop the server from booting normally, and on a VPS you may then need your provider's console to repair it. Back up the file, append one line and run step 5 before any reboot.
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab5. Test the fstab entry before you reboot
findmnt --verify checks that /etc/fstab can be parsed and used. [5] On systemd systems, the fstab manual recommends systemctl daemon-reload after you edit the file. [8] Then switch the swap file off and run swapon -a, which enables every swap entry in /etc/fstab, so the file only comes back if its entry works. [4]
sudo findmnt --verify
sudo systemctl daemon-reload
sudo swapoff /swapfile
sudo swapon -a
sudo swapon --showIf findmnt reports an error, fix the line or restore the backup with sudo cp /etc/fstab.bak /etc/fstab. Reboot when it suits you and run sudo swapon --show again.
Swappiness: what it is and how to change it
Swappiness tells the kernel how costly swap IO is compared with filesystem IO when it frees memory. It runs from 0 to 200: at 100 both count as equal, lower values make swap look more costly and higher values make it look less costly. The default is 60. [2] The kernel documentation adds that filesystem IO under memory pressure tends to be more efficient than swap's random IO, that the best value depends on the workload, and that at 0 the kernel does not start swapping until free and file-backed pages drop below a zone's high watermark. [2]
Ubuntu's SwapFaq suggests 10 for desktops, but says the default of 60 is likely more suitable for Ubuntu server installations. [1] To check the value and try another one until the next reboot (10 is only the FAQ's example): [1]
cat /proc/sys/vm/swappiness
sudo sysctl vm.swappiness=10To keep a value, put it in a .conf file in /etc/sysctl.d/, which systemd-sysctl reads at boot. The sysctl.d manual suggests a two-digit prefix from 60 to 90 for files in /etc; if two files set the same key, the one whose name sorts last wins, so check for others. [9]
echo 'vm.swappiness = 10' | sudo tee /etc/sysctl.d/60-swappiness.conf
grep -rs swappiness /etc/sysctl.conf /etc/sysctl.d/Leave vm.vfs_cache_pressure, which sets how readily the kernel reclaims its directory and inode caches, at its default. At 0, those caches are never reclaimed under memory pressure, which can easily lead to out-of-memory conditions. [2]
The kernel documentation calls zram and zswap in-memory swap, for which swappiness values above 100 can be considered; they are outside the scope of this guide. [2]
How to resize or remove the swap file
To resize the file, switch it off, create it again at the new size and switch it back on; the fstab line stays the same as long as the path does. Switching swap off moves its contents back into RAM, so there must be room: the SwapFaq's script checks that free RAM is at least the used swap [1], and swapoff exits with status 2 when the system has too little memory to stop swapping. [4]
free -h
sudo swapoff /swapfile
sudo rm /swapfile
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon --showIf you created the first file with dd, use dd again, with count=$((4*1024)) for 4 GiB.
To remove swap, switch it off, delete its line from /etc/fstab and delete the file. [1] In the editor, remove only the /swapfile line:
sudo swapoff /swapfile
sudo cp /etc/fstab /etc/fstab.bak
sudo nano /etc/fstab
sudo findmnt --verify
sudo systemctl daemon-reload
sudo rm /swapfileWhen to add RAM instead of more swap
Add RAM when the server swaps all the time under normal load, when available memory stays near zero or when the OOM killer still stops processes. Watch the server while it is busy:
vmstat 5 12
free -hvmstat 5 12 prints twelve reports, five seconds apart; ignore the first, which shows averages since the last reboot. si is memory swapped in from disk per second and so is memory swapped out to disk. [10] These signs point to more RAM, not a bigger swap file:
- Constant swapping.
siandsostay above zero in most reports during normal work: the heavy swap use that causes performance problems. [1] - No headroom. The
availablefigure infree -hstays near zero. [3] - Swap past the ceiling. You would need more than twice your RAM in swap. [1]
- OOM kills continue. By default, the kernel produces a task dump on each OOM kill that shows why the OOM killer ran and why it chose that task; look for it in the kernel log. [2]
For the other limits to watch, see our guides to the signs that your VPS needs more resources and common VPS performance bottlenecks.
StealthRDP plans in the USA and EU all use NVMe storage, but swap on NVMe is still storage, not memory. If the signs persist, upgrade to a plan with more RAM at any time from the client area, paying the prorated difference; the Linux VPS page compares the plans.
FAQ
How much swap should I add on Ubuntu?
Ubuntu's SwapFaq says a system with more than 1 GB of RAM that does not hibernate needs at least the rounded square root of its RAM in swap, and at most twice its RAM. For a 4 GB server, that means 2 GB to 8 GB. [1]
Is a swap file slower than a swap partition?
Ubuntu's SwapFaq says that with the 2.6 kernel, a swap file is just as fast as a swap partition, so you do not need to reinstall to get more swap. It notes that a file may not use contiguous disk blocks the way a partition does. [1]
What is the default swappiness on Ubuntu?
The default is 60, both in the kernel documentation [2] and in Ubuntu's SwapFaq. [1] The kernel accepts values from 0 to 200, and its documentation says the best value depends on the workload. [2]
Can I use fallocate to create a swap file?
You can, and Ubuntu's SwapFaq uses fallocate in its own example. [1] The swapon manual warns that a preallocated file may count as a file with holes on some filesystems, which swapon rejects, and calls dd from /dev/zero the most portable method. [4]
How do I turn off swap on Ubuntu?
Run swapoff on the swap file, remove its line from /etc/fstab and delete the file. [1] swapoff needs enough free RAM to take back what is in swap, and it exits with status 2 when the system has too little memory to stop swapping. [4]
Does adding swap stop the OOM killer?
Not on its own. Swap is not a complete replacement for physical memory [1], and when the system runs out of memory, the kernel's OOM killer still kills a task, normally a memory-hungry one. [2]
Sources & references
- SwapFaq — Ubuntu Community Help Wiki (accessed 2026-11-03)
- Documentation for /proc/sys/vm/ — The Linux Kernel documentation (accessed 2026-11-03)
- free(1) - Linux manual page — man7.org (accessed 2026-11-03)
- swapon(8) - Linux manual page — man7.org (accessed 2026-11-03)
- findmnt(8) - Linux manual page — man7.org (accessed 2026-11-03)
- fallocate(1) - Linux manual page — man7.org (accessed 2026-11-03)
- Ubuntu Manpage: mkswap - set up a Linux swap area — Ubuntu Manpage Repository (accessed 2026-11-03)
- fstab(5) - Linux manual page — man7.org (accessed 2026-11-03)
- sysctl.d(5) - Linux manual page — man7.org (accessed 2026-11-03)
- vmstat(8) - Linux manual page — man7.org (accessed 2026-11-03)
Continue learning
VPS Control Panels and Management Tools: 6 Compared
Compare VPS control panels such as Plesk, cPanel, Webmin, Virtualmin and DirectAdmin on price, features and skill level, and learn how to choose one.
High Availability VPS: How to Design Automated Failover
Design a high availability VPS setup with redundancy, replication, automated failover and monitoring, and see how VPS and cloud compare for uptime.
Server Uptime Monitoring: 7 Tools to Compare
Compare server uptime monitoring tools, what to look for in alerts and checks, and when a self-hosted uptime monitor is enough.
