Pages

How to increase swap size in Ubuntu? (works for all Ubuntu Flavors)

Here we will see how we can increase the SwapSize  in Ubuntu!

  1. Open Terminal
  2. Enter "sudo swapon --show"
    1. This is show you the details of current swap size
  3. Enter "df -h"
    1. This is to see how much disk space you have.
  4. Now Turn the swap off using "sudo swapoff -a"
    1. This will allow us to write to swap file without being interrupted by File in Use error
  5. This is the important part and requires some knowledge if you want to understand what is going on under the hood. But high level understanding here is as follows:
    1. If have xyz GB of ram, your swap file size will be XYZ GB + 2GB at the minimum. In my case, I had 16GB of Ram on Laptop, but current swap file was of 2GB only. When created automatically using the command that follows, it made 27GB in SwapFile.
    2. Enter "sudo dd if=/dev/zero of=/swapfile bs=5M count=5120 status=progress"
    3. Wait for the entire process to end.
  6. Now lets give this file root only permissions: "sudo chmod 600 /swapfile"
  7. Now mark this file as swap space file: "sudo mkswap /swapfile"
  8.  Now make this file permanent so that restart of system doesn't revert it to previous state: 
    1. "echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab"
  9. Now turn the swap back on using "sudo swapon /swapfile"
  10. Use HTOP to see change in swap size.