How to create a swapfile in Lubuntu 20.04 & later

How to create a swapfile in Lubuntu 20.04 and 20.10

1.Check to see if the system has any swap configured :

$ swapon --show

If you receive no output this means the system has no swapfile available

To verify this run the following command:

$ free -h

  1. In the case that a previous swapfile is on your system you need to run the following 2 commands - otherwise continue on to step 3

a. To turn off swap:

$ sudo swapoff -a 

b. Then remove the swapfile:

$ sudo rm -i  /swapfile
  1. In this example we create a 8GB swapfile (in general double your RAM -please note that I am using 8 GB only as an example)

    $ sudo dd if=/dev/zero of=/swapfile bs=1M count=8192

Set file protections:

   $ sudo chmod 600 /swapfile

Verify the permissions:

   $ ls -lh /swapfile


We can see that only root user has the read and write flags enabled.

Mark the file as swap space by typing:

  $ sudo mkswap /swapfile

We then enable the swap file:

   $ sudo swapon /swapfile

Verify that the swap is available and confirm 4 GB RAM and 8 GB swap by typing:

   $ free -h

  1. Make the swap file permanent
    Back up the /etc/fstab file in case thing goes wrong:

    $ sudo cp -pv /etc/fstab /etc/fstab.bak
  2. Edit /etc/fstab (use your favorite text editor; example uses vim which isn’t for everyone)

    $ sudo vim /etc/fstab

Add this line in /etc/fstab and confirm that there are no other “swap” lines

/swapfile none swap sw 0 0

  1. Reboot the system and verify the operation

$ free -h