Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Add Swap in Linux: A Safe, Practical Guide

· 5 min read · Home Tech

In this article
  1. What Linux swap does
  2. Check existing swap before adding more
  3. Choose a size and location
  4. Add a swap file
  5. Make swap persist after reboot
  6. Common problems and useful alternatives
  7. Frequently Asked Questions

Yes—you can add swap space to Linux by creating a swap file or setting up a swap partition. A swap file is usually the simplest option: it can be created without repartitioning a disk, enabled immediately, and made persistent across reboots with a configuration entry.

What Linux swap does

Swap is disk space that the Linux kernel can use to hold memory pages when physical RAM is under pressure. It can help a system keep running when active workloads need more memory than RAM alone can provide. Because disks and SSDs are much slower than RAM, swap is not a substitute for adequate memory: heavy swapping can make a computer or server sluggish.

Swap is also involved in some suspend-to-disk (hibernation) setups, which save system state before powering off. Hibernation has additional requirements: the available swap must be large enough for the system’s memory image, and the distribution and boot configuration must support restoring it. Simply adding a small swap file does not automatically enable hibernation.

Check existing swap before adding more

First find out whether swap is already configured. These commands report active swap and memory use:

  • swapon --show lists active swap devices and files.
  • free -h shows RAM and swap totals and current use.

If swapon --show lists an existing swap file or partition, you may not need to add another. Swap use by itself is not proof of a problem: the kernel may move inactive pages out of RAM while leaving memory available for useful work. Look at overall memory pressure and responsiveness, not just whether the swap-used figure is nonzero.

Choose a size and location

There is no universal swap-size rule. The right amount depends on RAM, the workload, available storage, and whether hibernation is required. A lightly used desktop and a memory-intensive server have different needs; hibernation can require substantially more swap than ordinary memory-pressure protection. Check your distribution’s documentation and hibernation guidance before choosing a size for that purpose.

A swap file is convenient on many common Linux filesystems. It must be created on a filesystem and storage device that support swap files as configured by the distribution; some filesystems or special storage arrangements have extra restrictions. If the system uses such a setup, consult its documentation or choose a supported swap partition or other supported swap device. Put swap on storage with enough free space, and remember that using an SSD for swap is generally supported, though sustained heavy swapping creates extra writes and is best treated as a sign to investigate memory demand.

Add a swap file

The following example creates a 2 GiB swap file at /swapfile. Run commands with administrative privileges, such as through sudo. Confirm that the path is not already in use and that the filesystem supports this method before proceeding.

  1. Create a file of the desired size: sudo fallocate -l 2G /swapfile. If fallocate is unsupported or produces a file unsuitable for swap on your filesystem, use the filesystem’s documented method; on many systems, sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress is an alternative.
  2. Restrict access so ordinary users cannot read the swap file: sudo chmod 600 /swapfile.
  3. Format it for swap: sudo mkswap /swapfile.
  4. Enable it now: sudo swapon /swapfile.
  5. Verify it appears: swapon --show or free -h.

Be careful with the file path and size before running these commands. Do not format or overwrite an existing file that contains data. The permission change is important because swap can contain fragments of information that were in memory.

Make swap persist after reboot

Enabling swap with swapon normally lasts only until shutdown or reboot. To enable the file at startup, add this line to /etc/fstab:

/swapfile none swap sw 0 0

Edit the file with administrative privileges, and check that the entry is correct before rebooting. A typo or incorrect device path can cause startup or mount errors. You can test the configuration without rebooting by turning the swap file off and asking the system to enable all swap entries:

sudo swapoff /swapfile
sudo swapon -a

Only run swapoff when the system has enough available memory to move the pages back into RAM. If it cannot, the command may fail; avoid forcing it on a memory-constrained system.

Common problems and useful alternatives

If enabling the file reports an invalid argument, check filesystem support and how the file was allocated. If the system reports insufficient memory during swapoff, leave swap enabled, reduce memory use, or add memory before trying again. If swap is active but the computer remains very slow, inspect memory use and the processes creating pressure; more swap may prevent an out-of-memory failure but will not make disk-backed memory as fast as RAM.

Other options include adding or resizing a swap partition, using a distribution-supported zram setup (compressed swap held in RAM), or increasing physical memory. A swap partition is not inherently faster than a swap file on every system; performance depends on the storage and configuration. Zram can help in some memory-limited systems, but it uses CPU time and is not the same as extra disk capacity or a hibernation target.

Frequently Asked Questions

How much swap should I add to Linux?
Choose a size based on workload, RAM, available storage, and hibernation needs. There is no single ratio that suits every machine. For hibernation, follow your distribution’s requirements rather than assuming a general-purpose swap size will be enough.

Can I add swap without restarting?
Yes. After creating and formatting a supported swap file, sudo swapon /swapfile activates it immediately. Add a valid /etc/fstab entry separately if it should remain enabled after reboot.

Does adding swap make Linux faster?
Usually not. Swap can help avoid failures when RAM is exhausted, but accessing it is much slower than accessing RAM. If a workload constantly swaps, reduce its memory use or add RAM where possible.

Is a swap file better than a swap partition?
For many users, a swap file is easier to create, resize, and remove without changing disk partitions. A partition may make sense for particular layouts or hibernation configurations. Follow filesystem and distribution guidance for your setup.

How can I remove a swap file?
First remove its line from /etc/fstab, then disable it with sudo swapoff /swapfile when enough RAM is available, and finally delete it with sudo rm /swapfile. Verify the path carefully before deleting anything.