First, create a file that will be used as swap:
fallocate -l 2G /swapfile
If the fallocate utility is not present on your system, or you get an error message saying fallocate failed: Operation not supported, use the following command to create the swap file:
dd if=/dev/zero of=/swapfile bs=1024 count=2097152 (4194304)
Set the file permissions to 600 to prevent regular users to write and read the file:
chmod 600 /swapfile
Create a Linux swap area on the file:
mkswap /swapfile
Activate the swap file by running the following command:
swapon /swapfile
To make the change permanent open the /etc/fstab file:
nano /etc/fstab
/swapfile swap swap defaults 0 0
Verify that the swap is active by using either the swapon or the free command, as shown below:
swapon --show
Source: How to Add Swap Space