Skip to content

Tutorials Servers Beginner1 min read

How to Add Swap Space on Ubuntu

Create a swap file on an Ubuntu VPS to prevent out-of-memory crashes, make it permanent, and tune swappiness for a server workload.

By ZSOLNET Editorial Team Updated

Key takeaways

  • Swap is disk space used as overflow when RAM runs out.
  • It prevents crashes but is much slower than RAM.
  • If you use swap constantly, upgrade your plan's memory.

Check current swap

free -h
swapon --show

Create a 2 GB swap file

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Make it permanent

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

Tune swappiness

Swappiness (0–100) controls how eagerly the kernel uses swap. For servers, a low value keeps active data in RAM:

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

Frequently asked questions

How much swap should I add?

For small servers, 1–2 GB is plenty as a safety net. Swap isn't a substitute for enough RAM.

How do I see whether swap is being used?

Run free -h or htop. Constant heavy swap usage means the server needs more memory.

Share X LinkedIn Facebook

Log in to your account

Manage your domains, hosting and invoices in one place.

Secure connection straight to the ZSOLNET client area.

Open the ZSOLNET dashboard

No account yet?