Swap is disk space the Linux kernel uses as overflow when RAM is full. Without swap, a memory spike (a busy WordPress site, a composer install, an npm build) makes the kernel kill a process, often MariaDB, and your site goes down. A modest swap file gives the server breathing room.
Swap is much slower than RAM, so it is a safety net, not a replacement for memory.
Which VPS is this for? These steps are for an Unmanaged VPS, where you log in as root and look after the server yourself. On a Managed VPS we take care of the operating system and server software, and you manage your websites in StackCP instead.
Step 1: Check what you have
free -h swapon --show df -h /
If swapon --show prints nothing, the VPS has no swap. Make sure you have enough free disk space for the file.
How big should it be?
| VPS RAM | Suggested swap |
|---|---|
| 1 GB | 1–2 GB |
| 2–4 GB | 2 GB |
| 8 GB or more | 2–4 GB (just as a safety net) |
Step 2: Create the swap file
Ubuntu and Debian (ext4)
sudo fallocate -l 2G /swapfile
AlmaLinux and Rocky Linux (usually XFS)
Write the file out in full with dd, which works on every file system:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
All systems: secure and enable it
sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile swapon --show
The chmod 600 step matters: swap can contain passwords and other data from memory, so only root may read it.
Step 3: Make it permanent
Add the swap file to /etc/fstab so it is used after every reboot:
sudo cp /etc/fstab /etc/fstab.bak echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
A typo in /etc/fstab can stop the server from booting, which is why we keep a copy. Check the file with cat /etc/fstab before rebooting.
Step 4: Tune swappiness (optional)
vm.swappiness controls how eagerly Linux moves memory to swap (0–100, default 60). On a server, a lower value keeps active data in RAM longer:
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf sudo sysctl --system cat /proc/sys/vm/swappiness
Changing the size or removing swap
sudo swapoff /swapfile # to resize: create it again with the new size, then mkswap and swapon # to remove completely: sudo rm /swapfile sudo nano /etc/fstab # delete the /swapfile line
Is swap enough, or do I need more RAM?
Watch the server for a few days with free -h and vmstat 5. Occasional small swap use is fine. Signs you need more RAM:
- Swap usage stays high all day and keeps growing.
- The
si/socolumns invmstat 5are constantly above zero (the server is actively swapping). - Pages load slowly even though CPU usage is low.
- The kernel log shows out-of-memory kills:
sudo journalctl -k | grep -i 'out of memory'.
If you see these, reduce memory use (fewer PHP-FPM workers, smaller database buffers, fewer containers) or move to a bigger plan: How to Upgrade or Downgrade Your VPS Plan.
Common problems
swapon: /swapfile: insecure permissions: runsudo chmod 600 /swapfile.swapon failed: Invalid argument: the file system did not accept a file made withfallocate(this can happen on XFS). Remove it and create it again withdd.fallocate failed: Text file busy: the swap file is in use. Runsudo swapoff /swapfilefirst.
Need help?
If something about the VPS itself is not working (it won't start, you can't reach it, or you need console access, an upgrade or a reinstall), open a support ticket from your client area or message us on WhatsApp at 01818160926. Include your VPS IP address and what you have already tried so we can help faster.
Categories
Written by
FimuroHost Team
Technical Writer