Change swap size in Ubuntu 18.04 or newer

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. Make the file usable as swap
      sudo mkswap /swapfile
    4. Activate the swap file
      sudo swapon /swapfile
    5. Check the amount of swap available
      grep SwapTotal /proc/meminfo


  1. 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 *

4 × four =

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