Add Swap on Ubuntu, Debian, AlmaLinux, and Rocky Linux

Why adding swap helps a small VPS
If your VPS is short on RAM, add swap before apps start failing under load. Swap gives the kernel room to move inactive memory pages out of RAM, which helps during package updates, control panel installs, WordPress bursts, and database spikes. It is not a replacement for real memory, but it can keep a low-RAM server running long enough for you to fix the underlying issue.
On Hostperl VPS plans, swap is often the difference between a clean launch and a restart loop after a busy deployment. If you are still sizing the server, review Hostperl VPS options first, then come back and add swap only if the workload genuinely needs it.
This guide shows a safe way to create a swap file on Ubuntu, Debian, AlmaLinux, and Rocky Linux, then verify that it survives reboot. It also covers the checks you should run after the change, so you can tell whether the swap is helping or just masking a capacity problem.
Connect to the server and check the OS
On your local computer
ssh root@203.0.113.10203.0.113.10 is a reserved documentation example. Replace it with the real public IP assigned to your server. If your provider gives you a default non-root login, use that account in the same way, for example ssh deploy@203.0.113.10.
Keep this first root session open while you work.
On the VPS as root
cat /etc/os-releaseThis tells you whether you are on Ubuntu, Debian, AlmaLinux, or Rocky Linux. The package and service checks below use that output to point you to the right commands.
Check whether you actually need swap
On the VPS as root
free -hLook at the Mem: and Swap: lines. If swap is already present, you may only need to resize it or tune swappiness. If swap is zero and RAM usage regularly climbs above 80%, creating a swap file is a reasonable next step.
For a quick view of current pressure, run:
vmstat 1 5Frequent si and so activity means the system is swapping in and out already. A little is normal during spikes; constant activity means you should treat swap as a short-term cushion, not a final fix.
Add swap on Ubuntu and Debian
On the VPS as root
These commands create a 2 GB swap file at /swapfile. If your VPS is very small, 1 GB is acceptable. For larger VPS plans, 2 GB or 4 GB is more common.
fallocate -l 2G /swapfileIf fallocate fails on your filesystem, use this alternative:
dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progressSet the correct permissions so only root can read the file:
chmod 600 /swapfileFormat the file as swap and enable it immediately:
mkswap /swapfile
swapon /swapfileConfirm the kernel accepted it:
swapon --show
free -hYou should now see /swapfile listed and the swap total should be greater than zero.
Make the change permanent by adding it to /etc/fstab:
cp /etc/fstab /etc/fstab.bak
printf '/swapfile none swap sw 0 0\n' >> /etc/fstabTest the fstab entry without rebooting:
swapoff /swapfile
swapon /swapfile
swapon --showIf swapon --show lists the file again, the line in /etc/fstab is valid.
Add swap on AlmaLinux and Rocky Linux
On the VPS as root
AlmaLinux and Rocky Linux use the same swap file approach. The commands are identical here, which keeps recovery simple across RHEL-compatible systems.
fallocate -l 2G /swapfileUse dd if fallocate is not supported on your filesystem:
dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progressLock down the file, then initialize it:
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfileVerify the swap is active:
swapon --show
free -hPersist it across reboots with /etc/fstab:
cp /etc/fstab /etc/fstab.bak
printf '/swapfile none swap sw 0 0\n' >> /etc/fstabCheck the entry by turning swap off and back on:
swapoff /swapfile
swapon /swapfile
swapon --showIf the file appears again, your system will mount swap after reboot too.
Tune how aggressively Linux uses swap
Most VPS hosts do not need an aggressive swap setting. A conservative value keeps RAM usage steady while avoiding unnecessary disk churn. A vm.swappiness value of 10 is a common starting point for web servers and small databases.
On the VPS as root
cat <<'EOF' > /etc/sysctl.d/99-swap.conf
vm.swappiness = 10
EOF
sysctl --systemThen confirm the value took effect:
sysctl vm.swappinessIf you run a database-heavy stack, you may choose a slightly higher value after testing. Do not set it blindly. Watch memory pressure first, especially if the VPS also runs MySQL, PostgreSQL, or a control panel.
For a broader memory strategy, Hostperl’s note on swap sizing on Ubuntu and AlmaLinux pairs well with database sizing decisions when you are planning a new launch.
Make the server safer before leaving the session
Swap helps stability, but low-RAM servers still need basic housekeeping. Update packages, then reboot later only after you have confirmed the new swap survives a reboot test.
Ubuntu and Debian
apt update
apt upgrade -yAlmaLinux and Rocky Linux
dnf update -yIf you are using a Hostperl VPS for a live site, keep an eye on memory after updates. Package upgrades can temporarily use more RAM than normal. Swap gives you breathing room during that maintenance window.
Verify the result from the server and a client
On the VPS as root
swapon --show
free -h
ls -lh /swapfile
cat /etc/fstab | grep swapExpected result: /swapfile appears in swapon --show, free -h shows non-zero swap, the file permissions are -rw-------, and the fstab line exists.
Check that the system boots with the entry intact by rebooting only after you have saved your work:
rebootOn your local computer
Reconnect after the reboot and confirm the swap is still active:
ssh root@203.0.113.10Then run:
swapon --show
free -hIf swap vanished after reboot, the issue is usually a bad /etc/fstab line or a typo in the file path.
Troubleshooting the most likely problems
1) fallocate fails
Diagnostic command:
fallocate -l 2G /swapfileIf it errors out, use the dd fallback shown above. That command works on more filesystems, though it takes longer.
2) Swap is listed but the server still feels slow
Diagnostic command:
vmstat 1 10If you see constant swapping, the VPS is undersized for the workload. Reduce the app footprint, add RAM, or move to a larger Hostperl VPS. Swap can cushion a burst, but it cannot make a 1 GB machine behave like a 4 GB server.
3) The system refuses to boot after editing /etc/fstab
Diagnostic command after rescue access:
cat /etc/fstabRemove the bad swap line, save the file, then test with swapon --show again. A single typo or extra character is enough to break boot-time mounting.
4) You want to remove swap later
Run this carefully only if you are sure the server has enough RAM:
swapoff /swapfile
sed -i '\|/swapfile none swap sw 0 0|d' /etc/fstab
rm -f /swapfileAfter that, check free -h again to confirm swap is gone.
If your current VPS is paging constantly, the real fix may be a larger plan rather than a bigger swap file. Hostperl can help you size the server correctly and keep the migration clean, whether you are moving a small business site or a control-panel workload. Start with Hostperl VPS and choose the memory headroom your applications actually need.
FAQ
How much swap should I add?
For most small VPS installs, 1 GB to 2 GB is enough. Use more only if you have a clear reason and enough disk space.
Is swap bad for SSDs?
Not by itself. Heavy, constant swapping is the problem. If your server is swapping all day, the VPS is undersized.
Should I use a swap file or a swap partition?
A swap file is easier on VPS hosting and simpler to resize. A partition is fine on bare metal, but it is less flexible for most customers.
Will swap fix out-of-memory crashes?
It can reduce them, but not always. If the workload keeps growing, add RAM or move the service to a larger VPS.
Final check before you call it done
Run one last command set and save the output for your records:
swapon --show
free -h
uptimeIf swap is active after reboot and the server remains responsive, the job is done. For customers who need help choosing the right VPS size before they hit this point again, Hostperl’s managed VPS hosting is a better fit than repeatedly stretching a small server past its limits.
