Set Up Swap and Basic Memory Tuning on a Small Ubuntu VPS
Give a small Ubuntu VPS a little more breathing room with swap and a few conservative tuning changes that help during short memory spikes.
How to add a swap file, enable it safely, make it persistent, and tune a few memory settings without getting overly clever.
1 GB to 4 GB Ubuntu VPS instances that occasionally hit RAM limits during package upgrades, builds, or application spikes.
Swap helps with bursts, but too much swapping can make a slow VPS feel worse. It is a safety net, not a substitute for enough RAM.
Before you begin
- An Ubuntu VPS where you have sudo access.
- Enough free disk space for a swap file.
- A realistic expectation that swap reduces crashes from short bursts but does not magically create more real RAM.
Small VPS plans are often good enough until one package update, build job, import task, or container restart pushes memory usage over the edge. When that happens, the kernel can kill a process abruptly. A modest swap file gives the system more room to survive brief pressure, which is often enough to keep the server usable while you fix the real bottleneck.
Step 1: Check current memory and swap status
Before changing anything, see what the server is doing now.
free -h
swapon --show
cat /proc/meminfo | egrep 'MemTotal|MemAvailable|SwapTotal|SwapFree'
vmstat 1 5If swapon --show returns nothing, swap is not enabled yet. Also check whether the server has enough disk space to host a swap file.
df -h /As a rough beginner-friendly starting point:
- 1 GB RAM VPS: try 1 GB to 2 GB swap
- 2 GB RAM VPS: try 1 GB to 2 GB swap
- 4 GB RAM VPS: often 1 GB to 4 GB swap is enough for burst protection
You do not need a perfect number on day one. You need a safe starting point you can observe.
Step 2: Create and enable the swap file
On modern Ubuntu, a swap file is usually simpler than a dedicated swap partition.
Create a 1 GB swap file:
sudo fallocate -l 1G /swapfileIf fallocate is not supported on your storage, use dd instead:
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024 status=progressLock down permissions so only root can read and write it:
sudo chmod 600 /swapfile
ls -lh /swapfileFormat it as swap and enable it:
sudo mkswap /swapfile
sudo swapon /swapfileConfirm it is active:
swapon --show
free -hYou should now see the new swap size listed.
Step 3: Make the swap file survive reboots
Add the swap file to /etc/fstab so Ubuntu enables it automatically after restart.
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabCheck that you did not accidentally duplicate entries:
grep swapfile /etc/fstabIf you are cautious, test the file system table before your next reboot:
sudo mount -aThis command should finish quietly. If it prints errors, fix them before restarting the VPS.
Step 4: Apply basic memory tuning conservatively
Ubuntu lets you influence swap behavior through kernel parameters. For a small VPS, a lower vm.swappiness often makes sense because it tells the kernel to prefer RAM until pressure builds.
Check the current value:
cat /proc/sys/vm/swappinessA common conservative starting point is 10:
sudo sysctl vm.swappiness=10To keep it after reboot, create a small sysctl file:
printf 'vm.swappiness=10\nvm.vfs_cache_pressure=50\n' | sudo tee /etc/sysctl.d/99-swap-tuning.conf
sudo sysctl --systemWhat these settings mean:
vm.swappiness=10reduces eagerness to swap under normal load.vm.vfs_cache_pressure=50keeps inode and directory cache a bit more willingly, which can help on busy small servers.
These are intentionally mild settings. Avoid giant lists of internet tuning tweaks unless you can explain each one.
Rollback and recovery notes
If the change causes problems, rollback is straightforward.
To disable swap temporarily:
sudo swapoff /swapfileTo remove the persistent entry:
sudo nano /etc/fstabDelete the /swapfile none swap sw 0 0 line, then remove the tuning file if needed:
sudo rm /etc/sysctl.d/99-swap-tuning.conf
sudo sysctl --systemFinally, remove the file itself only after swap is off:
sudo rm /swapfileIf the VPS becomes sluggish after enabling swap, check whether a process is consuming too much memory instead of blaming swap alone.
Step 5: Verify the server behaves the way you expect
After setup, confirm all pieces are in place:
free -h
swapon --show
cat /proc/sys/vm/swappiness
grep swapfile /etc/fstabYou should see:
- An active swap file
- A persistent
fstabentry - Your chosen swappiness value applied
- Fewer abrupt out-of-memory failures during short spikes
Troubleshooting common swap problems
swapon: /swapfile: insecure permissions
Run sudo chmod 600 /swapfile and try again.
fallocate fails.
Use the dd method instead. Some filesystems or storage layers do not support fallocate cleanly.
The server is still slow.
Swap can prevent crashes, but it is slower than RAM. Look for the actual memory-heavy process with top, htop, or container stats.
Swap disappeared after reboot.
Check /etc/fstab for typos and confirm the swap file still exists.
What to do next
Once the VPS can survive memory spikes a little better, the next useful skill is monitoring real CPU, RAM, and disk trends so you know when you have outgrown the box. Continue with Monitor Disk, CPU, and Memory on a VPS With Netdata.
