Create a swap file on Linux
Create a swap file on Linux, enable it and make it persistent through /etc/fstab.
A swap file adds swap space to a Linux system without creating a dedicated partition.
Create the swap file
The commands below create a 1 GiB file (count=1048576) or a 4 GiB file (count=4194304), using 1 KiB blocks.
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
# or
sudo dd if=/dev/zero of=/swapfile bs=1024 count=4194304
sudo mkswap /swapfile
sudo chmod 600 /swapfile
sudo swapon /swapfile
swapon --show
swapon --show lists the active swap areas.
Make the swap persistent
To activate the file at boot, add the following line to /etc/fstab:
/swapfile none swap sw 0 0