Add swap space on AlmaLinux 9 by creating a protected 2 GiB swap file and enabling it immediately and after reboots. The setup takes about five to ten minutes and gives a small VPS extra disk-backed headroom during short RAM spikes.
Prerequisites
- An AlmaLinux 9 server with at least 2 GiB of free space on the filesystem that contains
/swapfile. - SSH access as
root, or a regular account with workingsudoprivileges. - The path
/swapfilemust not be an existing application file or active swap area. - A Linux VPS from VPSLake if you need an AlmaLinux server for this procedure.
Swap is slower than RAM, so it is a safety buffer rather than a replacement for more memory. This guide uses a 2 GiB file; use a smaller size when the disk has limited free space.
Step 1: Check AlmaLinux 9, free disk, and existing swap
Inspect the server first so you do not create a second swap file accidentally or fill the root filesystem.
cat /etc/almalinux-release
free -h
swapon --show
df -h /
sudo test -e /swapfile && ls -lh /swapfile || echo '/swapfile is available'
On a clean VPS, the output should identify AlmaLinux 9, show Swap: 0B, return no rows from swapon --show, and report enough available space for the file:
AlmaLinux release 9.x (Emerald Puma)
Swap: 0B 0B 0B
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 7.0G 33G 18% /
/swapfile is available
If swapon --show already lists swap, keep it unless you have a specific reason to add more. If /swapfile exists, choose another path such as /swapfile-2g instead of overwriting it.
Step 2: Allocate and secure the swap file
Allocate the file and restrict it to root before creating the swap signature, which prevents other local users from reading memory pages written to disk.
sudo fallocate -l 2G /swapfile
sudo chmod 0600 /swapfile
sudo mkswap /swapfile
mkswap should report a new swapspace UUID and a size close to 2 GiB:
Setting up swapspace version 1, size = 2 GiB
no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
AlmaLinux 9 commonly uses XFS, where fallocate is the efficient choice. If swapon later rejects the file with Invalid argument, first confirm that it is not active, remove only this newly created file, and recreate it as a fully written file:
sudo rm /swapfile
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
sudo chmod 0600 /swapfile
sudo mkswap /swapfile
The dd fallback writes 2,048 MiB and can take longer because it touches every block.
Step 3: Enable swap now and at boot
Put the file in /etc/fstab so AlmaLinux activates it during startup, then turn it on for the current session without rebooting.
Check for an existing entry before appending one:
sudo grep -nE '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab || true
If that command returns no line, add the entry exactly once:
printf '%s\n' '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab
Reload systemd’s generated configuration and activate the file:
sudo systemctl daemon-reload
sudo swapon /swapfile
sudo swapon --show
The active table should include the file:
NAME TYPE SIZE USED PRIO
/swapfile file 2G 0B -2
Priority can differ when another swap device is already configured. The important checks are that /swapfile appears and its SIZE is close to the value you selected. Red Hat documents the same file-based sequence for RHEL 9 in its official swap documentation.
Step 4: Tune swappiness only when needed
The vm.swappiness setting influences how readily the kernel moves inactive pages to swap; AlmaLinux’s default is usually a good starting point, so change it only for a known workload.
See the current value:
sysctl vm.swappiness
If the VPS should prefer RAM and use swap mainly during pressure, test a value of 10:
sudo sysctl vm.swappiness=10
If monitoring shows that value suits the workload, save it in a dedicated file rather than changing a vendor-managed configuration:
printf '%s\n' 'vm.swappiness = 10' | sudo tee /etc/sysctl.d/99-swap.conf
sudo sysctl -p /etc/sysctl.d/99-swap.conf
sysctl vm.swappiness
Expected output is:
vm.swappiness = 10
Do not set swappiness to zero just to avoid disk activity. That can leave the kernel with less flexibility during a sudden memory shortage.
Verify the persistent swap setup
Verify the active kernel table, reported capacity, file permissions, and boot entry together:
sudo swapon --show
cat /proc/swaps
free -h
stat -c '%A %U %n' /swapfile
sudo grep -E '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab
Successful output contains an active /swapfile, a Swap total near 2 GiB, mode -rw-------, and the following configuration line:
/swapfile file 2G 0B -2
-rw------- root /swapfile
/swapfile none swap defaults 0 0
Troubleshooting
swapon: /swapfile: swapon failed: Invalid argument
The file may be sparse, partially allocated, or on a filesystem layout that does not support it as a swap file. Run sudo swapon --show first; if the file is not active, remove it and use the dd fallback in Step 2.
fallocate: fallocate failed: No space left on device
The filesystem holding /swapfile cannot provide 2 GiB. Check df -h /, preserve free space for updates and logs, and recreate the file at a smaller size such as 1 GiB with sudo fallocate -l 1G /swapfile.
swapon: /swapfile: swapon failed: Device or resource busy
The path is already active or another process has it open. Confirm with sudo swapon --show and ls -l /swapfile; if it is already listed, keep the active file and remove any duplicate /etc/fstab line instead of recreating it.
swapon: /swapfile: swapon failed: Operation not permitted
Some container-based VPS environments prevent guests from managing swap. This is a virtualization policy, not a missing AlmaLinux package; ask the provider to enable host-level swap or move the workload to a plan that permits swap files.
FAQ
How large should swap be on AlmaLinux 9?
There is no universal size. Two GiB is a practical starting point for a small VPS, but choose based on available disk, peak memory use, and the amount of space required by the operating system and applications.
Does swap increase the VPS’s physical RAM?
No. Swap is disk-backed virtual memory and is much slower than RAM. It can keep a short memory spike from immediately terminating a process, while frequent swap activity indicates that the workload needs more memory or tighter limits.
Do I need to reboot after creating the swap file?
No. swapon /swapfile enables it immediately, and the /etc/fstab entry handles future boots. systemctl daemon-reload refreshes systemd’s generated configuration after the file is edited.
How can I remove the swap file later?
Confirm the path with sudo swapon --show, run sudo swapoff /swapfile, and remove only its line from /etc/fstab. After checking that it is no longer active, delete the file with sudo rm /swapfile.