Change swap size in Ubuntu 18.04 or newer

[Updated July 26, 2020]: Change swapfile permission; Set swapfile in /etc/fstab.

Swap is a special area on your computer, which the operating system can use as additional RAM.
Starting with Ubuntu 17.04, the swap partition was replaced by a swap file. The main advantage of the swap file is easy resizing.

In the following example, we’ll extend the swap space available in the /swapfile from 4 GB to 8 GB.

    1. Turn off all swap processes
      sudo swapoff -a
    2. Resize the swap
      sudo dd if=/dev/zero of=/swapfile bs=1G count=8

      if = input file
      of = output file
      bs = block size
      count = multiplier of blocks

    3. Change permission
      sudo chmod 600 /swapfile
    4. Make the file usable as swap
      sudo mkswap /swapfile
    5. Activate the swap file
      sudo swapon /swapfile
    6. Edit /etc/fstab and add the new swapfile if it isn’t already there
      /swapfile none swap sw 0 0
    7. Check the amount of swap available
      grep SwapTotal /proc/meminfo


  1. Hi Bogdan

    Thanks for this. I created the swapfile successfully with 20GB but I notice there are now two swapfiles – the 20GB one and the original one created by the OS (4GB). From the fstab file I can see two entries. The second on was entered by myself. Would this cause any issues? Thanks.

    /dev/mapper/cryptswap none swap defaults 0 0
    /swapfile none swap sw 0 0

    When I run sudo swapon -s, it shows the /swapfile type as ‘file’ and the ‘/dev/dm-0’ type as ‘partition’.


    1. Hi Edwin,

      From what I can see, the OS created an encrypted swap partition. This is most likely because you choose to encrypt your Ubuntu installation. In this case, you can use a partition tool such as GParted or Disks (from Gnome) to resize the swap partition.
      I haven’t verified this, but I don’t think the swap file you created is being used if you also have the encrypted one.

    1. Hi Meet,
      Steps 1, 4, 5, 6 and 7 are also applicable to a swap partition.
      You will need a partition editor program such as GParted to create the swap partition.
      When adding the partition to fstab, you’ll need to add it by UUID (instead of just using “/swap”) which you can find out by running the “blkid” command.

  2. In step 2, I got the following error:
    DD: Memory Exhausted by Input Buffer of Size Bytes

    This can be fixed by using this command instead:
    sudo dd if=/dev/zero of=/swapfile bs=1M count=2000
    (2000 = 2GB)

    1. Hi Enrique,

      Can you please add this line at the end of the /etc/fstab file?
      /swapfile none swap sw 0 0

      Please let me know if this works for you and I’ll update the article.

      Thank you,

Leave a Comment

Your email address will not be published. Required fields are marked *

20 + 10 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.