Add Swap Space on Ubuntu 16.04

Before begin, please check if the system already has swap space available. Even though it’s possible to have multiple swap files or swap partitions, one should be enough.

free -h

Output
total used free shared buff/cache available
Mem: 488M 36M 104M 652K 348M 426M
Swap: 0B 0B 0B

As we can see no swap is active on the system. We will create a 1 Gigabyte file in this guide. Adjust this to meet the needs of your own server:

fallocate -l 1G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

We can check the output of the “free” command again to verify the swap:

free -h

Output
total used free shared buff/cache available
Mem: 488M 37M 96M 652K 354M 425M
Swap: 1.0G 0B 1.0G

The swap space has been set up successfully and our operating system will begin to use it.

Now let’s make the swap space used permanently:

cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Done!

Leave a Reply

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