How to Add a Swap File on a Linux VPS — Step by Step

19 Mar 2026 By Edgars Kalējs

Run on the server with one command (check the script contents below first):

curl -sSL https://cp.lv/scripts/nastrojka-swap-vps.sh | sudo bash

…or with wget:

wget -qO- https://cp.lv/scripts/nastrojka-swap-vps.sh | sudo bash

Script contents nastrojka-swap-vps.sh:

#!/usr/bin/env bash
# Create a 2 GB swap file, enable it at boot via fstab, set swappiness
set -euo pipefail

SWAPFILE=/swapfile
SWAPSIZE=2G

# 1. Create the file (fallocate, falling back to dd)
if ! sudo fallocate -l "$SWAPSIZE" "$SWAPFILE"; then
  sudo dd if=/dev/zero of="$SWAPFILE" bs=1M count=2048
fi
sudo chmod 600 "$SWAPFILE"

# 2. Format it as swap and turn it on
sudo mkswap "$SWAPFILE"
sudo swapon "$SWAPFILE"

# 3. Enable at boot via fstab (only if not listed yet)
if ! grep -q "^${SWAPFILE} " /etc/fstab; then
  echo "${SWAPFILE} none swap sw 0 0" | sudo tee -a /etc/fstab
fi

# 4. swappiness=10 now and after reboot (heredoc into a sysctl.d file)
sudo sysctl vm.swappiness=10
sudo tee /etc/sysctl.d/99-swappiness.conf > /dev/null << 'EOF'
vm.swappiness=10
EOF

sudo swapon --show
free -h
echo "Done: the 2 GB swap file is active, swappiness=10."

Swap is a file or partition on disk that the system uses as “spare” memory when RAM runs out. On a VPS the easiest option is a swap file: the fallocate, mkswap and swapon commands take a couple of minutes. Here is the whole process and how to tune it.

Why swap is useful

Swap does not replace RAM (it is many times slower), but it protects you from processes being killed during load spikes. The kernel moves rarely used memory pages to swap, freeing RAM for active work. That is especially useful on plans with a small amount of memory.

Step 1. Check the current swap

sudo swapon --show
free -h

If swapon prints nothing, swap is not configured yet.

Step 2. Create the swap file

Create a file of the size you need. For example, 2 GB:

sudo fallocate -l 2G /swapfile

If fallocate is not available, use dd:

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

Restrict the permissions so only root can read the file:

sudo chmod 600 /swapfile

Step 3. Format it and turn it on

Mark the file as swap and activate it:

sudo mkswap /swapfile
sudo swapon /swapfile

Check that swap has appeared:

sudo swapon --show
free -h

Step 4. Enable it at boot

For now swap only lives until the next reboot. To have it enabled automatically, add an entry to /etc/fstab:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Check that the line was added correctly:

grep swapfile /etc/fstab

Step 5. Tune swappiness

The vm.swappiness parameter (0–100) decides how eagerly the kernel uses swap. On a server it is usually lowered to 10, so swap kicks in only when RAM really runs short:

sudo sysctl vm.swappiness=10

To keep the value after a reboot:

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

How much swap to create

RAM Recommended swap
up to 2 GB equal to RAM
2–8 GB equal to RAM
more than 8 GB 2–4 GB is enough

This is a guideline, not a hard rule: for a database server it is better to keep swap small to avoid performance degradation.

How to remove swap

If you no longer need swap:

sudo swapoff /swapfile
sudo rm /swapfile

Then delete the matching line from /etc/fstab.

Frequently asked questions

Will swap slow the server down? Swap on disk is dozens of times slower than RAM. As long as the data in swap is rarely touched, the impact is minimal. Constant heavy swapping means you are out of memory and should add RAM.

Swap file or swap partition? On a VPS a file is more convenient: it is easy to create, resize and delete without repartitioning the disk. A partition is rarely needed.

Which swappiness should I choose for a server? Usually 10 — swap is used only when memory really runs short. The default of 60 is too aggressive for servers running databases.

Can I create swap on a VPS with SSD? Yes. On NVMe/SSD swap is faster than on an HDD, but still slower than RAM. It is better to avoid constant heavy use so you do not wear the disk out.

Summary

A swap file is simple insurance against running out of memory under load. Create it with fallocate, enable it with swapon, add it to fstab and set swappiness=10. If swap is in use all the time, that is a signal to move to a plan with more RAM.

Need more memory? Pick a plan in the Linux VPS section, order VPS hosting, or order Server administration.

Edgars Kalējs