Linux tutorials

How To Add swap in Ubuntu 20.04 or Ubuntu 22.04

Step 1 – Checking the System for Swap Information

Before we begin, we can check if the system already has some swap space available. It is possible to have multiple swap-files or swap partitions, but generally one should be enough.

We can see if the system has any configured swap by typing:

sudo swapon

Step2  –  Creating a Swap File


In Our example we create 4G for /swap.img

sudo fallocate -l  /swap.img

Step 3 – Enabling the Swap File

First we give correct permissions for swap



sudo chmod  /swap.img

First we format the

sudo mkswap /swap.img

Now we turn the swap on

sudo swapon /swap.img

Step 4 – Let´s make permanent changes to our swap-file

Making a backup of /etc/fstab file

sudo cp /etc/fstab /etc/fstab.bak

Adding our swap.img to fstab

echo '/swap.img none swap sw 0 0' sudo tee -a /etc/fstab'

Leave a Reply

Your email address will not be published. Required fields are marked *