If you’re wondering how to configure swap space on a Linux VPS, the process is straightforward. First, check whether your VPS already has swap enabled using the swapon –show command. If no swap exists, create a swap file with fallocate or dd, secure it with the correct permissions, format it using mkswap, enable it with swapon, and make it permanent by adding it to /etc/fstab. Properly configured swap space helps prevent out-of-memory errors, improves system stability, and allows your Linux VPS to continue running smoothly during periods of high memory usage.
Learn how to configure swap space on a Linux VPS with this step-by-step guide. Create, enable, optimize, and permanently configure swap for better server performance and stability.
What Is Swap Space?
Swap space is a reserved area on your storage device that the Linux kernel uses as virtual memory when physical RAM becomes full. Instead of immediately terminating applications due to insufficient memory, Linux temporarily moves inactive memory pages to the swap area.
Although swap is much slower than RAM, it provides an important safety net for servers handling unexpected traffic spikes, background processes, or memory-intensive applications.
Why Does a Linux VPS Need Swap Space?
Many VPS providers allocate only the RAM you’ve purchased. If your applications exceed available memory, the Linux kernel may invoke the Out-of-Memory (OOM) Killer, which forcefully terminates running processes.
Adding swap space offers several advantages:
- Prevents sudden application crashes
- Improves server stability
- Helps during temporary memory spikes
- Supports memory-intensive tasks
- Allows safer software updates
- Provides additional virtual memory
Keep in mind that swap is not a RAM replacement. If your VPS constantly relies on swap, upgrading your VPS plan is usually the better long-term solution.
Check Existing Swap Space
Before creating new swap, verify whether your VPS already has one configured.
swapon --show
Or use:
free -h
Example output:
total used free Mem: 2.0G 1.3G 400M Swap: 0B 0B 0B
If Swap shows 0B, your server doesn’t currently have swap configured.
How to Configure Swap Space on Linux VPS? Step-by-Step Guide
Step 1: Check Available Disk Space
Before creating swap, verify that sufficient storage is available.
df -h
Ensure your VPS has enough free disk space for the swap file.
Step 2: Create a Swap File
For modern Linux distributions, using a swap file is the recommended approach.
Create a 2GB swap file:
sudo fallocate -l 2G /swapfile
If fallocate isn’t available:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
This creates a 2GB swap file filled with zeros.
Step 3: Secure the Swap File
Only the root user should access the swap file.
sudo chmod 600 /swapfile
Verify permissions:
ls -lh /swapfile
Expected output:
-rw------- 1 root root 2.0G
Step 4: Format the Swap File
Initialize the file as swap.
sudo mkswap /swapfile
Example output:
Setting up swapspace version 1
Step 5: Enable Swap
Activate the newly created swap space.
sudo swapon /swapfile
Confirm it is active:
swapon --show
Or:
free -h
You should now see the configured swap size.
Step 6: Make Swap Permanent
Without this step, swap disappears after reboot.
Edit the filesystem table:
sudo nano /etc/fstab
Add this line:
/swapfile none swap sw 0 0
Save the file.
Test the configuration:
sudo mount -a
If no errors appear, your swap configuration is correct.
Adjust Swappiness
Linux uses the swappiness value to determine how aggressively it moves memory pages into swap.
Check the current value:
cat /proc/sys/vm/swappiness
Typical default:
60
Recommended values:
| Server Type | Recommended Swappiness |
|---|---|
| Web Server | 10 |
| Database Server | 10–20 |
| General VPS | 20–30 |
| Desktop Linux | 60 |
Temporarily change it:
sudo sysctl vm.swappiness=20
Make it permanent:
echo "vm.swappiness=20" | sudo tee -a /etc/sysctl.conf
Reload settings:
sudo sysctl -p
Monitor Swap Usage
Monitor memory usage:
free -h
View swap details:
swapon --show
Display memory statistics:
vmstat 2
Interactive monitoring:
top
Or install:
htop
These tools help determine whether your VPS is using swap excessively.
Disable Swap
If necessary, disable swap temporarily:
sudo swapoff /swapfile
To remove it permanently:
Remove the entry from /etc/fstab.
Delete the file:
sudo rm /swapfile
Verify:
swapon --show
Recommended Swap Sizes
| VPS RAM | Recommended Swap |
|---|---|
| 512 MB | 1 GB |
| 1 GB | 2 GB |
| 2 GB | 2 GB |
| 4 GB | 2–4 GB |
| 8 GB | 4 GB |
| 16 GB+ | 4–8 GB |
These values work well for most Linux VPS environments.
Common Errors and Solutions
“fallocate failed”
Use the dd command instead.
Permission denied
Ensure you run commands with sudo.
swapon: Invalid argument
Run:
sudo mkswap /swapfile
again before enabling swap.
Swap disappears after reboot
Check that /etc/fstab contains:
/swapfile none swap sw 0 0
Best Practices
For optimal performance:
- Use SSD-backed VPS hosting.
- Configure swap even if sufficient RAM exists.
- Avoid relying on swap continuously.
- Monitor memory usage regularly.
- Reduce unnecessary background services.
- Tune swappiness based on your workload.
- Upgrade RAM if swap usage remains consistently high.
- Keep your Linux kernel and system packages updated.
Final Thoughts
Configuring swap space on a Linux VPS is one of the simplest yet most effective ways to improve server reliability. While swap cannot replace physical RAM, it acts as a valuable backup during memory shortages, helping prevent crashes and ensuring your applications remain available. By creating a properly secured swap file, enabling it permanently, and tuning swappiness for your workload, you can optimize your VPS for better stability and performance. Whether you’re hosting websites, running databases, or managing development environments, a well-configured swap setup is an essential part of Linux server administration.



