How to create a swapfile in Lubuntu 20.04 and 20.10

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

    screen5

    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

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

    $ sudo cp -pv /etc/fstab /etc/fstab.bak

  3. Edit /etc/fstab

    $ sudo vim /etc/fstab (or your favorite text editor)

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

3 Likes

Nice writeup, thanks.
But why would you need a swapfile in the first place? Just for my understanding. RAM is cheap nowadays.

I’ve seen many Lubuntu users reporting issues with the system being slow, the issue being fixed by the addition of swapfile.

Some are using old laptops which have a 2GB or other low maximum capacity, for old laptops it’s often hard to get suitable RAM anyway, esp. if the owners is on limited income where one has more immediate calls on their limited funds.

2 Likes

One small suggestion: use stat instead of ls to check permissions. By default it will give permissions both in octal and symbolic (i.e. rwx) but if you want to simply check octal you can use stat -c '%a'.

1 Like

FWIW, I’m running two old laptops with 20.04, both from ~2008. One with 2 GB RAM, the other with 4 GB RAM.
Both are blazingly fast (especially compared to my third, 3 year old Win10 machine). None of them have a swapfile, and I’ve never had any RAM-full problems with them.
Now, I can imagine that using ,eg, Firefox with 20+ tabs open can create problems, but in that case I’d perhaps reflect on my surfing habits.

2 GB of 667 MHz SO-DIMM costs 10…12 Euro today. I appreciate that some users are on a small budget, but still…

PS: my 4 GB ancient laptop has a 6 GB swap partition, but that’s because I need Hibernate on that one.

2 Likes

A post was split to a new topic: How to create a swapfile in Lubuntu 20.04 & 20.10