Table of Contents

Ubuntu - Swap - Increase Swap Size

Recent releases of Ubuntu use swap file instead of the traditional swap partition.

IMPORTANT:

  • If you are using a swap partition and want to increase the swap size, you can still go ahead and create a swap file.
  • Linux can use multiple swap spaces as needed; and this way, there is no need to touch the partition.

Check if there is a swap file in the system

swapon --show

returns:

NAME      TYPE SIZE USED PRIO
/swapfile file   4G 2.1G   -2

NOTE: This shows that there is a swapfile.

  • TYPE: If this shows file, it indicates that a swap file is being used.

Take the swap file offline

Before resizing the swap file, turn the swap off.

sudo swapoff /swapfile

NOTE: The command does not produce any output and it may take a few minutes to complete.

Make sure that there is enough free RAM available to take the data from swap file.

  • Otherwise, create a temporary swap file.

Resize the swap file

sudo fallocate -l 4G /swapfile

Mark this file as a swap file

sudo mkswap /swapfile

NOTE: There should be some output where it warns you that the old swap signature is being wiped.


Enable the swap file

sudo swapon /swapfile

NOTE: All done!

  • The new swap file is created with the increased swap size.

Check the swap size

swapon --show

free -h

returns:

               total        used        free      shared  buff/cache   available
Mem:            62Gi        10Gi       886Mi       364Mi        50Gi        50Gi
Swap:          4.0Gi       2.1Gi       1.9Gi