1. Check What You Have
swapon --show free -h
If swapon --show lists a swap partition or file already, you only need this guide to add more. If it prints nothing, the server has no swap.
2. Create and Turn On a Swap File
sudo fallocate -l 2G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile swapon --show
chmod 600 matters: swap can contain anything that was in memory, including passwords, so only root may read it.
3. Keep It After a Reboot
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab sudo findmnt --verify
findmnt --verify checks /etc/fstab for mistakes before a reboot finds them for you.
4. Set Swappiness
Swappiness (0 to 200, default 60) is how readily the kernel moves memory to swap. On a server, a low value keeps working memory in RAM and uses swap only under pressure:
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf sudo sysctl --system cat /proc/sys/vm/swappiness
If Swap Is Always in Use
Watch vmstat 5 for a minute. Non-zero numbers in the si and so columns all the time mean the server is constantly moving memory to and from disk. That is the point to find what is using the memory (top, sorted with M) or move to a bigger plan. How much CPU and RAM does a VPS need? has rough sizes by workload.