In this article, we have explained how to add swap memory in Ubuntu 20.04. Swap space is used in Linux systems as an overflow when the physical RAM is fully utilised. It helps improve system performance and stability, especially on systems with limited RAM.
Follow the guide:
- Check Existing Swap Space
- Open a terminal and run:
sudo swapon –show
If nothing is returned, there is no swap currently active.
- Open a terminal and run:
- Create a Swap File
- Use fallocate to create a 2GB swap file (you can adjust the size):
sudo fallocate -l 2G /swapfile
- If fallocate fails, use:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
- Use fallocate to create a 2GB swap file (you can adjust the size):
- Set Correct Permissions
Set permissions to keep the file secure:sudo chmod 600 /swapfile
- Mark as Swap Space
Format the file to swap:sudo mkswap /swapfile
- Enable the Swap File
- Turn on the swap:
sudo swapon /swapfile
- Check if it’s working:
sudo swapon –show
- Turn on the swap:
- Make Swap Permanent
To enable the swap file on every boot, add it to /etc/fstab: echo ‘/swapfile none swap sw 0 0’ | sudo tee -a /etc/fstab
- (Optional) Adjust Swappiness Value
- Swappiness controls how often Linux uses swap. The default is 60. To reduce swapping, set to 10:
sudo sysctl vm.swappiness=10
- To make it permanent, add to /etc/sysctl.conf:
echo ‘vm.swappiness=10’ | sudo tee -a /etc/sysctl.conf
- Swappiness controls how often Linux uses swap. The default is 60. To reduce swapping, set to 10:
- To Remove Swap Later (Optional)
- Turn off swap:
sudo swapoff /swapfile
- Remove the swap file:
sudo rm /swapfile
- Remove the entry from /etc/fstab.
- Turn off swap:
In such a manner, you can add Swap Memory in Ubuntu 20.04. If you need help, do contact our support specialists.
Need to configure local domain mapping? Check How to update hosts file entry on Windows, Ubuntu and Mac