How to Add Swap Space on Linux (Ubuntu)
DodaTech
1 min read
In this tutorial, you'll learn about How to Add Swap Space on Linux (Ubuntu). We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
Your server runs out of memory and the OOM killer terminates processes. Swap gives you a safety buffer by using disk space as extra RAM.
Quick Fix
1. Check current swap
free -h
If Swap shows 0B, you have no swap.
2. Create a swap file (1GB example)
sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
3. Verify it's active
free -h
Expected output:
total used free ...
Mem: 1.9G 1.2G 0.7G
Swap: 1.0G 0.0G 1.0G
4. Make permanent
Add to /etc/fstab:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
5. Adjust swappiness (optional)
Lower = use RAM more, swap less. For servers: 10. For desktops: 60.
sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
Sizing Guide
| RAM | Swap Size |
|---|---|
| < 2GB | 2 × RAM |
| 2-8GB | Equal to RAM |
| 8GB+ | 4-8GB (for hibernation) or 0 (no swap needed) |
← Previous
How to Fix ActiveMQ Persistent Message / KahaDB Store Full
Next →
Adobe XD Cloud Documents Not Syncing Fix
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro