Add Swap Space on Ubuntu 20.04 - Step by Step Process ?

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

Memory Swapping is the modern way of managing the memory whenever the physical memory is not sufficient, the operating system uses the swap memory to run or process the program. Swap space is the portion in the disk that is used for temporal storing of program data whenever the physical memory is full. 

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Ubuntu Linux queries.

In this context, we shall look into how to maintain and add swap space in ubuntu 20.04 as it's managed in a Linux system by default.

How to check Allocated Swap Memory in the System ?

Before we continue let's check if the system had any available swap space or not. You can also append multiple swap files but having only one is enough. We can check swap by using the following command.

$ sudo swapon --show

Similarly, we can also check swap using the free command, and add -h flag will show the output in human-readable form. 

It shows the record of usages and availability of physical and swap memory. If there is no swap space allotted in the system it will show 0B in the memory record column:

$ free -h

How to check Available Disk Storage in the System ?

As the swap file takes space from the system storage we need to make sure there is enough storage in your hard drive. If there is not then you should upgrade the physical memory rather than maintaining swap space. 

Since swap space is part of the hard drive its read and write speed is slower as compared to physical memory. Use the following command to view the record of device storage or hard drive.

$ df -h

How to create a Swap File in the System ?

Swap files are files that use disk space to maintain swap space in the system. We will create the swap file using the fallocate command along with the -l flag which is used to determine file size in bytes. The fallocate command will create the file with a preallocated file with the specific file size mentioned in the command. 

Allocating swap file size is based on personal preferences or need but it is recommended to make swap file size double the physical memory if the physical memory is less than 2 GB.

$ sudo fallocate -l 2G /pagefile

Here, we can see that the swap file is created at base dir (/) with pagefile name with the initial size of 2 GB.

Now change the permission of the pagefile so it is only accessible by the only root user. For that execute:

$ sudo chmod 600 /pagefile

How to Mark and Enable Swap Space ?

After creating and changing the permission of the pagefile we need to set the file as swap space using the mkswap command:

$ sudo mkswap /pagefile

Then, enable it using the swapon command which will be used by our system:

$ sudo swapon /pagefile

How to Set Swap File as Permanent ?

Till now we have set the swap space for the current session if we restart the system the instance of swap space will be gone. For setting the swap file permanent simply execute the following command. 

In the command, we print the swap configuration then piping the stander output with the tee command which will append the output to fstab or you can simply open the fstab file and copy-paste the configuration:

$ echo '/pagefile none swap sw 0 0' | sudo tee -a /etc/fstab

Now check if swap space has been allotted or not.

How to Manage Swappiness Value ?

Swappiness is a Linux kernel feature that has a value from 0 to 100 which determines how often or how much the swap space is used by the system. By default, ubuntu determines the swappiness value by 60. 

As the value of swappiness increases, the more aggressive system begins to use the swap space. To check the swappiness value execute the following command:

$ cat /proc/sys/vm/swappiness

For a local Linux system having a swappiness value might be fine but in the case of a server, its value must be as small as possible. 

To change the swappiness value temporary run :

$ sudo sysctl vm.swappiness=8

To persist the swappiness value on the next reboot we add the following line in the /etc/sysctl.conf file.


Although, the swappiness value depends on the system's workload and amount of physical memory use.

[Need urgent assistance in installing Software on any Linux Distribution? We can help you. ]


This article covers how we can add and maintain swap space in the ubuntu system. Swap space can take the form of either a dedicated swap partition or a swap file. Typically, when running Ubuntu on a virtual machine, a swap partition is not present, and the only option is to create a swap file.

To Adjust 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 to try to avoid swapping whenever possible, while a higher value will make the kernel to 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

To Remove a Swap File:

1. First, deactivate the swap space:

$ sudo swapoff -v /swapfile

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

3. Finally, remove the actual swapfile file using the rm command:

$ sudo rm /swapfile