Guide for creating or modifying swap file in Ubuntu 22.04
First, check if you have an existing swap file:
sudo swapon --show
If you have an existing swap file that you want to modify, disable it:
sudo swapoff /swapfile
Choose the desired size (e.g., 8GB) and create/recreate the swap file:
sudo fallocate -l 8G /swapfile
Note: If fallocate fails, you can use dd instead:
sudo dd if=/dev/zero of=/swapfile bs=1M count=8192
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
Edit the fstab file:
sudo nano /etc/fstab
Add or modify the line:
/swapfile swap swap defaults 0 0
sudo swapon --show
# or
free -m
Reboot your system to ensure all changes take effect properly:
sudo reboot
Note: The default swap file location is /swapfile
. If you want to use a different location, adjust the paths in the commands accordingly.