Create a Swap File

Here’s how to create a swap file and make it permanent via fstab:

Create the swap file (example: 2 GB):

bash
sudo fallocate -l 2G /swapfile

If fallocate isn’t available (e.g., on some filesystems), use dd instead:

bash
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048

Set permissions, format, and enable it:

bash
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Verify it’s active:

bash
swapon --show

Add it to fstab so it persists across reboots. Append this line to /etc/fstab:

bash
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab