Enable Swap Memory on Ubuntu on Raspberry Pi

Swap memory will extend RAM capacity by occupying some space on your SD-card, It will only be used when there isn’t enough room for the applications to store its processes, and also prevents applications from crashing when it requires more ram during the runtime.

  • Raspberry Pi 3 Model B+
  • Ubuntu 18.04.5 LTS Server
$ sudo swapon --show

If swap is not enabled it will show nothing

Otherwise it will be displayed like this.

$ sudo swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 1024M 0B -2

Procedure

I will make 1GB for the time being. Well, I’m not so pressed for it, and the physical memory is 1GB, so it’s about the same.

$ sudo fallocate -l 1G /swapfile
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=44dd3594-5812-473e-839d-d9c7a1af3d6a
$ sudo swapon /swapfile
$ sudo swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 1024M 0B -2
$ htop
$ echo '/swapfile swap swap defaults 0 0' | sudo tee -a /etc/fstab
/swapfile swap swap defaults 0 0
$ sudo reboot$ sudo swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 1024M 0B -2

Full Stack React and Python Developer