Creating a SWAP file on Linux

3 min read

A swap is a disk space (or file) that operating systems use when physical RAM becomes full or is running out. If a Linux system starts to run out of memory, the kernel begins to move inactive (long-unused or never-used) memory areas to the swap space, thereby freeing up memory.

The swap space can be either a dedicated swap partition or a swap file. In most cases, when running Linux in a virtual machine, there is no dedicated swap partition, so the only option is to create a swap file.

The method described here has been tested on Ubuntu 20.04, CentOS and AlmaLinux 8, but it will work on any modern Linux distribution.

How to add a swap file

Follow the steps below to create 1 GB of swap space. If you wish to add 2 GB instead of 1 GB, replace ‘1G’ with ‘2G’ or any other value (e.g. 512M or 4G).

First, let’s create a file to be used as swap space:

sudo fallocate -l 1G /swapfile

If the fallocate command is not installed on the machine, or if you receive the error “fallocate failed: Operation not supported”, you can also use the dd command:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576

Only the root user should be able to read and write to the swap file. Set the appropriate file permissions:

sudo chmod 600 /swapfile

Use the mkswap command to format the swap file:

sudo mkswap /swapfile

Enable the use of the swap file with the ‘swapon’ command:

sudo swapon /swapfile

To ensure this happens automatically every time, you need to configure the use of the swap file in the /etc/fstab file. Open the file in your favourite editor and add the following on a new line:

/swapfile swap swap defaults 0 0

To check whether the swap space is active, you can use the ‘swapon’ or ‘free’ command as follows:

sudo swapon --show

We should see something similar:

NAME      TYPE SIZE   USED PRIO
/swapfile file   2G 530.8M   -2
Video: How to Create, Resize, or Extend a Linux Swap File | (Ubuntu) (SavvyNik)

How to set the swappiness value

‘Swappiness’ is a Linux kernel parameter that determines how often the system uses swap space. The parameter can take a value between 0 and 100. A low value causes the kernel to try to avoid using swap whenever possible, whilst a higher value causes the kernel to use swap space more aggressively.

On most Linux systems, the default value is 60, but we can also check this manually:

cat /proc/sys/vm/swappiness

The output is the current value:

60

Whilst a swap value of 60 is suitable for most Linux systems, in the case of production servers it may be necessary to set a lower value.

For example, to set the swap value to 10, run the following sysctl command:

sudo sysctl vm.swappiness=10

To ensure that this setting remains permanent after reboots, add the following line to the /etc/sysctl.conf file:

vm.swappiness=10

The optimal swappiness value depends on the system load and memory usage. This parameter should be adjusted in small increments to find the optimal value.

Removing a swap file

If, for any reason, you wish to deactivate and remove the swap file, follow the steps below:

First, disable the swap space by entering the following command:

sudo swapoff -v /swapfile

This can take quite a long time if the swap file is large or your system is slow. If there isn’t enough free memory, the operation may even fail.

The next step is to remove the line beginning with “/swapfile” from the /etc/fstab file.

Finally, delete the actual swap file using the rm command:

sudo rm /swapfile

If you encounter any problems or have any feedback, please get in touch with us!

Back to the blog