Adding swap after installation
by Bryanpwo edited by joekamprad (12.08.2020) + (24.02.2021) + (28.09.2023) + (09.09.2025)
To add after installation there are two options:
-
creating a swap partition by shrinking space and adding a partition using Gparted on the Live ISO
-
creating a swap file.
ℹ
Early SSDs had a reputation for failing after fewer writes than HDDs.
This might be why you heard it could be bad to use an SSD for swap.
Modern SSDs don’t have this issue
Making a swap partition with Gparted
Section titled “Making a swap partition with Gparted”Boot your machine with the Live ISO and open Gparted. Select the last partition on your HDD (this can either be the Root partition or the Home partition, depending on how you partitioned your disk) and shrink the partition by leaving a disk space free equal to the size of your RAM or double the size, that’s up to you. Then select the free space and mark it as a swap partition, let Gparted do its work, and boot back into your installed system and swap should be recognized by systemd. To check enter:
swapon --showMaking a swap file
Section titled “Making a swap file”The above method is the easiest one, but it can damage the partition that you’ve shrunk. So you can also make a swap file.
Commands like cp(1) or truncate(1) create files with holes. These files will be rejected by swapon.
Preallocated files created by fallocate(1) may be interpreted as files with holes too depending of the filesystem.
As documented here: swapon.8#Files_with_holes
So as Archwiki recommends better use dd command with some danger of using a Destroyer of Disks.
!! This following step is only needed if you run on Btrfs filesystem !!
Notes if you are using Btrfs
Section titled “Notes if you are using Btrfs”Note: Swap files on file systems spanning multiple devices are not supported. See btrfs(5) § SWAPFILE SUPPORT for all limitations.
To properly initialize a swap file, first create a non-snapshotted subvolume to host the file, e.g.
sudo btrfs subvolume create /swapTip: Consider creating the subvolume directly below the top-level subvolume, e.g. @swap. Then, make sure the subvolume is mounted to /swap (or any other accessible location).
Create the swap file:
sudo btrfs filesystem mkswapfile --size 8g --uuid clear /swap/swapfileIf --size is omitted, the default of 2GiB is used.
Further for all file systems:
Section titled “Further for all file systems:”Use mkswap to create a swap file the size of your choosing. For example, creating a 8 GiB swap file:
sudo mkswap -U clear --size 8G --file /swapfileTo make the file as swap space and finally to enable the file:
sudo swapon /swapfileEdit the fstab configuration to add an entry for the swap file:
/etc/fstab
/swapfile none swap defaults 0 0Note: The swap file must be specified by its location on the file system, not by its UUID or LABEL.
to check:
swapon --showRemove swap file:
Section titled “Remove swap file:”To remove a swap file, it must be turned off first and then can be removed:
sudo swapoff /swapfilesudo rm -f /swapfileFinally, remove the relevant entry from /etc/fstab.
For more details about swap usage in general and options:
Section titled “For more details about swap usage in general and options:”https://wiki.archlinux.org/title/Swap
Swappiness: https://wiki.archlinux.org/title/Swap#Swappiness