How to Add Swap on Ubuntu 24.04 Server
To add swap on Ubuntu, create a swap file with fallocate, lock down its permissions (chmod 600), format it with mkswap, enable it with swapon, and add it to /etc/fstab so it survives reboots. It takes two minutes and no reboot. Swap keeps small VPS instances from crashing when n8n, a bot or a build briefly eats all the RAM.
When a VPS Needs Swap
- 1–2 GB RAM servers running n8n, a Telegram bot with an LLM, or Docker on an s1-1 or s1-2 plan.
- Short spikes:
npm install, Docker image builds, system upgrades. - Processes dying with
KilledorOut of memoryin the logs (dmesg | grep -i oom).
Swap is not extra RAM: even on NVMe it is orders of magnitude slower. Treat it as a cushion for rare peaks. For real-world memory numbers, see how much RAM a bot needs.
Step 1. Check for Existing Swap
# list active swap areas; no output means no swap
sudo swapon --show
# memory and swap in human-readable units
free -h
# free disk space
df -h /
If swapon --show prints nothing, swap is off. Confirm you have enough disk space for the file.
Step 2. Add Swap on Ubuntu: Create the Swap File
Example for a 2 GB swap file:
# allocate a 2 GB file
sudo fallocate -l 2G /swapfile
# root-only access, otherwise swapon refuses it
sudo chmod 600 /swapfile
# format the file as swap
sudo mkswap /swapfile
# enable it now
sudo swapon /swapfile
# verify
sudo swapon --show
If fallocate fails, create the file with dd instead (slower, but works on any filesystem):
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
Step 3. Make Swap Permanent
Without an /etc/fstab entry, swap disappears after a reboot:
# back up fstab first
sudo cp /etc/fstab /etc/fstab.bak
# add the swap file entry
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Validate the file with sudo findmnt --verify. Options are documented in man swapon.
Step 4. Tune Swappiness
vm.swappiness (0 to 100, default 60) controls how eagerly the kernel moves pages to swap. On a server, 10 is a good value: swap kicks in only when RAM is nearly full.
# current value
cat /proc/sys/vm/swappiness
# set to 10 until next reboot
sudo sysctl vm.swappiness=10
# persist it
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
How Much Swap to Add
| Server RAM | 0x.cloud plan | Suggested swap |
|---|---|---|
| 1 GB | s1-1 (15 GB disk) | 1–2 GB |
| 2 GB | s1-2 (30 GB disk) | 2 GB |
| 4 GB | s2-4 (60 GB disk) | 2–4 GB |
| 8 GB | s4-8 (120 GB disk) | 2–4 GB |
Servers do not hibernate, so the old "twice your RAM" rule does not apply.
How to Increase Swap on Ubuntu
# turn off current swap (its contents move back to RAM)
sudo swapoff /swapfile
# remove the old file (this also lets you shrink swap)
sudo rm /swapfile
# recreate the file at the new size, for example 4 GB
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
The /etc/fstab line stays the same. To remove swap entirely: sudo swapoff /swapfile, delete the line from /etc/fstab, then sudo rm /swapfile.
Errors and Fixes
| Error | Cause | Fix |
|---|---|---|
insecure permissions 0644, 0600 suggested |
file is world-readable | sudo chmod 600 /swapfile |
fallocate failed: Operation not supported |
filesystem does not support fallocate | create the file with dd |
swapon failed: Invalid argument |
the file has holes | recreate it with dd |
swapoff: Cannot allocate memory |
swap contents do not fit in RAM | stop heavy processes and retry |
| Swap gone after reboot | no /etc/fstab entry |
repeat step 3 |
| Processes still get killed, swap is full | you are genuinely out of memory | find the culprit with htop, move to a bigger plan |
Swap or a Bigger Plan?
If free -h shows swap constantly full and the server feels slow, you need more RAM, not more swap. Find what is eating resources with how to check server load, and size your next plan with how to choose a VPS plan.
| Plan | vCPU / RAM / NVMe | Monthly |
|---|---|---|
| s1-1 | 1 / 1 GB / 15 GB | $4.49 |
| s1-2 | 1 / 2 GB / 30 GB | $7.99 |
| s2-4 | 2 / 4 GB / 60 GB | $15.99 |
| s4-8 | 4 / 8 GB / 120 GB | $31.99 |
All plans run on NVMe; full terms on the pricing page. You can test a bigger plan without paying for a month: spin up an hourly server (s2-4 is about $0.109520 an hour), move the workload over and compare free -h. If it is not worth it, delete the server and the unused amount returns to your balance.
You can also hand the swap setup to an AI agent:
Connect to my server over SSH or through the 0x.cloud MCP. If there is no swap yet (swapon --show prints nothing), create a 2 GB /swapfile: fallocate, chmod 600, mkswap, swapon. Back up /etc/fstab, add the swap line, validate it with findmnt --verify, and set vm.swappiness=10 in /etc/sysctl.d/99-swappiness.conf. Show me free -h at the end.
FAQ
What is swap on Ubuntu? Swap is disk space the kernel uses to park rarely used memory pages when RAM runs low. It keeps processes from being killed, but it is much slower than real RAM.
How much swap does a server need? For a VPS with 1–2 GB of RAM, swap equal to RAM (1–2 GB) is usually enough. On 4–8 GB servers, 2–4 GB is plenty. More swap does not make up for missing memory.
Does swap slow down a server? Only while the system is actively swapping. NVMe makes it far less painful than HDD, but if swap stays full all the time, the server needs more RAM.
How do I check whether swap is enabled? Run swapon --show or free -h. Empty output from swapon --show, or a Swap line of zeros in free -h, means no swap is active.
Can I add swap without rebooting? Yes. You create the swap file and enable it with swapon on a running system. The /etc/fstab entry only makes sure it comes back after a reboot.