How to Add Swap Space on Ubuntu 22.04

Published on

5 min read

Create Swap File on Ubuntu Linux

Swap is a space on a disk drive that is used when the amount of physical RAM memory is full. When a Linux system runs out of RAM, it moves the inactive pages from the RAM to the swap space.

Swap space can take the form of either a dedicated swap partition, a swap file, or a combinaton of partitions and files. Generally, when running Ubuntu on a virtual machine, a swap partition is not present, and the only option is to create a swap file.

This article will guide you through the steps of adding a swap file on an Ubuntu 22.04 system.

Before You Begin

Swap is not a replacement for physical memory. Since swap space is a section of the drive, it has a slower access time than physical memory. If your system constantly runs out of memory, you should add more RAM.

In most cases, the size of the swap file depends on the physical RAM the Linux system has:

  • Systems with less than 2 GB RAM - 2 times the amount of RAM.
  • Systems with 2 to 8 GB RAM - the same size as the amount of RAM.
  • Systems with more than 8 GB RAM - at least 4 GB of Swap.

Only root or users with sudo privileges can activate the swap file.

Before you begin, it’s always a good idea to check if you already have a swap on your system. You can do that by using the following command:

sudo swapon --show

If the output is empty, it means that the system does not have a swap space configured. Otherwise, the command will display a list of configured swap spaces.

Another option is to use the free command:

free -h

The command will print the information about the system’s memory usage, as well as the swap space:

               total        used        free      shared  buff/cache   available
Mem:           3.8Gi       563Mi       2.8Gi       0.0Ki       516Mi       3.1Gi
Swap:             0B          0B          0B

Creating a Swap File

In this guide, we will create a 4 GB swap file. If you need to add more or less swap, replace 4G with the required swap space size.

Before creating the file, ensure you have enough free disk space to complete this process successfully. You can get a detailed report on the system’s disk space usage with the df command :

df -h

Here, the root filesystem (/) has enough free space (53G) to create the file.

Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              197M  948K  196M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   62G  6.3G   53G  11% /
tmpfs                              982M     0  982M   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  129M  1.7G   8% /boot
tmpfs                              197M  4.0K  197M   1% /run/user/1000

The first step is to create a file that will be used as a swap:

sudo fallocate -l 4G /swap.img

If the fallocate utility is not present on your system, or you get an error message saying fallocate failed: Operation not supported, use the following command to create the swap file:

sudo dd if=/dev/zero of=/swap.img bs=1024 count=4194304

Once the file is created, set the file permissions to 600 to prevent regular users to write and read the file:

sudo chmod 600 /swap.img

Next, create a Linux swap area on the file:

sudo mkswap /swap.img
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=6f66b47a-fa4a-4346-8163-dc0337066572

Activate the swap file by running the following command:

sudo swapon /swap.img

To make the change permanent, open the /etc/fstab file:

sudo nano /etc/fstab

and paste the following line:

/etc/fstab
/swap.img swap swap defaults 0 0

Verify that the swap is active using the swapon or the free command, as shown below:

sudo swapon --show
NAME      TYPE SIZE USED PRIO
/swap.img file   4G   0B   -2
sudo free -h
               total        used        free      shared  buff/cache   available
Mem:           3.8Gi       566Mi       2.8Gi       0.0Ki       516Mi       3.1Gi
Swap:          4.0Gi          0B       4.0Gi

Adjusting the Swappiness Value

Swappiness is a Linux kernel property that defines how often the system will use the swap space. It can have a value between 0 and 100. A low value will make the kernel try to avoid swapping whenever possible, while a higher value will make the kernel use the swap space more aggressively.

On Ubuntu, the default swappiness value is set to 60. You can check the current value by typing the following command:

cat /proc/sys/vm/swappiness
60

While the swappiness value of 60 is OK for most Linux systems, you may need to set a lower value for production servers.

For example, to set the swappiness value to 10, run:

sudo sysctl vm.swappiness=10

To make this parameter persistent across reboots, append the following line to the /etc/sysctl.conf file:

/etc/sysctl.conf
vm.swappiness=10

The optimal swappiness value depends on your system workload and how the memory is being used. You should adjust this parameter in small increments to find an optimal value.

Removing a Swap File

To deactivate and delete the swap file, first, deactivate the swap space:

sudo swapoff -v /swap.img

Next, remove the swap file entry /swap.img swap swap defaults 0 0 from the /etc/fstab file.

Finally, remove the actual swap.img file using the rm command:

sudo rm /swap.img

Conclusion

We have shown you how to create a swap file and activate and configure swap space on your Ubuntu 22.04 system.

If you have a problem or feedback, leave a comment below.