How to increase swap space on a VPS
If your VPS is running out of memory and you're seeing slow performance or out-of-memory (OOM) kills, adding swap space gives the kernel an overflow valve — it's not a substitute for RAM, but it can prevent crashes and keep services running under load.
Check Your Current Swap and Disk Space First
Before adding swap, confirm you don't already have one configured and that you have enough free disk space to create one.
free -h
The output will look something like this:
total used free shared buff/cache available
Mem: 1.9Gi 1.2Gi 180Mi 45Mi 580Mi 520Mi
Swap: 0B 0B 0B
If the Swap row shows 0B, you have no swap configured. Now check available disk space:
df -h /
Look at the Avail column. You need enough free space for the swap file you plan to create — typically 1–2x your RAM for servers with 1–4 GB of memory, or 1–2 GB flat if you have more RAM and just need an OOM safety net.
Create the Swap File
Use fallocate to create a 2 GB swap file quickly. If fallocate fails (more on that below), use dd instead.
sudo fallocate -l 2G /swapfile
Set the correct permissions — swap files should only be readable by root:
sudo chmod 600 /swapfile
Format it as swap:
sudo mkswap /swapfile
Enable it immediately:
sudo swapon /swapfile
Verify it's active:
free -h
You should now see a non-zero value in the Swap row. You can also run swapon --show to get a cleaner breakdown of all active swap areas including the file path, size, and usage.
Make the Swap Permanent
The swap file is active right now, but it won't survive a reboot unless you add it to /etc/fstab. Open the file:
sudo nano /etc/fstab
Add this line at the bottom:
/swapfile none swap sw 0 0
Save and close. You can verify the entry will work without rebooting by running:
sudo mount -a
If there are no errors, the fstab entry is valid.
Tune Swappiness (Optional but Recommended)
The vm.swappiness kernel parameter controls how aggressively the system uses swap. The default is 60, which is tuned for desktops. On a VPS where you want to keep as much data in RAM as possible and only use swap as a last resort, a lower value like 10 is usually better.
sudo sysctl vm.swappiness=10
To persist this across reboots, add it to /etc/sysctl.conf:
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
Common Gotcha: fallocate Fails on Certain Filesystems
On some VPS setups — particularly those using Btrfs, or certain network-backed storage configurations — fallocate will return an error like:
fallocate: fallocate failed: Operation not supported
In this case, use dd instead to create the file. It's slower but works universally:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
This creates a 2 GB file (2048 × 1 MB blocks). After that, continue with the same chmod, mkswap, and swapon commands as before.
Another edge case: some OpenVZ-based VPS providers don't allow swap files at all at the container level — it's controlled by the host node. If swapon returns a permission error and you're on an OpenVZ VPS, you'll need to contact your provider to enable swap allocation for your container. KVM-based VPS instances (which CloudSurph uses) don't have this restriction and support swap files without any special configuration.
How to Remove or Resize Swap Later
If you need to change the swap size, disable it first, then delete and recreate the file:
sudo swapoff /swapfile
sudo rm /swapfile
Then repeat the creation steps with your new target size. Make sure to update or re-add the /etc/fstab entry after recreating the file — the path stays the same so in most cases the existing entry is still valid.
Quick Summary
- Use
fallocate -l 2G /swapfileto create the file (orddif fallocate fails) - Set permissions with
chmod 600, thenmkswapandswapon - Add
/swapfile none swap sw 0 0to/etc/fstabfor persistence - Lower swappiness to
10on production servers to prefer RAM - OpenVZ containers may block swap — KVM-based VPS instances don't have this limitation