How to add swap space to Ubuntu Linux Server to prevent it from Freezing

leangaurav
3 min readMar 24, 2024

If you don’t want to read the complete thing and just get this done quick. Then just copy paste the below lines... but before that replace 1G in second command with however much space you like (probably consider hitting 👏 if it helped you)

sudo swapon --show                  
sudo fallocate -l 1G /swapfile # allocate 1G of swap space
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon --show
sudo echo '/swapfile swap swap defaults 0 0' | sudo tee -a /etc/fstab

When do I add Swap Space ?

Whenever I’m working on a hobby project and I want to setup a new Linux server with Ubuntu OS, I do this as the first step.
I mostly use small instances/VMs in free tier 😝 from AWS or OCI or some other cloud vendor. These VMs have limited amount of RAM and often freeze due to out of memory issues when I run automated github actions or something which puts pressure on memory all of a sudden.

Adding swap right after creating server has helped me avoid frozen instances and sometimes even deleting the instance and setting up a new from sacratch again because the server froze and couldn’t come up even on reboot.

Step-1: Check some stuff

Check if swap is enabled
Below output is after I have already added swap. If there’s no swap, output will be empty.

sudo swapon --show                  # show current swap details

NAME TYPE SIZE USED PRIO
/swapfile file 2G 25.5M -2

Check available Memory
️❗Take a note of available memory (RAM) from here.
Shows 949Mi (~1G) for me (Ignore the Swap bit).

free -h
total used free shared buff/cache available
Mem: 949Mi 263Mi 95Mi 0.0Ki 590Mi 518Mi
Swap: 2.0Gi 25Mi 2.0Gi

Check available disk space
Just see how much free space you have on disk to use for swap. Looks like I have enough 🆓disk space

df -h

Filesystem Size Used Avail Use% Mounted on
/dev/root 20G 6.7G 13G 35% /
tmpfs 475M 0 475M 0% /dev/shm
tmpfs 190M 972K 189M 1% /run
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/xvda15 105M 6.1M 99M 6% /boot/efi
tmpfs 95M 4.0K 95M 1% /run/user/1000

Step-2: Allocate Swap File

Usually I keep allocate swap space which is 1–2x the memory available on system. But it could be higher also.
Since I have ~1G RAM, I’m allocating swapfile of size 1G. Change to whatever you like

sudo fallocate -l 1G /swapfile      # allocate 1G of swap space

Step-3: Secure swapfile by changing

sudo chmod 600 /swapfile

Step-4: Convert swap file to swap area

sudo mkswap /swapfile

Step-5: Enable swap

sudo swapon /swapfile

Step-6: Make sure swap stays even after reboot

To make changes we did till now permanent, either
paste this /swapfile swap swap defaults 0 0 at end of this file /etc/fstab

OR, just run this 🙌🏻

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

Thats it done 🙌

Now if you wish to check stuff you can run either this free -h or this swapon --show command.

Note-1: If you ever wish to disable swap, you need to do this

  1. Turn off swap sudo swapoff -v /swapfile
  2. Open the file /etc/fstab and delete this entry /swapfile swap swap defaults 0 0
  3. Delete the swap file sudo rm /swapfile

I also keep all above steps handy in my guides repo on github.

Note-2: There are two ways of adding swap space

  1. Swap file
  2. Swap partition

We have used Swap file here. I’ve never used the other one since this option was just enough for me always. If you wish, feel free to explore the other one.

Hope this helped you 😀… consider hitting 👏
You can find me on Linkedin and say 👋
Want to buy me a tea ☕ ❓

--

--

leangaurav

Engineer | Trainer | writes about Practical Software Engineering | Find me on linkedin.com/in/leangaurav | Discuss anything topmate.io/leangaurav