69

I have laptop with 8 GB RAM and 1TB HDD. I have swapfile which is 2 GB (Ubuntu 18.04 uses a swapfile instead of a separate swap partition by default) and I want to increase it to use hibernation.

I want to increase it from 2 GB to 16 GB. Here is a screenshot of GParted:

GParted screenshot

I tried to increase it with fallocate -l 16G but it did not work.

Also there is the image from free -m:

<code>free</code> output

8
  • This is for swap file, I want to increase swap partition. I don't think that this thread is duplicating with other's.
    – Miroslav
    Sep 15, 2018 at 11:54
  • From the screenshot you posted, there is no swap partition. I guess you already have a swapfile. AFAIK Ubuntu 18.04 does create swapfile by default and no swap partition.
    – Thomas
    Sep 15, 2018 at 12:01
  • 2
    Oh yes, actually from swapon --show it it indeed /swapfile. I will try this guide.
    – Miroslav
    Sep 15, 2018 at 12:04
  • If you have 8GB of RAM you only need 8GiB for swap. But why do you want hibernation. Ubuntu boots very fast without hibernation.
    – oldfred
    Sep 15, 2018 at 15:01
  • 1
    Could you please post text files, dialogue messages, and program output listings as text, not as images? To achieve the latter two you can either 1) select, copy & paste the dialogue text or terminal content or 2) save the program output to a file and use that. Longer listings (the editor will tell you what’s too long) should be uploaded to a pastie service and linked to in the question. Thanks. Sep 28, 2018 at 8:24

2 Answers 2

160

From Ubuntu 18.04 onwards, a swapfile rather than a dedicated swap partition is used (except when LVM is used). The swap file is named swapfile. To change the size of this swap file:

  1. Disable the swap file and delete it (not really needed as you will overwrite it)

    sudo swapoff /swapfile
    sudo rm  /swapfile
    
  2. Create a new swap file of the desired size. With thanks to user Hackinet, you can create a 4 GB swap file with the command

    sudo fallocate -l 4G /swapfile
    

    In this command, adjust 4G to the size you want.

    Alternatively, you can use dd but feeding the correct parameters requires some calculations. If you want to make a 4 GB swap file, you will need to write 4 * 1024 blocks of 10242 bytes (= 1 MiB). That will make your count equal to 4 * 1024 = 4096. Create the file of this size with the command

    sudo dd if=/dev/zero of=/swapfile bs=1M count=4096
    
  3. Assign it read/write permissions for root only (not strictly needed, but it tightens security)

    sudo chmod 600 /swapfile
    
  4. Format the file as swap:

    sudo mkswap /swapfile
    
  5. The file will be activated on the next reboot. If you want to activate it for the current session:

    sudo swapon /swapfile
    

You can check the swap that is available with the command swapon -s (no root permissions needed).

10
  • 2
    this works like a charm, but unfortunately it does not survive reboots on my setup with an encrypted partition: nvme0n1p3_crypt -> ubuntu--vg-root / ubuntu--vg-swap_1 any idea on how to persist this?
    – rypel
    Sep 3, 2019 at 8:13
  • 1
    Is bs=1M the fastest block size? what happens if I change that? Oct 25, 2019 at 4:18
  • 2
    You need to add text to your answer that includes the mods required to /etc/fstab.
    – heynnema
    Nov 11, 2019 at 20:35
  • 1
    "From Ubuntu 18.04 onwards, a swapfile rather than a dedicated swap partition is used." - that doesn't seem to be the case any more, as seen in this question, plus my own experience with installing Ubuntu 18.04.3 last week. Though where exactly the swap partition is, beats me. Dec 3, 2019 at 17:36
  • 1
    In step2 to skip all the calculation, you can do sudo fallocate -l 4G /swapfile. Just change the value for 4G to whatever size you want, 1G, 2G, 3G... etc.
    – Hackinet
    Feb 24, 2021 at 7:58
6

From the man mkswap it is recommended to use the dd command as demonstrated in @vanadium post.

If  you  don't  know  the  page  size  that  your  machine uses, 
you may be able to look it up with 
"cat /proc/cpuinfo" 
(or you may not – the contents of this file depend on architecture and kernel version).

   To set up a swap file, it is necessary to create that file before   
   initializing  it  with  mkswap,  e.g. using a command like

          # fallocate --length 8GiB swapfile

   Note  that  a  swap  file must not contain any holes.  Using cp(1) to  
   create the file is not acceptable.
   Neither is use of fallocate(1) on file systems that support preallocated 
   files, such as XFS or ext4,  or on  copy-on-write  filesystems like btrfs.  

   It is recommended to use dd(1) and /dev/zero in these cases.
   Please read notes from swapon(8) before adding a swap file to copy-on- 
   write filesystems.

And here the notes of man swapon

NOTES
       You should not use swapon on a file with holes.  This can be seen in
       the system log as

              swapon: swapfile has holes.

       The swap file implementation in the kernel expects to be able to write  
       to the file directly, without the assistance  of the filesystem.  This 
       is a problem on preallocated files (e.g.  fallocate(1)) on filesys‐
       tems like XFS or ext4, and on copy-on-write filesystems like btrfs.

       It is recommended to use dd(1) and /dev/zero to avoid holes on XFS
       and ext4.

       swapon may not work correctly when using a swap file with some  
       versions of btrfs.  This is due to  btrfs being  a copy-on-write 
       filesystem: the file location may not be static and corruption can 
       result.  
       Btrfs actively disallows the use of swap files on its filesystems
       by refusing to map the file.

       One possible workaround is to map the swap file to a loopback device.  
       This will allow the filesystem to determine the mapping properly but  
       may come with a performance impact.

       Swap over NFS may not work.

Not the answer you're looking for? Browse other questions tagged .