How to Add Swap Memory in Ubuntu 20.04

May 20, 2025 / Servers, Hosting & Email

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:

  1. Check Existing Swap Space
    1. Open a terminal and run:
      sudo swapon –show

      If nothing is returned, there is no swap currently active.

  2. Create a Swap File
    1. Use fallocate to create a 2GB swap file (you can adjust the size):
      sudo fallocate -l 2G /swapfile
    2. If fallocate fails, use:
      sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
  3. Set Correct Permissions
    Set permissions to keep the file secure:

    sudo chmod 600 /swapfile
  4. Mark as Swap Space
    Format the file to swap:

    sudo mkswap /swapfile
  5. Enable the Swap File
    1. Turn on the swap:
      sudo swapon /swapfile
    2. Check if it’s working:
      sudo swapon –show
  6. 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
  7. (Optional) Adjust Swappiness Value
    1. Swappiness controls how often Linux uses swap. The default is 60. To reduce swapping, set to 10:
      sudo sysctl vm.swappiness=10
    2. To make it permanent, add to /etc/sysctl.conf:
      echo ‘vm.swappiness=10’ | sudo tee -a /etc/sysctl.conf
  8. To Remove Swap Later (Optional)
    1. Turn off swap:
      sudo swapoff /swapfile
    2. Remove the swap file:
      sudo rm /swapfile
    3. Remove the entry from /etc/fstab.

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

Spread the love