Here we will take a look at how to create a Linux Swap File. I prefer to use a swap file, over a swap partition. It’s simple to set up and quick to extend. So you don’t have to mess around with Partitions.

So let’s start!

Allocate the Swap File

First of all we have to create the file. To do that we will create the File /swapfile with 1G. But you can of course choose a bigger or smaller swap file. If you want more, or less, you can simply adjust the 1G in the command!

fallocate -l 1G /swapfile

Set permissions for the swap file

Now we will secure the file with a simple command!

chmod 600 /swapfile

Now we create the Linux Swap

THis will turn the file we created into an swap file we can use.

mkswap /swapfile

Activate Swap file

Now we tell our system to actually use the Swap File.

swapon /swapfile

And now we can already take advantage of the swap file! But if we want to have the swap persist after an reboot, we have to configure it to be attached on startup.

Make swap persist

To do this, we will edit the file /etc/fstab. We simply have to add a new line.

/swapfile swap swap defaults 0 0

🎉 Aaaaaaand we are done!

Thanks for reading this :)