sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
Check that the swap file is active:
sudo swapon --show
free -h
To enable it after reboot, add it to /etc/fstab:
/swapfile none swap sw 0 0
To remove it later:
sudo swapoff /swapfile
sudo rm /swapfile