Create a Linux Swap File

A quick guide to creating and enabling a swap file.

① Turn off existing swap

sudo swapoff /swapfile

② Create a new swap file (8 GB)

sudo fallocate -l 8G /swapfile

③ Set correct permissions

sudo chmod 600 /swapfile

④ Format it as swap

sudo mkswap /swapfile

⑤ Enable the swap file

sudo swapon /swapfile

⑥ Verify

free -h

⑦ Enable it at boot

Edit /etc/fstab and add the following line

/swapfile none swap defaults 0 0

After rebooting, run free -h again to verify that your swap file is active.