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.

Note: before running the following commands, please make sure you have a backup of your data!

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, I already had a swapfile but reduced the size with your method. The last two lines of the fstab file read

    /dev/mapper/vgubuntu-swap_1 none swap sw 0 0
    /swapfile swap swap defaults 0 0

    Is this fine, or should I change this?

  2. Hey dear
    I was facing a freezing problem and i started follow this process but the freezing problem can’t allow me even type this commands how can i solve this?
    Can someone please help me what should i do

  3. Thank you ..Bogdan…..
    besides the GREAT step by step!
    That is exactly what I wanted to know to dual boot with Ubuntu and that other “thingy” that came with my laptop, (for warranty purposes only! LOL!)

    I have 12GB Ram, and so I’d use 24GB swap partition, so I can RUN “hibernation?
    As I LOVE not using ANY battery as “sleep” might do!

    So the “swap file” only POINTS to the “swap partition” and is NOT actual “allocated space???” I am a nooby, as you can TELL! LOL!

    1. Yes! :), Exxcccatly according to this table! 🙂

      RAM(GB) No hibernation With Hibernation Maximum
      1 1 2 2
      2 1 3 4
      3 2 5 6
      4 2 6 8
      5 2 7 10
      6 2 8 12
      8 3 11 16
      12 3 15 24
      16 4 20 32
      24 5 29 48
      32 6 38 64
      64 8 72 128
      128 11 139 256

  4. Thank you for the detailed instructions with explanation. This article solved my problem very quickly.

  5. Bogdan, that’s an excellent article and thanks.

    A minor feedback: the default swap file name in Ubuntu 18.04 (Server) is /swap.img . Maybe it is helpful for readers if you describe how to see that by using cat /etc/fstab (or grep ”swap” /etc/fstab). In this case readers can skip Step 3 (to change the swap file permission) & 6 (to update the fstab file).

    1. Hi Culip,
      Thank you for your kind words and your feedback.
      I don’t have an Ubuntu Server 18.04 installation available to check the default swap file name and I couldn’t find any reference in the official Ubuntu Server 18.04 docs
      Skipping steps #3 and #6 if the file already exists is a great idea and I’ll update the article.

  6. 2 comments?
    1.-if we stop swap and ,we do the file with dd, the server has lag
    do you recomend make the swapfile first?

    2.-after reboot the server the swap is 0% used, and all is setings is correct an my server show the correct new /swapfile as the new swap area. but is not used, fstab is correcto an the proc show te correct swapfile

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

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

  9. 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. 🙂

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

  11. anish@anish:~$ /swapfile none swap sw 0 0
    bash: /swapfile: Permission denied

    swap is created but why show is permission denied when type /swapfile non swap sw 0 0 ?

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

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

      1. yes i got
        .. First i resized my swap partion and the root also
        Then i already update all UUID from the (blkid) and paste them in that (/etc/fstab)
        then restart
        and the laptop still slow
        and run swapon -s
        and same bad result with zero used and 6gb size free!!!!!

  13. 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)


  14. 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

  15. 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.

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

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

seventeen − sixteen =

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