Create, Resize or Remove a Swap File on Linux

Short answer: add a 4 GB swap file
sudo fallocate -l 4G /swapfile        # use dd if fallocate fails (see below)
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab
swapon --show

This works on ext4 and XFS on every distro. On Btrfs (Fedora, openSUSE, many Arch installs) use the Btrfs method instead, and check first whether you already have zram.

Check what swap you already have

Before adding anything, see what's active. Many systems already have a swap partition, a swap file or zram.

swapon --show
free -h
cat /proc/sys/vm/swappiness
lsblk -f | grep -i swap

Example output:

NAME       TYPE      SIZE USED PRIO
/swap.img  file        4G   0B   -2
/dev/zram0 partition 7.7G 1.2G  100

A /dev/zram0 entry means compressed RAM swap (default on Fedora). /swap.img or /swapfile is a file (Ubuntu installs one by default). A /dev/sdX or /dev/nvme0n1pX entry is a partition. Higher PRIO is used first. Check which filesystem holds your root with findmnt -no FSTYPE /: the method differs on Btrfs.

Create a swap file on ext4 or XFS

1. Allocate the file

sudo fallocate -l 4G /swapfile

fallocate reserves blocks instantly. If swapon later complains that the file "has holes", or you are on an unusual filesystem, create it with dd, which writes real zeros:

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

2. Lock down permissions

sudo chmod 600 /swapfile

Swap can contain passwords and keys from memory, so only root may read it. swapon warns about "insecure permissions" otherwise.

3. Format and enable it

sudo mkswap /swapfile
sudo swapon /swapfile

4. Make it permanent

echo '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab
sudo systemctl daemon-reload
sudo swapoff /swapfile && sudo swapon -a && swapon --show

The last line proves the fstab entry works before you reboot. For more on fstab syntax, see the disk management guide.

Resize or remove an existing swap file

You can't grow an active swap file in place. Turn it off, recreate it at the new size, and turn it back on. The path in fstab stays the same, so you don't need to edit it.

# Ubuntu's default file is /swap.img; others use /swapfile
sudo swapoff /swap.img
sudo rm /swap.img
sudo fallocate -l 8G /swap.img
sudo chmod 600 /swap.img
sudo mkswap /swap.img
sudo swapon /swap.img
swapon --show
swapoff needs free RAM

swapoff moves everything in swap back into RAM. If swap is heavily used and RAM is nearly full, it can be slow or trigger the OOM killer. Close large programs first or do it right after a reboot.

To remove swap entirely: sudo swapoff /swapfile, delete its line from /etc/fstab, then sudo rm /swapfile. Remove the fstab line before rebooting or systemd will wait for a missing swap device.

Swap file on Btrfs

Fedora, openSUSE and many Arch installs use Btrfs. A Btrfs swap file must be No_COW, not compressed, on a single device, and on a subvolume that is not snapshotted (an active swap file blocks snapshots of its subvolume). With btrfs-progs 6.1 or newer, one command handles the details:

# a dedicated subvolume keeps the file out of root snapshots
sudo btrfs subvolume create /swap
sudo btrfs filesystem mkswapfile --size 4g --uuid clear /swap/swapfile
sudo swapon /swap/swapfile
echo '/swap/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab

On older btrfs-progs, do it manually: sudo truncate -s 0 /swap/swapfile, sudo chattr +C /swap/swapfile, then dd, chmod 600 and mkswap as above. The +C attribute must be set while the file is still empty.

If your root subvolume is mounted from @ (Ubuntu-style layouts used by Timeshift), create the swap subvolume at the top level and mount it separately, so Timeshift and Snapper snapshots don't include it.

Tune swappiness

vm.swappiness (0–200, default 60) controls how readily the kernel swaps out anonymous memory compared with dropping file cache. Lower values keep applications in RAM longer; it does not disable swap.

# try it now
sudo sysctl vm.swappiness=10
# make it permanent
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl --system
Which value?

With disk swap only, 10–30 suits desktops that feel sluggish after swapping. With zram, high values (100–180) are reasonable because compressed RAM swap is cheap; Fedora and others tune this for you.

Per-distro notes and zram

zram creates a compressed block device in RAM and uses it as swap. A 2:1 to 3:1 compression ratio is typical, so 8 GB of RAM behaves like noticeably more. It's handled by zram-generator ([email protected]).

Ubuntu, Debian and Linux Mint

Ubuntu's installer creates /swap.img on ext4 installs. Debian's installer usually creates a swap partition. Mint creates /swapfile. To add zram:

sudo apt install systemd-zram-generator
sudo tee /etc/systemd/zram-generator.conf <<'EOF'
[zram0]
zram-size = min(ram / 2, 8192)
compression-algorithm = zstd
EOF
sudo systemctl daemon-reload
sudo systemctl start [email protected]
swapon --show

Older guides use zram-tools or zram-config. Pick one zram tool only; don't install several.

Fedora

Fedora enables zram by default through zram-generator-defaults and creates no disk swap. Change the size by creating /etc/systemd/zram-generator.conf (it overrides /usr/lib/systemd/zram-generator.conf), then restart [email protected]. Fedora's root is Btrfs, so add disk swap with the Btrfs method. You only need it if you want hibernation or extra overflow.

Arch Linux

Arch creates nothing by default. Use a swap file (ext4 or Btrfs method) and/or zram: sudo pacman -S zram-generator, then write the same zram-generator.conf as above. The Arch Wiki Swap and zram pages cover edge cases.

openSUSE

The installer normally creates a swap partition next to Btrfs root. To add a file, use the Btrfs method with a dedicated subvolume so Snapper doesn't snapshot it. zram is available as zram-generator via sudo zypper install zram-generator.

Swap for hibernation

Hibernation writes RAM to disk swap (not zram), so swap must be at least as big as the memory in use. With a swap file, the kernel also needs to know where it is:

# UUID of the filesystem holding the swap file
findmnt -no UUID -T /swapfile
# physical offset (ext4): start of the first extent
sudo filefrag -v /swapfile | awk '$1=="0:" {print substr($4, 1, length($4)-2)}'
# physical offset on Btrfs
sudo btrfs inspect-internal map-swapfile -r /swap/swapfile

Add resume=UUID=<uuid> resume_offset=<offset> to your boot parameters (see how to set kernel parameters) and rebuild the initramfs. Recent systemd can find the swap device on its own on some setups, but setting it explicitly is the reliable option. If resume fails, the suspend and hibernate troubleshooting guide covers Secure Boot lockdown and NVIDIA issues.

Verify it works

swapon --show
free -h
cat /proc/swaps
# zram stats: original vs compressed size
zramctl
# after a reboot, check that fstab swap came up
systemctl list-units --type swap

Swap is working if swapon --show lists it after a reboot. Some USED on a busy machine is normal and not a problem on its own. Constant heavy swapping (watch the si/so columns in vmstat 1) means you need more RAM or need to find the memory hog. See finding what's using CPU and memory.

Common problems

SymptomCause and fix
swapon: /swapfile: swapon failed: Invalid argumentFile has holes, is on Btrfs without No_COW, or lives on an unsupported filesystem (some network or FUSE filesystems). Recreate with dd or btrfs filesystem mkswapfile.
insecure permissions 0644, fix with: chmod 0600Run sudo chmod 600 /swapfile.
fallocate failed: Operation not supportedThe filesystem doesn't support fallocate. Use dd.
Boot waits 90 seconds for a swap deviceStale UUID or path in /etc/fstab (common after reinstalling or deleting a swap partition). Fix or remove the line, and check /etc/initramfs-tools/conf.d/resume on older Debian/Ubuntu.
Btrfs snapshot fails: "cannot snapshot subvolume with active swapfile"Move the swap file into its own subvolume as shown above.
Swap never used, system freezes insteadSwap too small or swappiness very low. Add swap, or install systemd-oomd / earlyoom so a single process gets killed before the whole desktop stalls.

If programs still die with "Cannot allocate memory" while free RAM and swap look fine, the cause is usually overcommit limits or cgroup memory caps, not swap size.

Frequently asked questions

How much swap do I need?

For most desktops 2–8 GB is plenty if you don't hibernate. To hibernate, swap must be at least as large as your RAM in use, so size it close to your RAM.

Is a swap file slower than a swap partition?

No. On ext4 and XFS the kernel maps the file's blocks directly, so performance is effectively the same, and a file is much easier to resize.

Should I use zram or a swap file?

zram compresses memory in RAM and is faster, so it suits most desktops. You can use both: zram at high priority and a disk swap file as overflow or for hibernation.

Why does swapon say "Invalid argument"?

Usually the file has holes (created with fallocate on a filesystem that doesn't support it for swap) or it is on Btrfs without No_COW. Recreate it with dd or with btrfs filesystem mkswapfile.

Spotted a mistake or a command that no longer works? Send a correction — pages are re-checked when reports come in.