How to Add Swap Space on Ubuntu 20.04
We need Swap Space in Linux. When our RAM is full, Swap space does the role of the smooth operation of our system. It takes inactive pages from the RAM.
In this article, we are going to discuss the methods of adding swap space on Ubuntu 20.04 LTS.
Tutorial To Add Swap Space On Ubuntu 20.04
This tutorial explains how to add a swap file on Ubuntu 20.04 LTS.
Note: If your system constantly runs out of memory, you should add more RAM, not the Swap Space.
At first, we need to create a swap file.
sudo fallocate -l 2G /swapfile
Use the following command if you don’t have fallocate utility in your Ubuntu 20.04 LTS to create Swap space.
sudo dd if=/dev/zero of=/swapfile bs=1024 count=2097152
We are setting permission right to 600.
sudo chmod 600 /swapfile
Create a Linux swap area on the file:
sudo mkswap /swapfile
Run the following command to activate the swap file.
sudo swapon /swapfile
Verify that the swap is active.
sudo swapon --show
Or,
sudo free -h
Swappiness Value Setting In Ubuntu 20.04 LTS
Swappiness is a method that defines how often your Linux system will use the swap space. On Ubuntu, the default swappiness value is set to 60
. You can check the current value with the following command.
cat /proc/sys/vm/swappiness
Note: It can have a value between 0 and 100. A lower value is better as it will make the kernel try to avoid swapping whenever possible and at the same when you have a higher value, the kernel will try to use Swap Space like never before.
Meanwhile, if you want to set the swappiness value, then you need to open the /etc/sysctl.conf
file and add the following line.
vm.swappiness=20
Run the following command to delete or deactivate the Swap value in Ubuntu 20.04 LTS.
sudo swapoff -v /swapfile
Now, remove the swap file entry /swapfile swap swap defaults 0 0
from the /etc/fstab
file.
remove the actual swapfile file with the following command:
sudo rm /swapfile
How To Define Swap Space In Ubuntu 20.04
- Swap space 3 times of the RAM if your system has less than 2 GB RAM
- Swap space same size of the RAM if your system has systems has 2 to 8 GB RAM
- Systems with more than 8 GB RAM then your Swap space will be at least 4 GB of Swap