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

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

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