Creating a Swap File

Now that we know our available hard drive space, we can create a swap file on our filesystem. We will allocate a file of the swap size that we want called swapfile in our root (/) directory.

The best way of creating a swap file is with the fallocate program. This command instantly creates a file of the specified size.

Since the server in our example has 1G of RAM, we will create a 1G file in this guide. Adjust this to meet the needs of your own server:

sudo fallocate -l 1G /swapfile

 We can verify that the correct amount of space was reserved by typing:

ls -lh /swapfile

 -rw-r--r-- 1 root root 1.0G Apr 25 11:14 /swapfile

 Our file has been created with the correct amount of space set aside.