Add a swap file:
Create a file (you can replace /swapfile with another location and filename): sudo fallocate -l 1G /swapfile
Change its permissions: sudo chmod 600 /swapfile
Mark the new file as swap space: sudo mkswap /swapfile
Enable the swap file: sudo swapon /swapfile
Check and see if the file is enabled: swapon --show
Update fstab to make your changes permanent (you might want to create a backup of the existing fstab): echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Adjust swappiness, if needed:
temporarily: sudo sysctl vm.swappiness=25
permanent: echo 'vm.swappiness=25' | sudo tee -a /etc/sysctl.conf
Resize a swap file:
Turn off the active swap file you want to resize: sudo swapoff /swapfile
Change the swap file size: sudo fallocate -l 2G /swapfile
Mark it as swap space: sudo mkswap /swapfile
Turn the swap back on: sudo swapon /swapfile
Remove a swap file:
Turn off the active swap file you want to resize: sudo swapoff /swapfile
Remove the file: sudo rm /swapfile
Clean up the entry for the removed swap file from /etc/fstab