Enable swap
sudo swapon -s
Create a 2GB file
sudo dd if=/dev/zero of=/swapfile bs=1024 count=2048k
Create the swap
sudo mkswap /swapfile
You should get something like this in return
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=b6f6f8ba-d8c7-4c12-9c1d-fa96826d9075
Turn the swap on
sudo swapon /swapfile
Edit your /etc/fstab and add to it:
/swapfile none swap sw 0 0
Set swapiness to 10 so you don't get a poor performance
echo 10 | sudo tee /proc/sys/vm/swappiness
echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf
Protect the swap file
sudo chown root:root /swapfile
sudo chmod 0600 /swapfile