How to Add or Increase Swap Space on Ubuntu (24.04/22.04 Guide)

Add or increase swap space on Ubuntu to prevent memory crashes and improve server stability. Works on Ubuntu 24.04, 22.04, and all VPS providers.

If your VPS is running out of memory, adding or increasing swap space can instantly improve stability. Swap helps your Ubuntu server handle occasional spikes in RAM usage by providing extra virtual memory on disk. In this guide, you’ll learn how to create, enable, and verify swap space on Ubuntu 24.04 and 22.04, even on the smallest VPS plans.

Small VPS servers often come with limited RAM, and when memory runs out, your server may freeze, crash, or kill important processes like PHP-FPM or MySQL. Adding swap space is one of the simplest ways to stabilize your system without upgrading your VPS plan.

This guide shows you how to create, increase, enable, and optimize swap space on Ubuntu 24.04, 22.04, or any other modern Ubuntu release.

What Is Swap Space?

Swap is a reserved area on your disk that acts as virtual memory when RAM is full.

Swap helps:

  • Prevent out-of-memory (OOM) crashes

  • Stabilize PHP-FPM

  • Avoid MySQL shutdowns

  • Handle traffic spikes

  • Complete heavy tasks (Composer, Docker builds, imports)

  • Keep WordPress stable under load

Swap should NOT:

  • Replace RAM

  • Be used for constant heavy workloads

For most VPS servers, 1–2 GB of swap is enough.

Step 1: Check if Swap Already Exists

Run:

				
					swapon --show
				
			

If there is no output, your server has no swap.

Check system memory:

				
					free -h
				
			

You’ll see something like:

				
					Swap:       0B        0B        0B

				
			

If swap exists, you’ll see numbers like:

				
					Swap:     1.0G      50M     950M

				
			

Step 2: Create Swap File (Recommended Method)

Most VPS users should create swap using a swapfile — not partition.

Create a 2GB swap file:

				
					fallocate -l 2G /swapfile
				
			

If fallocate doesn’t work (rare), use:

				
					dd if=/dev/zero of=/swapfile bs=1M count=2048
				
			

Secure the swap file:

				
					chmod 600 /swapfile
				
			

Turn swapfile into usable swap:

				
					mkswap /swapfile
				
			

Enable swap:

				
					swapon /swapfile
				
			

Verify:

				
					swapon --show
free -h
				
			

You now have swap memory active.

Step 3: Make Swap Permanent

Add to /etc/fstab:

				
					echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
				
			

This ensures swap loads on reboot.

Step 4: Choose the Right Swap Size

A basic rule:

RAMRecommended Swap
1 GB1–2 GB
2 GB1 GB
4 GB1 GB or none
8 GB+Swap optional

For VPS servers running:

  • WordPress

  • Virtualmin

  • MariaDB

  • Composer/Docker tasks

2 GB is the sweet spot.

Step 5: Increase Swap Size (If You Already Have Swap)

If swap is too small, remove the old file and recreate a bigger one.

Disable current swap:

				
					swapoff -a
				
			

Create larger file (example: 4GB):

				
					fallocate -l 4G /swapfile
				
			

Secure & re-enable:

				
					chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
				
			

Update fstab (if needed):

				
					nano /etc/fstab
				
			

Ensure line exists:

				
					/swapfile none swap sw 0 0
				
			

Step 6: Optimize Swap (Optional but Recommended)

1. Adjust swappiness

Controls how often Ubuntu uses swap.

Check current value:

				
					cat /proc/sys/vm/swappiness
				
			

Default is often 60 (too aggressive).

Set to 10:

				
					echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl -p

				
			

2. Adjust cache pressure

Improves filesystem caching:

				
					echo 'vm.vfs_cache_pressure=50' | sudo tee /etc/sysctl.d/99-cache-pressure.conf
sudo sysctl -p
				
			

Step 7: Verify Everything Works

Run:

				
					free -h
swapon --show
				
			

You should see swap activated.

Reboot test:

				
					reboot
				
			

After reboot:

				
					swapon --show
				
			

If it shows your swapfile → your setup is successful.

When Should You NOT Use Swap?

Avoid swap if:

  • Your VPS uses slow HDD storage

  • Your workload is high-performance (DB-heavy)

  • You rely on predictable latency

For typical WordPress + Nginx + PHP-FPM + Virtualmin servers, swap is safe and recommended.

🎯 Conclusion

Adding swap space is one of the easiest and most effective ways to stabilize a VPS with limited RAM. It prevents service crashes, reduces downtime, and ensures smoother performance under load.

This guide works for:

  • Ubuntu 24.04

  • Ubuntu 22.04

  • Any VPS provider (Vultr, Hetzner, DO, Contabo, Linode, AWS Lightsail)

If you want deeper optimization, check out the next articles about slow plugins and MySQL bottlenecks.

Tharindu

Hey!! I'm Tharindu. I'm from Sri Lanka. I'm a part time freelancer and this is my blog where I write about everything I think might be useful to readers. If you read a tutorial here and want to hire me, contact me here.

Related Articles

Leave a Reply

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

Back to top button