How To

How to Add Ubuntu Swap Space for Better System Stability

Ubuntu Swap Space helps Linux systems stay responsive when physical RAM starts running low. Instead of crashing applications or freezing the system, Ubuntu temporarily moves inactive memory data to disk-based swap storage.

This feature is especially useful on VPS servers, cloud instances, and low-memory systems. In many Ubuntu installations, swap may not be configured by default, making it important to create it manually for improved stability.

In this guide, you will learn how to create, enable, and optimize swap space on Ubuntu systems safely.

Why Ubuntu Swap Space Matters

Swap acts as backup memory for your Linux system. When RAM usage becomes too high, Ubuntu transfers less-used processes to swap storage. Although swap is slower than RAM, it prevents unexpected crashes and improves multitasking performance.

Systems running virtual machines, databases, Docker containers, or heavy browser workloads often benefit from additional swap memory.

Before creating swap, check whether your system already has active swap configured:

sudo swapon --show

You can also monitor memory usage with:

free -h

If swap usage displays 0B, your system likely has no configured swap space.

Create Ubuntu Swap Space on Linux

First, verify available disk storage:

df -h

Next, create a 4GB swap file:

sudo fallocate -l 4G /swap.img

If fallocate does not work on your filesystem, use:

sudo dd if=/dev/zero of=/swap.img bs=1024 count=4194304

Set secure permissions for the swap file:

sudo chmod 600 /swap.img

Now format the file as swap memory:

sudo mkswap /swap.img

Enable the swap file immediately:

sudo swapon /swap.img

To make the configuration permanent after reboot, add the swap entry to /etc/fstab:

/swap.img swap swap defaults 0 0

Finally, confirm the swap is active:

sudo swapon --show

Optimize Ubuntu Swap Space Performance

Ubuntu uses a kernel setting called swappiness to control how aggressively swap memory is used.

Check the current value:

cat /proc/sys/vm/swappiness

Most Ubuntu systems use a default value of 60. For servers and performance-focused workloads, many administrators prefer a lower value such as 10.

Temporarily change swappiness:

sudo sysctl vm.swappiness=10

To keep the setting after reboot, add this line to /etc/sysctl.conf:

vm.swappiness=10

Lower swappiness values reduce unnecessary disk swapping and improve responsiveness on systems with enough RAM.

Common Ubuntu Swap Space Issues

Some systems may show errors during swap creation. If Ubuntu reports insecure permissions, reapply the correct permissions using:

sudo chmod 600 /swap.img

If swap does not activate after reboot, verify the /etc/fstab entry carefully and check it using:

sudo findmnt --verify

Insufficient disk space can also prevent swap creation, so always confirm available storage before allocating large swap files.

Conclusion

Setting up Ubuntu Swap Space is one of the easiest ways to improve Linux system reliability and memory management. Whether you run Ubuntu on a cloud server, VPS, or desktop machine, properly configured swap helps prevent crashes during high memory usage.

By creating a swap file, enabling it permanently, and tuning swappiness values, you can keep Ubuntu running smoothly even under heavy workloads.

Cyber Defence

Recent Posts

Install TeamViewer on Ubuntu 26.04: Complete Setup Guide

If you need secure remote desktop access on Linux, learning how to Install TeamViewer on…

16 hours ago

Install VirtualBox Ubuntu 26.04 for Easy VM Setup

If you want to test operating systems, build development labs, or safely run isolated environments,…

19 hours ago

How to Install Node.js and npm on Ubuntu

If you want to build JavaScript applications on Linux, learning how to Install Node.js Ubuntu…

22 hours ago

How to Add APT Repositories on Ubuntu Safely

Managing software sources is an essential part of maintaining a Linux system, and understanding APT…

1 day ago

How to Check Website for Malware and Protect Your Site

Website malware is one of the biggest threats for website owners, bloggers, businesses, and WordPress…

2 days ago

Install Python on Ubuntu 26.04 Like a Pro

If you want to Install Python on Ubuntu systems for development, automation, or scripting, Ubuntu…

2 days ago