Enable Swap Memory on Ubuntu on Raspberry Pi

Rayan Fernandes
2 min readDec 15, 2021

--

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.

Specifications

  • Raspberry Pi 3 Model B+
  • Ubuntu 18.04.5 LTS Server

Check if swap is set

$ 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

Swap file creation

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

Change permissions so that only the root user can read and write

$ sudo chmod 600 /swapfile

Create swap space in a file using mkswap

$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=44dd3594-5812-473e-839d-d9c7a1af3d6a

Enable swap

$ sudo swapon /swapfile

Check status of swap

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

Open task manager (htop)

$ htop

Add to fstab to automount at boot time

$ echo '/swapfile swap swap defaults 0 0' | sudo tee -a /etc/fstab
/swapfile swap swap defaults 0 0

Reboot and check

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

Referenced site

--

--