1.Create an empty file (1K * 8M = 8 GiB).
$sudo mkdir -v /var/cache/swap
#cd /var/cache/swap
#dd if=/dev/zero of=swapfile bs=1K count=8M
#chmod 600 swapfile
2.Convert newly created file into a swap space file.
#mkswap swapfile
3.Enable file for paging and swapping.
#swapon swapfile
Verify by: swapon -s or top:
#top -bn1 | grep -i swap
Should display line like: KiB Swap: 4194300 total, 4194300 free
To disable, use sudo swapoff swapfile command.
4.Add it into fstab file to make it persistent on the next system boot.
#echo "/var/cache/swap/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
5.Re-test swap file on startup by:
#swapoff swapfile
#$swapon -va
Reference :
https://askubuntu.com/questions/33697/how-do-i-add-swap-after-system-installation