📢 LordCloud support has moved to Ballastra — Join now →
VPS & Linux

Ubuntu Create Swap File: Add Swap to a Small VPS

A swap file is a cheap safety net against out-of-memory kills on small servers. Here is how to create one on Ubuntu, make it survive reboots and know when it is not enough.

LordCloud Engineering

Infrastructure & Support Team

Published · 5 min read
Rows of server hardware in a data centre
On this page

To create a swap file on Ubuntu, you make a file of the size you want, lock its permissions down to root, format it with mkswap, turn it on with swapon and add one line to /etc/fstab so it comes back after a reboot. The whole job takes about two minutes on a VPS, and it can be the difference between a busy server surviving a memory spike and the kernel killing your database.

Many small VPS images ship with no swap at all. That is fine until a package upgrade, a build step or a traffic burst pushes memory past what the plan has. Then the out-of-memory (OOM) killer picks a victim, usually your biggest process. This guide shows the exact commands, how to size the file, how to tune it and how to remove it again.

Check whether your VPS already has swap#

Before adding anything, look at what exists:

bash
free -h
swapon --show

If swapon --show prints nothing, there is no active swap. If you see a /swap.img or /swapfile entry, Ubuntu or your image already created one, and you may only need to resize it. Memory problems usually show up in the kernel log, so also check for past kills:

bash
sudo journalctl -k | grep -i "out of memory"

A line like "Out of memory: Killed process" tells you the box has already run out at least once.

How big should a swap file be?#

Treat swap as a safety net, not extra RAM. Disk is far slower than memory, so a server that lives in swap feels frozen. For a small VPS a sensible starting point is:

  • 1 GB RAM: 1 to 2 GB of swap.
  • 2 to 4 GB RAM: 1 to 2 GB of swap.
  • 8 GB RAM or more: 1 to 2 GB is usually plenty for spikes, unless you have a specific reason for more.

These are rules of thumb from experience, not fixed requirements. Pick a size, watch real usage for a week, and adjust. If swap use keeps growing, the fix is more RAM or a lighter workload, not a bigger swap file. Our adding swap docs use a 2 GB example, and you can compare plan sizes on the VPS hosting page if you outgrow it.

Create the swap file step by step#

Run these as a user with sudo. The example creates a 2 GB file at /swapfile.

1. Allocate the file#

The most portable method is dd reading from /dev/zero. The swapon manual page notes that files created with tools such as cp or truncate contain holes and are rejected, and that fallocate can also count as having holes on some filesystems (swapon(8)).

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

On the default ext4 filesystem, sudo fallocate -l 2G /swapfile normally works and is faster. If a later step complains about holes, delete the file and use dd.

2. Lock down permissions#

bash
sudo chmod 600 /swapfile

Swap can contain anything that was in memory, including passwords and keys, so only root should read it. Newer versions of the tools warn if the file is readable by others.

3. Format it as swap#

bash
sudo mkswap /swapfile

The mkswap manual page lists this as the step that sets up the swap area inside the file (mkswap(8)). Do not pass a size argument; the page advises against it.

4. Enable it#

bash
sudo swapon /swapfile
swapon --show
free -h

You should see /swapfile listed with the size you chose, and the Swap row in free -h should match.

Make the swap file permanent with fstab#

A swap area enabled with swapon lasts until reboot. To bring it back automatically, back up fstab and append one line:

bash
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Test it without rebooting by turning swap off and then enabling everything listed in fstab:

bash
sudo swapoff /swapfile
sudo swapon -a
swapon --show

If the file shows up again, the entry is correct. A typo in fstab can cause boot problems on some setups, which is why the backup copy is worth the extra second. If you lose SSH access after a reboot, see our SSH access docs for ways back in.

Tune vm.swappiness#

vm.swappiness controls how willing the kernel is to swap. The kernel documentation describes it as the estimated cost of swapping relative to filesystem paging, accepting values from 0 to 200 with a default of 60 (kernel vm documentation). Lower values make the kernel favour keeping application memory in RAM.

For a database or app server on a small VPS, many administrators set it to 10 so swap is used mainly under real pressure. This is a common convention, not a hard rule, so test with your own workload.

bash
cat /proc/sys/vm/swappiness
sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf

The first command shows the current value, the second applies it immediately and the third keeps it after reboot.

Resize or remove the swap file#

To change the size, switch it off, delete it and recreate it:

bash
sudo swapoff /swapfile
sudo rm /swapfile

Then repeat the creation steps with a new size. To remove it for good, also delete the /swapfile line from /etc/fstab:

bash
sudo nano /etc/fstab
swapon --show

If swapoff hangs or fails, the system does not have enough free RAM to take back what is in swap. Close heavy processes first or run it during a quiet period.

When swap is not enough#

Swap buys time; it does not fix a workload that is too big. Signs you should upgrade rather than tune:

  • free -h shows swap usage that keeps climbing over days.
  • The server becomes slow or unresponsive during normal traffic.
  • Kernel logs keep reporting OOM kills even with swap enabled.

Use top or the tools in our server monitoring docs to find which process is eating memory. If you already followed our guide to deploying a Node.js app with Nginx and PM2, PM2's process list shows per-app memory quickly. Upgrading a plan is covered in the upgrade docs.

Frequently asked questions#

How do I create a swap file on Ubuntu?#

Allocate a file with dd, set it to chmod 600, run mkswap, enable it with swapon and add /swapfile none swap sw 0 0 to /etc/fstab. Confirm with swapon --show.

How big should a swap file be on a VPS?#

Start with 1 to 2 GB on a small VPS. It is a buffer for short spikes. If it is used heavily day after day, you need more RAM.

Is swap bad on an SSD or NVMe VPS?#

For a small safety-net swap file it is fine. Swap is still far slower than RAM, so constant swapping will make the server sluggish no matter what kind of disk you have.

How do I remove a swap file in Ubuntu?#

Run sudo swapoff /swapfile, delete the /swapfile line from /etc/fstab, then sudo rm /swapfile. Verify with swapon --show.

Frequently asked questions

How do I create a swap file on Ubuntu?

Create a file with dd or fallocate, set permissions to 600, run mkswap on it, enable it with swapon and add a line to /etc/fstab so it returns after a reboot. Confirm with swapon --show and free -h.

How big should a swap file be on a VPS?

For a small VPS, 1 to 2 GB is a practical start. It is a safety net for short memory spikes, not a substitute for RAM. If the swap is regularly in use, move to a plan with more memory.

Is swap bad on an SSD or NVMe VPS?

Not in a way that matters for a small safety-net swap file. Swap is still much slower than RAM, so heavy swapping will hurt performance, but occasional use during spikes is normal and harmless.

How do I remove a swap file in Ubuntu?

Run swapoff on the file, delete the matching line from /etc/fstab, then remove the file with rm. Check swapon --show afterwards to confirm it is gone.

References

Drafted with AI assistance and checked against LordCloud's published product information. Spotted a mistake?Suggest an edit.

All articles →
Have a question? Chat with us👋