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.

UbuntuSwapMemory tuning
What you learn

How to add a swap file, enable it safely, make it persistent, and tune a few memory settings without getting overly clever.

Best for

1 GB to 4 GB Ubuntu VPS instances that occasionally hit RAM limits during package upgrades, builds, or application spikes.

Risk to watch

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.

Expected outcome: By the end, your VPS will have active swap, safer defaults for small-memory systems, and a simple way to verify the change worked.

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 5

If 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 /swapfile

If fallocate is not supported on your storage, use dd instead:

sudo dd if=/dev/zero of=/swapfile bs=1M count=1024 status=progress

Lock down permissions so only root can read and write it:

sudo chmod 600 /swapfile
ls -lh /swapfile

Format it as swap and enable it:

sudo mkswap /swapfile
sudo swapon /swapfile

Confirm it is active:

swapon --show
free -h

You 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/fstab

Check that you did not accidentally duplicate entries:

grep swapfile /etc/fstab

If you are cautious, test the file system table before your next reboot:

sudo mount -a

This 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/swappiness

A common conservative starting point is 10:

sudo sysctl vm.swappiness=10

To 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 --system

What these settings mean:

  • vm.swappiness=10 reduces eagerness to swap under normal load.
  • vm.vfs_cache_pressure=50 keeps 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.

Warning: If your VPS is swapping heavily all the time, the real fix may be optimizing the app or upgrading the server, not tuning endlessly.

Rollback and recovery notes

If the change causes problems, rollback is straightforward.

To disable swap temporarily:

sudo swapoff /swapfile

To remove the persistent entry:

sudo nano /etc/fstab

Delete 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 --system

Finally, remove the file itself only after swap is off:

sudo rm /swapfile

If 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/fstab

You should see:

  • An active swap file
  • A persistent fstab entry
  • 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.