Tools: Complete Guide to Adding Swap Memory on Ubuntu 26.04

Tools: Complete Guide to Adding Swap Memory on Ubuntu 26.04

Check Current Swap

Create a Swap File

Enable Swap

Configure Automatic Mounting at Boot

Tune Swappiness

Next Steps Servers with limited RAM are vulnerable to out-of-memory kills during traffic spikes or memory-intensive workloads. Swap provides an overflow area on disk, giving the kernel room to move inactive memory pages before resorting to terminating processes. This guide configures a 2 GB swap file on Ubuntu 26.04, mounts it persistently via /etc/fstab, and sets the swappiness value to a level appropriate for server workloads. Verify memory and swap status before making any changes: A Swap: 0B line confirms no swap is currently configured. Allocate a 2 GB file, set the correct permissions, and format it as swap: 1. Create a 2 GB file: 2. Restrict file permissions: The chmod 600 restricts access to root only. Swap files with world-readable permissions will be rejected by the kernel. 3. Format the file as swap: Activate the swap file and confirm it is recognized by the system: 1. Activate the swap file: 2. Verify swap is active: 3. Confirm the allocation: The Swap line should now show 2.0Gi total. Swap activated with swapon does not persist across reboots without an /etc/fstab entry. 1. Back up the fstab file: 2. Add the swap entry: 3. Verify the entry was written correctly: Swappiness controls how aggressively the kernel moves memory pages to swap. The value ranges from 0 to 100. 1. Check the current value: 2. Set the swappiness value: Add the following line: 3. Apply the change immediately: Swap is now active and persists across reboots. From here you can: For the complete guide, visit the original article on Vultr Docs. Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to ? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse

Command

Copy

$ free -h $ -weight: 600;">sudo fallocate -l 2G /swapfile $ -weight: 600;">sudo fallocate -l 2G /swapfile $ -weight: 600;">sudo fallocate -l 2G /swapfile $ -weight: 600;">sudo chmod 600 /swapfile $ -weight: 600;">sudo chmod 600 /swapfile $ -weight: 600;">sudo chmod 600 /swapfile $ -weight: 600;">sudo mkswap /swapfile $ -weight: 600;">sudo mkswap /swapfile $ -weight: 600;">sudo mkswap /swapfile $ -weight: 600;">sudo swapon /swapfile $ -weight: 600;">sudo swapon /swapfile $ -weight: 600;">sudo swapon /swapfile $ -weight: 600;">sudo swapon --show $ -weight: 600;">sudo swapon --show $ -weight: 600;">sudo swapon --show $ free -h $ -weight: 600;">sudo cp /etc/fstab /etc/fstab.bak $ -weight: 600;">sudo cp /etc/fstab /etc/fstab.bak $ -weight: 600;">sudo cp /etc/fstab /etc/fstab.bak $ echo '/swapfile swap swap defaults 0 0' | -weight: 600;">sudo tee -a /etc/fstab $ echo '/swapfile swap swap defaults 0 0' | -weight: 600;">sudo tee -a /etc/fstab $ echo '/swapfile swap swap defaults 0 0' | -weight: 600;">sudo tee -a /etc/fstab $ grep swap /etc/fstab $ grep swap /etc/fstab $ grep swap /etc/fstab $ cat /proc/sys/vm/swappiness $ cat /proc/sys/vm/swappiness $ cat /proc/sys/vm/swappiness $ -weight: 600;">sudo nano /etc/sysctl.conf $ -weight: 600;">sudo nano /etc/sysctl.conf $ -weight: 600;">sudo nano /etc/sysctl.conf vm.swappiness=10 vm.swappiness=10 vm.swappiness=10 $ -weight: 600;">sudo sysctl -p $ -weight: 600;">sudo sysctl -p $ -weight: 600;">sudo sysctl -p - 0 — only swap when RAM is critically full - 10 — recommended for servers (keeps more data in RAM) - 60 — Ubuntu default (more aggressive) - 100 — swap as aggressively as possible - Monitor swap usage in real time with vmstat 1 during peak load periods - Increase swap size by repeating these steps with a larger fallocate value - Explore zswap or zram for in-memory compression as an alternative to disk-backed swap