How to Add Swap Space on Ubuntu

By 

Updated on

6 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 combination of partitions and files. Generally, when running Ubuntu on a virtual machine, a swap partition is not present, and the simplest option is to create a swap file.

This guide walks through adding a swap file on Ubuntu. The steps apply to current supported Ubuntu releases, including Ubuntu 24.04 and Ubuntu 26.04.

Quick Reference

TaskCommand
Show active swapsudo swapon --show
Show memory and swap usagefree -h
Check free disk spacedf -h
Create a 4 GB swap filesudo fallocate -l 4G /swap.img
Format the file as swapsudo mkswap /swap.img
Enable the swap filesudo swapon /swap.img
Check swappinesscat /proc/sys/vm/swappiness

Before You Begin

Swap is not a replacement for physical memory. Since swap space is a section of the disk 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, check if you already have swap on your system:

Terminal
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. If the system already has enough swap, you do not need to create another swap file.

Another option is to use the free command:

Terminal
free -h

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

output
               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 :

Terminal
df -h

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

output
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 swap:

Terminal
sudo fallocate -l 4G /swap.img
Info

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 from reading or writing the file:

Terminal
sudo chmod 600 /swap.img

Next, create a Linux swap area on the file:

Terminal
sudo mkswap /swap.img
output
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:

Terminal
sudo swapon /swap.img

To make the change permanent, back up the /etc/fstab file:

Terminal
sudo cp /etc/fstab /etc/fstab.bak

Open the /etc/fstab file:

Terminal
sudo nano /etc/fstab

and paste the following line:

/etc/fstabini
/swap.img swap swap defaults 0 0

Verify that the /etc/fstab file is valid:

Terminal
sudo findmnt --verify

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

Terminal
sudo swapon --show
output
NAME      TYPE SIZE USED PRIO
/swap.img file   4G   0B   -2
Terminal
sudo free -h
output
               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:

Terminal
cat /proc/sys/vm/swappiness
output
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:

Terminal
sudo sysctl vm.swappiness=10

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

/etc/sysctl.confini
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. For recommended values and persistent configuration, see How to Change the Swappiness Value in Linux .

Removing a Swap File

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

Terminal
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:

Terminal
sudo rm /swap.img

Troubleshooting

fallocate failed: Operation not supported
Use the dd command shown above to create the swap file. This can happen on some filesystems or virtualized storage setups.

swapon reports insecure permissions
Run sudo chmod 600 /swap.img and try sudo swapon /swap.img again.

The system does not enable swap after reboot
Check the /etc/fstab entry and run sudo findmnt --verify. Make sure the path in /etc/fstab matches the swap file path.

There is not enough disk space
Run df -h and either free disk space or create a smaller swap file.

Conclusion

You can add swap space on Ubuntu by creating a swap file, enabling it with swapon, and adding it to /etc/fstab. Check existing swap first so you do not create extra swap space on a system that already has enough.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page