Overview

We’re going to cover a process how to create a swap file in Linux, how to activate it and how to adjust it. You might have heard of linux swap term or maybe of linux swap space when you installed your Linux distro on your PC/server. If you were not sure about creating a linux swap space process and didn’t create one for some reason, you can instead create a Linux swap file instead. Also, there are some advantages to have a swap file instead of swap partition. Swap file is much easier to manage - meaning, you can easily change swap file size anytime, you can activate it and deactivate it anytime and etc.

What is a Linux swap space/Linux swap file and when to use it?

To put it simply, linux swap is related to your PC RAM and it acts like RAM. A portion of you HDD space is dedicated to swap space and acts as RAM. It is recommended to have it on your PC or server, if the machine is low on RAM(less than 4GB) since it can help out with the performance of your PC. If sometimes you encounter a scenario where your PC/server runs out of RAM, one part of that information/processes will be transferred to the swap and that way it can prevent any system crashes or software crashes.

For that reason mainly, it is recommended to have it on your system. You can say, swap is like a virtual RAM. Swap file is most commonly used on virtual machines(VPS or cloud server and etc). But there’s a difference, swap is much slower than RAM since swap uses your hard drive resources, so don’t expect a RAM like performance.

How much of swap do you need?

Usually, the size of swap is based on how much of RAM you have installed on your PC. Then a multiplier of x2 is added to the amount of RAM you have on your PC. That means, if for an example you have 2 GB of RAM installed, then it is recommended to have 4 GB of swap, if you have 4 GB of RAM then swap should be 8 GB. This is the general rule but in my experience, I never had the need to have more than 4 GB of swap. Also, in some Linux distro documentation, you can find instructions that it is enough to have 2-4 GB of swap for 2-8 GB of installed RAM.

How to create a swap file in Linux

create a swap file in linux

This process is mainly done in terminal. All command lines will be listed here and we’ll go through each one. First, start the terminal and type in this command to create a swap file:

sudo fallocate -l 1G /swapfile

This command will create a 1 GB swapfile. So, if you need a larger swap file, just replace 1G in the command with the number you want. 1G represents swap file size. If you need to have 2 GB or 4 GB swap file, then we need to type in 2G or 4G in the command.

Next step is to set permissions for the swap file. Use the command:

sudo chmod 600 /swapfile

Now we need to format the file as swap. Type the command:

sudo mkswap /swapfile

After that, activate the swap with this command:

sudo swapon /swapfile

Next what we need to do is to make these changes permanent and make it so that the swap is active all the time. For that, we need to edit the fstab file with an text editor. We can use nano text editor and access the fstab file with this command:

sudo nano /etc/fstab

In the fstab file, add the following line:

/swapfile swap swap defaults 0 0

create a swap file in linux

You can check the status of the swap file with the command:

sudo free -h

Remove or edit the swap file

If for any reason you need to edit or to delete the swap file, first we need to deactivate it. We can deactivate with this command:

sudo swapoff -v /swapfile

And with this command we can delete it:

sudo rm /swapfile

Now to make a new swap file, repeat the process from above and create a new swap file with the file size you need. If you want to fully remove it and not to be active as well, go again to the fstab file and remove the swap file entry with the text editor.

Adjusting swappines

Swappines is a value that is controlled by the kernel and the process were kernel manages RAM resources and when to write to swap. If, for an example, we set a small swappines value, more RAM will be used. With these settings you can adjust the overall performance of your PC.

To see how the default settings of the swappiness, use this command:

cat /proc/sys/vm/swappiness

By default, the swappiness value is 60. This means that the kernel will write to swap if 40% of RAM or more is used. If you have enough RAM on your system, let’s say for an example 8 GB or more, you can change the swappiness level and let the system use more RAM in order to improve the performance a bit.

If you decide on that, it is recommended to set the swappiness around 10-20 and not less than 10. These swappiness values will let the system use 80-90% of RAM(20 for 80% and 10 for 90%).

Please note, that your mileage may vary on the performance and this depends on your system specs in general. So, you may need to set a different value than these in order to gain a better performance.

create a swap file in linux

To change the swappiness value, we again need to use a text editor and access the sysctl.conf file. Type in this command:

sudo nano /etc/sysctl.conf

Then add this value at the bottom:

vm.swappiness=10

Save changes and exit. In order for this change to take effect, we need to restart the PC. When the system boots up again, the swappiness value will be different,

Summary

We covered the steps how to create a swap file in Linux, and how to edit it, and how much swap file in size should be for your system. Also, we covered the steps how to adjust the swappines and how it works. I hope this guide was useful for you and also that your learned something as well. Thank you very much for your time.