Swap is space on your disk that Linux uses as overflow when your server runs out of RAM. Without swap, when memory runs out, Linux stops a process to free some up, which can crash your website, your database or a build halfway through. A swap file gives your server breathing room during short spikes in memory use.
It works on every operating system we offer: Ubuntu 22.04, 24.04 and 26.04, Debian 12 and 13, AlmaLinux 8, 9 and 10, and Rocky Linux 9 and 10.
When swap helps
- Small plans, such as START (1 GB RAM) and EMBER (2 GB RAM).
- Short, memory-hungry jobs, such as
npm install, building Node.js or Next.js apps, building Docker images, and large system updates. - Running several Docker containers on a small plan.
Swap is not a substitute for enough RAM. Disk is much slower than memory, so a server that leans on swap all the time becomes slow. If that's happening, your server needs more RAM. You can upgrade your plan with Upgrade/Downgrade in the left menu of your VPS's management page.
Before you start
- SSH access as root or as a user with sudo rights. If you're logged in as root, you can leave
sudooff the commands. - Enough free disk space for the swap file, with room to spare. Check with
df -h /.
Step 1: Check whether you already have swap
swapon --show
free -h
If swapon --show prints nothing, and the Swap line from free -h shows 0B, there's no swap. If swap is already listed, you can keep it as it is, or resize it (see "Resize or remove swap" below).
Step 2: Choose a size
As a guide:
- START (1 GB RAM, 20 GB disk): 1 to 2 GB
- EMBER (2 GB RAM): 2 GB
- BOOST (4 GB RAM): 2 to 4 GB
- SURGE and larger: optional. 2 to 4 GB is plenty as a safety net.
There's little point going much bigger. If you need more than this, your server needs more RAM, not more swap. The examples below create a 2 GB swap file. Change 2G to the size you want.
Step 3: Create the swap file
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
fallocatecreates an empty 2 GB file.chmod 600makes it readable by root only. Swap can hold anything that was in memory, including passwords, so this matters.mkswapsets the file up as swap, andswaponstarts using it.
If fallocate fails, or swapon complains that the file has holes or shows "Invalid argument", delete the file and create it with dd instead. This works on any file system, but takes a little longer:
sudo rm /swapfile
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
With dd, count is the size in megabytes: 1024 for 1 GB, 2048 for 2 GB and 4096 for 4 GB.
Check that swap is on:
swapon --show
free -h
Step 4: Keep swap turned on after a reboot
Swap turned on with swapon only lasts until the next reboot. To make it permanent, add it to /etc/fstab. Back up the file first, then add the line:
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Only run this once. Check that the line appears once, at the end of the file:
cat /etc/fstab
Test the new line without rebooting:
sudo swapoff /swapfile
sudo swapon -a
swapon --show
If swapon --show lists /swapfile, swap will come back after a reboot.
Be careful with /etc/fstab. A mistake in this file can stop your VPS from starting properly. If that happens, log in with Open Console (see Use the web console to access your VPS) and restore your backup with cp /etc/fstab.bak /etc/fstab.
Step 5 (optional): Tune swappiness
The swappiness setting controls how eager Linux is to move memory into swap. Check the current value with:
cat /proc/sys/vm/swappiness
The usual default is 60. On a server, a lower value such as 10 keeps more in RAM and uses swap mainly when memory is tight:
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl -p /etc/sysctl.d/99-swappiness.conf
The new value applies straight away, and the file keeps it after a reboot.
Resize or remove swap
Turning swap off moves everything in it back into RAM. Do this at a quiet time, and check free -h first: the amount of swap used must fit in your available memory.
To resize swap, turn the swap file off, delete it, and create it again at the new size:
sudo swapoff /swapfile
sudo rm /swapfile
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
Your /etc/fstab line stays the same.
To remove swap completely, turn it off and open /etc/fstab:
sudo swapoff /swapfile
sudo nano /etc/fstab
Delete the /swapfile none swap sw 0 0 line, save and exit, then delete the file:
sudo rm /swapfile
Is your server using too much swap?
Some swap use is normal. A server that's constantly moving memory in and out of swap needs more RAM. To check, run:
vmstat 5
Watch the si (swap in) and so (swap out) columns for a minute, then press Ctrl+C. If those numbers stay above zero most of the time and the server feels slow, it's time to upgrade. htop and btop also show memory and swap use, and you can see usage over time on the Resources graphs on your VPS's management page.
Troubleshooting
- "fallocate failed: Operation not supported": use the
ddmethod in Step 3. - "swapon: /swapfile: insecure permissions": run
sudo chmod 600 /swapfile. - "swapon failed: Invalid argument", or a message about holes: delete the file and create it with
dd(Step 3). - Swap is gone after a reboot: check the
/swapfileline is in/etc/fstab(Step 4). - swapoff fails with "Cannot allocate memory": there isn't enough free RAM to empty swap. Stop some services first, or try again when the server is quieter.
- Apps are still being stopped for lack of memory: check with
sudo dmesg -T | grep -i "out of memory". If you see recent entries, your server needs more RAM.
Related guides
- Secure your new VPS: a checklist for the first 15 minutes
- Turn on automatic security updates on your VPS
- Install Node.js on your VPS
- Install Docker Engine and Docker Compose on your VPS
Need help?
Our Cloud VPS plans are self-managed, so you're responsible for installing, securing and maintaining the software on your server. If something on our side isn't working, such as the network, or your VPS won't boot, open a support ticket. New to your VPS? Start with our Cloud VPS Getting Started Guide.












