This article will guide you on how to add Swap partitions manually under Debian 11 and Ubuntu 22.04.

Preparation

First, check that your system already has a Swap partition.

1
swapon -s

or

1
free -m

If no result is returned or the Swap column in free -m has a value of 0, then your system does not have a swap partition.

Creating a SWAP partition

We can use the fallocate command to create a 1GB swap partition.

1
fallocate -l 1G /swapfile

If this command does not work, please install the util-linux package.

1
apt install util-linux

Then set the permissions for this file.

1
chmod 600 /swapfile

Then activate the SWAP partition.

1
2
mkswap /swapfile
swapon /swapfile

At this point you can use the command swapon -s or free -m to see if the Swap partition is active.

Setting up boot-up

We need to edit the file /etc/fstab and add the following.

1
echo "/swapfile swap swap defaults 0 0" >> /etc/fstab

Once you have finished, use the command free -m to see if the Swap partition is correct.

free -m

Adjusting the system kernel Swappiness value

Swapiness is a property of the Linux kernel that defines how often the system uses swap space. Swapiness values range from 0 to 100 (the default is 60), a low value causes the kernel to avoid swaps as much as possible, while a high value causes the kernel to use swap space more aggressively.

This value defaults to 60 and we can check the current value using the command cat /proc/sys/vm/swappiness.

Normally we can change it to 10.

1
echo "vm.swappiness=10" >> /etc/sysctl.conf

Then use the sysctl -p command to make it effective.

Closing Swap

Sometimes we need to close a Swap partition, so we can use the following command.

First, deactivate the Swap partition.

1
swapoff -v /swapfile

Then check /etc/fstab and delete the line /swapfile swap swap defaults 0 0.

Finally delete the file /swapfile

1
rm /swapfile