Swap is a reserved space on a hard drive that is used when the physical RAM memory becomes full. When there is no longer sufficient space in RAM to hold in-use application data then Swap space will be used. This tutorial explains how to add a swap space on Ubuntu 20.04 Focal Fossa.
The information written to disk will be significantly slower than information kept in RAM. Overall, having a swap space can be good when your system’s RAM is depleted and throw out-of-memory exceptions on systems.
Make sure you are logged in as root or user with sudo privileges.
Step 1 – Checking Swap Information
Before you start, Check if the system already has some swap space available. You can check it by running below command:
sudo swapon --show
If the output is empty, it means your system don’t have swap space.
Step 2 – Creating a Swap File
In this tutorial, we will create
1 GB swap file. You can replace
1G with the size of the swap space you need.
Create a file which will be used as swap:
sudo fallocate -l 1G /swapfile
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:
We can verify that the correct amount of space was reserved by typing:
ls -lh /swapfile
-rw-r--r-- 1 root root 1.0G Apr 25 11:14 /swapfile
Now our file has been created with the correct amount of space set aside.
Step 3 – Set Permission
We need to lock down the permissions of the file so that prevent regular users to write and read the file:
sudo chmod 600 /swapfile
Step 4 – Enabling the Swap File
Create a Linux swap area on the file:
sudo mkswap /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes) no label, UUID=4e665809-2gb3-370f-aed6-5627e74089dbf
After that, enable the swap file, allowing our system to start using it:
sudo swapon /swapfile
Step 5 – Make Swap File Permanent
Our previous changes have enabled the swap file for the current session only. To make the change permanent edit the
sudo nano /etc/fstab
Append the swap file information at the end of your
/swapfile swap swap defaults 0 0
Step 6 – Tuning your Swap Settings
At this point, we will make few configuration which will have an impact on your system’s performance while dealing with swap.
Adjusting the Swappiness Value
The swappiness parameter defines how often your system swaps data out of RAM to the swap space. It can have a percentage value between
A higher value will make the kernel to try to put more data in to swap aggressively, while a low value will make the kernel to try to avoid swapping whenever possible.
You can check the current swappiness value by typing:
By default, Ubuntu system have
swappiness value is
60. It’s OK for desktop user, for a production server, it might be lower.
We can change this swappiness value using
sysctl command. For instance, to set the
swappiness value to
sudo sysctl vm.swappiness=15
vm.swappiness = 15
This change will persistent until next reboot. To make this change permanent append the following line to the
sudo nano /etc/sysctl.conf
Save and close the file.
Removing a Swap File
Perform the following steps to deactivate and remove swap file.
First, deactivate the swap space:
sudo swapoff -v /swapfile
Next, remove the swap file entry
/swapfile swap swap defaults 0 0 from the /
At last, delete the physical
swapfile file using the
sudo rm /swapfile
This tutorial shown you how to create a swap file and configure swap space on your Ubuntu 20.04 system.
If you have any problem or feedback, please leave a comment below.