How do I increase swapfile in Ubuntu 18.04?

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
  1. Assign it read/write permissions for root only (not strictly needed, but it tightens security)

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

    sudo mkswap /swapfile
    
  3. 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).


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.

Tags:

Hibernate

Swap