IPv4 & IPv6 Leasing - Any RIR, Any LocationOrder Now
Hostperl

Add Swap on Ubuntu and AlmaLinux VPS in 2026

By Raman Kumar

Share:

Updated on Aug 4, 2026

Add Swap on Ubuntu and AlmaLinux VPS in 2026

Why you may need swap on a fresh VPS

If your VPS gets close to its RAM limit during package updates, database imports, or a WordPress migration, add swap on Ubuntu and AlmaLinux VPS before the kernel starts killing processes. Swap is not a substitute for RAM, but it gives your server room to breathe and helps prevent sudden failures on smaller plans. On Hostperl VPS plans, that extra buffer is often the difference between a clean deployment and a support ticket about a crashed service. If you are still choosing the right size, compare it with Hostperl VPS before you launch.

This guide is for a fresh server. You will connect by SSH, check the operating system, create a swapfile, make it persistent, verify it, and confirm it survives a reboot. If you want a related baseline for the same server, Hostperl also publishes initial VPS setup guidance for Ubuntu and AlmaLinux and a separate guide to SSH, UFW, and Fail2Ban on a fresh VPS.

Before you start

You need root SSH access, or a sudo-capable admin account. Replace these placeholders in the commands below:

  • SERVER_IP = your VPS IP address, for example 203.0.113.10
  • ADMIN_USER = your non-root admin account, for example hostadmin
  • SWAP_SIZE = the swap size you want, for example 2G or 4G

On your local computer, connect to the server with SSH:

ssh root@SERVER_IP

If your provider gives you a default non-root account, use that instead:

ssh ADMIN_USER@SERVER_IP

After login, check the OS so you use the right package and service commands later:

cat /etc/os-release

You should see either Ubuntu or Debian-style output with ID=ubuntu or ID=debian, or AlmaLinux/Rocky output with ID="almalinux" or ID="rocky".

Check whether swap already exists

On the VPS as root, inspect current memory and swap before making changes:

free -h

Look at the Swap: line. If it already shows a non-zero amount, you may only need to resize or tune it instead of creating a new file. Also check active swap devices:

swapon --show

If the output is empty, you can safely create a swapfile.

Add swap on Ubuntu and Debian

Ubuntu and Debian use the same swapfile workflow. The commands below create a dedicated file, set safe permissions, format it as swap, enable it now, and keep it active after reboots.

On the VPS as root, create the swapfile with fallocate if your filesystem supports it:

fallocate -l 2G /swapfile

If fallocate fails on your filesystem, use dd instead:

dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress

Set strict permissions so only root can read it:

chmod 600 /swapfile

Format the file as swap:

mkswap /swapfile

Enable it immediately:

swapon /swapfile

Confirm it is active:

swapon --show

To make the change persistent, edit /etc/fstab. Open the file with your editor of choice:

nano /etc/fstab

Add this line at the end of the file:

/swapfile none swap sw 0 0

Save and exit the editor. In nano, press Ctrl+O, then Enter, then Ctrl+X.

Test the fstab entry without rebooting:

swapoff /swapfile
swapon -a
swapon --show

If the last command shows /swapfile, your persistence entry is correct.

Add swap on AlmaLinux and Rocky Linux

AlmaLinux and Rocky Linux use the same base commands, but the surrounding system often includes SELinux and firewalld. Swap creation itself does not require firewall changes, yet the persistence check matters just as much.

On the VPS as root, create the file:

fallocate -l 2G /swapfile

If needed, use the fallback:

dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress

Lock the permissions down:

chmod 600 /swapfile

Turn the file into swap space:

mkswap /swapfile

Enable it now:

swapon /swapfile

Check the active swap list:

swapon --show

Persist it by editing /etc/fstab:

nano /etc/fstab

Add this line exactly once:

/swapfile none swap sw 0 0

Save the file, then test it:

swapoff /swapfile
swapon -a
swapon --show

If you later see an SELinux denial tied to unusual storage policies, check the audit log first:

ausearch -m avc -ts recent

For a standard swapfile in /, you normally will not need an SELinux change.

Choose a sensible swap size

For small VPS instances, a 1G to 2G swapfile is usually enough for package installs, short spikes, and temporary memory pressure. A larger 4G swapfile can help when you are migrating a database or compiling software, but it does not fix underprovisioned RAM. If your workload routinely uses swap, move up a VPS size or a dedicated server instead of masking the problem.

That decision matters for agencies and small businesses alike. A shop running WooCommerce, backups, and mail on one box behaves differently from a test server. If that sounds familiar, compare service tiers on managed VPS hosting before you commit.

Verify memory pressure and performance

Once swap is enabled, verify that the kernel sees it and that the system still has room. On the VPS as root, run:

free -h
vmstat 1 5
swapon --show

free -h should show your swap total. vmstat should show activity only if the server is under memory pressure. If swap usage stays at zero, that is normal on an idle server.

Check the file is present and sized correctly:

ls -lh /swapfile

You want to see the expected size, such as 2.0G, and restrictive permissions like -rw-------.

Test that swap survives a reboot

Before you consider the job done, reboot the VPS and confirm the file comes back automatically. This step catches quiet configuration mistakes before your next maintenance window does.

On the VPS as root, reboot now:

reboot

After the machine comes back, reconnect from your local computer:

ssh root@SERVER_IP

Then check persistence again:

swapon --show
free -h

If the swapfile appears after reboot, your setup is correct.

Common problems and fixes

Problem: swapon: /swapfile: swapon failed: Invalid argument
Run:

file /swapfile
ls -l /swapfile

If the file is missing or not owned by root with mode 600, recreate it with the commands above.

Problem: swapon: Permission denied
Check the permissions:

ls -l /swapfile

Fix them with:

chmod 600 /swapfile

Problem: swap enabled now, but gone after reboot
Inspect /etc/fstab for a typo:

grep swapfile /etc/fstab

If the line is missing or malformed, edit it again and keep the format exactly as shown earlier.

Problem: the server still feels slow after adding swap
Check memory use, not just swap use:

free -h
ps aux --sort=-%mem | head

If a single service is consuming memory, tune that service or move the workload. Swap prevents a crash; it does not replace capacity planning.

If you are provisioning a new server for WordPress, a control panel, or a small application stack, start with enough RAM first and use swap as backup room. Hostperl can help you size the right plan, then move you onto a Hostperl VPS or a larger platform if your workload grows.

For customers doing live migrations or launch prep, that usually means fewer emergency restarts and cleaner maintenance windows.

Quick recap

To add swap on Ubuntu and AlmaLinux VPS, create a swapfile, secure it with chmod 600, activate it with mkswap and swapon, then add it to /etc/fstab and test persistence after reboot. That is the complete path from first login to verification. It is a small change, but on a lean VPS it often protects the rest of your stack.

FAQ

How much swap should I add?
For most small VPS instances, start with 1G to 2G. If you run imports, compile code, or host multiple services, 4G may be more practical.

Is swap bad for SSDs?
Modern SSDs handle normal swap use well. The real issue is sustained heavy swapping, which usually means you need more RAM.

Can I use swap on both Ubuntu and AlmaLinux the same way?
Yes. The swapfile commands are the same. Only the surrounding admin workflow differs slightly by distribution.

Should I use a swap partition instead?
On a VPS, a swapfile is usually easier to create, resize, and remove. A partition makes more sense on some dedicated servers.

Will swap improve performance?
Not by itself. It helps avoid memory exhaustion and process crashes, but RAM is still faster than swap.