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

32 Comments

  1. Well, I answered my own question. I left entries for both the swap file and the swap partition in the fstab. When I restarted, both showed up as active, when I ran swapon -s. I’m not sure if having them both running concurrently could cause problems, so I commented out the entry for the swap partition and restarted. The box came back up with only the swap file running. That little 90mb swap partition can just sit there and gather dust. It doesn’t appear to be hurting anyone.

  2. Hello. I recently moved onto an Ubuntu server v18.04 box, at a large webhost. The box was provisioned with a 90mb swap partition, which is really small and caused problems with a backup app. I walked through all of your commands and my server is now running a swap file, apparently bypassing the swap partition. The following lines are in the fstab:

    # swap was on /dev/sda6 during installation
    UUID=25678faa-bdb3-4a22-b3ae-e6ef02d200f2 none swap sw 0 0

    I added the swapfile line under it, but what should I do with the swap partition entry? Should I delete it or just let it initialize and sit dormant? Will running a swapfile in this situation cause problems?

    I really appreciate your information on resizing the swap space, BTW. It was well written and easy to follow.

  3. Thank you for this. I’d been wanting to increase the swapfile size for a while, but every instruction set I found seemed to technical. Your instructions were clear, concise and did exactly what I needed. So thank you. šŸ™‚

  4. My fstab on ubuntu 20.04 has these entries – did not change any, do I still need to change ?

    I did follow all steps except any fstab change and this shows proper 8gb
    grep SwapTotal /proc/meminfo
    SwapTotal: 8388604 kB

    # /etc/fstab: static file system information.
    #
    # Use ‘blkid’ to print the universally unique identifier for a
    # device; this may be used with UUID= as a more robust way to name devices
    # that works even if disks are added and removed. See fstab(5).
    #
    #
    # / was on /dev/sdb3 during installation
    UUID=0f2d2288-e7f1-4742-b53a-e466a50e91e7 / ext4 errors=remount-ro 0 1
    # /home was on /dev/sdb4 during installation
    UUID=b0651f1d-4dfd-46ff-9afd-964277a02e04 /home ext4 defaults 0 2
    # swap was on /dev/sdb2 during installation
    UUID=2f49d3f7-8691-40d8-b7a0-eaf71de5abf4 none swap sw 0 0

    1. Hi Ionut,
      I don’t think you need to change anything.
      But you can check what type of swap is active (file or partition) with the “swapon -s” command. “-s” meaning summary.
      This is what I get when I run this command on a machine with 4 GB of swap:
      $ swapon -s
      Filename Type Size Used Priority
      /swap/file file 4194304 0 -2

    1. $ sudo vim /etc/fstab
      (you can use any editor instead of vim)
      and then paste this line
      /swapfile none swap sw 0 0

  5. I got issue in step 4, after running: sudo swapon /swapfile
    I got: swapon: /swapfile: read swap header failed
    Any suggestion?

  6. Thank you Bogdan beautiful instructions. It worked out
    (only part confusied me is Edit /etc/fstab and add the new swapfile if it isnā€™t already there /swapfile none swap sw 0 0 I skipped it)

    Thanx

  7. Hi Bogdan!
    Thank you for the insights, it worked fine. However, now
    $sudo hibernate
    stopped working . No error msg, I just get back to my desktop.
    Would you have any idea how to resolve that? I’m using Ubuntu 20.04
    Thank you!

    1. Hi Daniel,
      I would look for more information into the system logs.
      If you’re using Gnome, you can open the “Logs” program and search there for anything related to “hibernate”, “sleep” or “acpi”.
      You could also try the command line way: sudo vim /var/log/dmesg or sudo vim/var/log/syslog

  8. 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’.

    Edwin

    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.

  9. Thank you for your clear explanation, I could resolve few issues in my OS by extending Swap memory.

  10. 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,
      Bogdan

Leave a Comment

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

fourteen − twelve =

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