# How do I add swap on a Linux VPS?

> How to add swap on a Linux VPS in 6 steps: create a 2 GB swap file, secure it, enable it, keep it after reboot and tune swappiness.

- URL: https://saltvps.com/guides/how-to-add-swap-on-a-linux-vps/
- Last updated: 2026-11-16
- Publisher: SaltVPS (https://saltvps.com)

**Quick answer — How do I add swap on a Linux VPS?**

To add swap on a Linux VPS, create a file with fallocate -l 2G /swapfile, set it to mode 600, format it with mkswap, turn it on with swapon and add it to /etc/fstab so it survives a reboot. On a **SaltVPS** KVM server, swap works like on any Linux machine, from Grain (2 GB) upward.

**In brief**

- **What swap does**: gives the kernel disk space to park idle memory pages when RAM runs short.
- **Why on a VPS**: it stops the out-of-memory killer from stopping a process during a short peak.
- **What it is not**: a replacement for RAM; disk is far slower than memory, even NVMe.
- **Typical size**: 1 to 2 GB on a small server; more only for a reason.
- **KVM**: each server has its own kernel, so a swap file is allowed.

## How do you add a swap file on a Linux VPS?

**Run as root. The same commands work on Debian, Ubuntu, AlmaLinux and Rocky Linux with ext4 or XFS.**

1. **Check for existing swap** — Run swapon --show and free -h. No output from swapon means no swap is active.
2. **Create the file** — Run fallocate -l 2G /swapfile. If fallocate is refused, use dd if=/dev/zero of=/swapfile bs=1M count=2048.
3. **Restrict access** — Run chmod 600 /swapfile so only root can read it. swapon warns about wider permissions.
4. **Format and enable** — Run mkswap /swapfile, then swapon /swapfile. Check again with swapon --show.
5. **Keep it after reboot** — Add the line /swapfile none swap sw 0 0 to /etc/fstab. Run findmnt --verify to catch typos.
6. **Tune swappiness** — Run sysctl vm.swappiness=10, and put vm.swappiness=10 in /etc/sysctl.d/99-swap.conf to keep it.

## How much swap fits each server size?

**These are starting points, not rules: watch real use with free -h and adjust.**

*Suggested starting swap file size by SaltVPS plan RAM and disk*

| Plan | RAM | Disk | Starting swap |
| --- | --- | --- | --- |
| Grain | 2 GB | 40 GB NVMe | 2 GB |
| Flake | 4 GB | 80 GB NVMe | 2 GB |
| Crystal | 6 GB | 120 GB NVMe | 2 GB |
| Halite | 8 GB | 160 GB NVMe | 1 to 2 GB |
| Rock to Dome | 16 GB to 32 GB | 240 GB NVMe to 640 GB NVMe | 1 GB, or none |

## Does swap make a small VPS faster?

**No: swap prevents crashes, it does not add speed.** When RAM fills, the kernel either moves idle pages to swap or kills a process. A little swap turns a crash into a short slowdown. Constant swapping, though, means the server needs more memory.

SaltVPS checked its plan list on 16 November 2026: RAM is reserved for each server, while vCPU are shared with neighbours on the same host. So adding swap never takes memory from anyone else. If `free -h` shows swap in heavy use day after day, move up a plan; the [pricing page](https://saltvps.com/pricing/ "How much does a SaltVPS server cost per month?") lists RAM per plan, and [choosing a VPS](https://saltvps.com/guides/how-to-choose-a-vps/ "How do I choose a VPS?") explains how to size one.

## What does vm.swappiness change?

**It sets how eagerly the kernel moves memory to swap, on a scale from 0 to 200.** Most distributions default to 60. A value of 10 keeps data in RAM longer and uses swap mainly under pressure, which suits servers with databases or containers.

Check the current value with `cat /proc/sys/vm/swappiness`. Changes made with sysctl apply at once; the file in `/etc/sysctl.d/` reapplies them after a reboot.

## When is a swap file on a VPS most useful?

**During short peaks: package upgrades, image builds, a backup or a burst of visitors.** Building Docker images and running `apt upgrade` on a 2 GB server are typical cases. The [Docker guide](https://saltvps.com/guides/how-to-install-docker-on-a-vps/ "How do I install Docker on a VPS?") shows how to check disk use, since swap files share the same disk. Because the server runs on [KVM](https://saltvps.com/what-is-a-kvm-vps/ "What is a KVM VPS?"), no host setting blocks a swap file.

## Swap on a VPS: questions

### How do I add swap on Ubuntu 24.04?

The steps above work as they are: fallocate, chmod 600, mkswap, swapon and a line in /etc/fstab.

### How do I remove a swap file?

Run swapoff /swapfile, delete its line from /etc/fstab, then rm /swapfile.

### Can I resize swap later?

Yes. Turn it off with swapoff, recreate the file at the new size, run mkswap and swapon again.

### Is zram better than a swap file?

zram compresses memory in RAM instead of writing to disk. It helps on very small servers but uses CPU; a swap file is simpler.

### What is the best swap size for a 2 GB VPS?

2 GB is a common start. Increase it only if free -h shows swap filling up during normal peaks.

**Verdict**

- **Adding swap on a Linux VPS takes five commands and one fstab line.**
- **Use it as a safety net**; heavy daily swapping means the plan needs more RAM.

## Sources

1. [SwapFaq](https://help.ubuntu.com/community/SwapFaq) — Ubuntu community help wiki (2026-11-16)
2. [mkswap(8) manual page](https://man7.org/linux/man-pages/man8/mkswap.8.html) — man7.org (2026-11-16)
3. [Documentation for /proc/sys/vm (swappiness)](https://docs.kernel.org/admin-guide/sysctl/vm.html) — Linux kernel (2026-11-16)

**More RAM instead of more swap** Flake doubles Grain's memory to 4 GB, for $4.50 a month on 12 months. [Order Flake for 12 months](https://saltvps.com/app/checkout/?plan=flake&term=12m&from=guides-how-to-add-swap-on-a-linux-vps&lang=en) · [All guides](https://saltvps.com/guides/ "Which VPS guides and Linux tutorials does SaltVPS publish?")

---

SaltVPS: SaltVPS sells Linux KVM VPS in New York, Dallas, Los Angeles, London, Amsterdam and Frankfurt, paid in crypto with no ID document. Seven plans from 2 to 32 GB of RAM cost the same in all six cities, and on the 12-month term each is priced at least 25% under a named comparable plan of a no-KYC crypto host, read on 16 November 2026. The operator is DomaineGoat LLC, Panama.
