Guides ยท Oct 5, 2026
How to Add a Swap File on an Ubuntu VPS (and Tune It Properly)
Create a persistent swap file on Ubuntu, set swappiness, check memory with free and vmstat, and learn when swap helps and when you need more RAM.
Swap is disk space the kernel can use when physical memory runs short. On a small VPS a modest swap file can be the difference between a harmless memory spike and the kernel's out-of-memory killer ending your database or game server. It is not a replacement for RAM, and this guide explains both how to set it up and when to stop relying on it.
Check what you have
free -h
swapon --show
cat /proc/sys/vm/swappiness
If swapon --show prints nothing, there is no swap. Many cloud and VPS images start without it. Also look at df -h / to make sure you have the free disk space for a swap file.
How much swap?
There is no single right answer. Reasonable starting points for a server that does not hibernate:
- Up to 4 GB RAM: 1 to 2 GB swap.
- 8 GB RAM or more: 2 to 4 GB is plenty as a safety net.
More swap does not make a server faster. It only delays the point at which something gets killed, and a server that actively swaps is slow. Treat it as insurance.
1. Create the swap file
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
If fallocate is not supported on your file system, use sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress instead. The chmod 600 step matters because swap can contain sensitive data from memory, so only root should be able to read it.
Verify:
swapon --show
free -h
2. Make it permanent
Without an entry in /etc/fstab, the swap file disappears at the next reboot. Back up the file first, then append the line:
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
sudo mount -a && echo fstab OK
A bad fstab can prevent a clean boot, which is why we test with mount -a right away.
3. Tune swappiness
The vm.swappiness value (0 to 100, default 60 on Ubuntu) tells the kernel how eagerly to move memory pages to swap. On a server with spare RAM you generally want it lower, so applications keep their memory and swap is used mostly in emergencies:
sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
For databases a value of 1 to 10 is a common choice. There is no magic number, so change it, observe and adjust.
You can also make the kernel hold on to its file-system cache a bit longer, which helps servers that read lots of files:
sudo sysctl vm.vfs_cache_pressure=50
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.d/99-swappiness.conf
4. Understand what you are looking at
In free -h, the column that matters is available, which estimates memory that applications could use, including reclaimable cache. A low "free" number alone is normal on Linux, because spare memory is used for caching.
Watch swap activity live with:
vmstat 5
The si and so columns show memory swapped in and out per second. Occasional small numbers are fine. Sustained non-zero values mean the server is short on RAM and is paying for it in speed.
5. Find the memory hog
ps aux --sort=-%mem | head -n 10
sudo journalctl -k | grep -i -E "out of memory|oom"
If the second command shows OOM kills, note which process died. Often the fix is a configuration limit rather than a bigger server:
- Java servers: set
-Xmxexplicitly, about 1 GB below total RAM, as in the Minecraft guide. - MySQL/MariaDB: reduce
innodb_buffer_pool_sizeon small servers. - Node.js apps: use PM2's
--max-memory-restart 400Mto restart a leaking process cleanly (see deploying Node.js with PM2). - PHP-FPM: lower
pm.max_childrenso a traffic burst cannot spawn more workers than RAM allows.
6. Removing or resizing swap
sudo swapoff /swapfile
sudo rm /swapfile
Remove the matching line from /etc/fstab too. To resize, turn swap off, delete the file, and create a new one with the new size. Doing this while memory is already tight can fail, so do it at a quiet time.
When swap is not the answer
If a server constantly uses swap, no tuning will fix it. The honest options are to reduce what runs on it or to move to a bigger plan. Our sizing guide shows how to measure real usage, and the VPS hosting page lists the plans you can move up to. Also remember that swap lives on the server's SSD, so heavy swapping adds a lot of disk writes.
Container note
Docker containers share the host's memory and swap. You can limit a container with --memory=512m in docker run, or mem_limit in Compose, so that one runaway container cannot take the whole server with it. See the Docker guide to get started.
Frequently asked questions
Do I need swap if I have plenty of RAM?
It is not required, but a small swap file is cheap insurance against brief spikes and lets the kernel move rarely used memory out of the way.
Will swap make my server faster?
No. Swap is much slower than RAM. It prevents crashes during spikes but heavy swapping makes the server slow.
What swappiness value should I use?
A value around 10 is a common choice for servers with enough RAM. Observe your workload with vmstat and adjust.
Is a swap file as good as a swap partition?
On modern Linux a swap file performs essentially the same and is much easier to create and resize on a VPS.
Related guides
- How to Choose a VPS Size for a Game Server (RAM, CPU, Disk)
- Ubuntu 26.04 VPS Setup and Hardening Checklist
- How to Deploy a Node.js App with PM2, Nginx and Free SSL
Need a server for this?
Every TidalVPS VPS comes with full root access, unmetered traffic, daily backups and DDoS protection.