How to Create a Swap File

Step 1: Turn off existing swap

sudo swapoff /swapfile

Step 2: Create a new swap file (example: 4 GB)

sudo fallocate -l 4G /swapfile

Step 3: Set correct permissions

sudo chmod 600 /swapfile

Step 4: Make it a swap area

sudo mkswap /swapfile

Step 5: Enable the swap file

sudo swapon /swapfile

Step 6: Verify

free -h

Step 7: Add to /etc/fstab

Add this line to /etc/fstab to enable swap at boot

/swapfile none swap defaults 0 0