Method-1 : Create the swap file using fallocate

Good way to add a swap file (like windows does) if you forget to create them during install

Create the swap file using fallocate

fallocate program is a best way to create a file of a preallocated size instantly.

The following command will create 1GB of /swapfile.

$ sudo fallocate -l 1G /swapfile

Check whether its created correct size of file or not.

$ ls -lh /swapfile
-rw-r--r-- 1 root root 1.0G Jun  7 09:49 /swapfile

Change the file permission to 600 to only accessible by root user.

$ sudo chmod 600 /swapfile

Convert the file as swap area by running below command.

$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=cda50e0e-41f3-49c7-af61-b8cb4a33a464

Enable the swap file by running below command.

$ sudo swapon /swapfile

Add newely created swap file into fstab file, so that swap space partition available even after the reboot.

$ vi /etc/fstab

/swapfile  swap  swap  defaults  0 0

Check newly created swap file.

$ swapon --show
/dev/sda5 partition    2G 657.8M   -1
/swapfile file      1024M     0B   -2

Yes i can see the new 1GB swapfile. Reboot the system to use the new swap file.