View Categories

How to Create a Swap File on Ubuntu

4 min read

What is a swap file? #

Swap is a file on the hard drive that has been set aside for the operating system to temporarily store data that exceeds the amount of available RAM. This is important as any program that even temporarily exceeds the amount of system RAM will crash. Swap is used as a last resort and is of course temporary in nature. 

Swap is much slower than RAM, so if you notice your system using swap space regularly you should consider increasing the amount of system RAM available. 

How large of a swap file is required? #

The standard rule of thumb is that the swap file size should equal the amount of system RAM. For example, a system with 4GB of RAM would be matched with a 4GB swap file. This is not a hard rule. The size of the swap file can be sized up or down depending on requirements. 

– Check if swap is already enabled #

Run the below command to check for existing swap partition. 

sudo swapon --show

If there is no output, that means the system does not have a swap file. This can also be checked by using the “free” command.

free -h

 Output would look something like this:

              total        used        free      shared  buff/cache   available
Mem:          3.8Gi       2.1Gi       469Mi       0.0Ki       1.2Gi       1.4Gi
Swap:            0B          0B          0B

– Check for available space #

To ensure that there is enough room on the HDD for the swap file, run the “df” command to check the available space. 

df -h
Filesystem  Size  Used   Avail  Use%   Mounted on
tmpfs 393M 1.1M 392M 1% /run
/dev/vda1 78G 15G 63G 20% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/vda15 105M 6.1M 99M 6% /boot/efi
tmpfs 393M 4.0K 393M 1% /run/user/0

– Create the swap file #

In this example we will create a file called “swapfile” in the root directory of the system. This is done using the fallocate program. This program should already be installed, but if missing it can be installed using the apt package management system. 

sudo apt-get -y install util-linux

Now allocate the file. Since this system has 4GB of RAM, we will create a 4GB swap file.

sudo fallocate -l 4G /swapfile

– Enable the swap file #

Run the below commands to set the permission, set the file to the type of swap, and enable the system to start using the swapfile.

sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Next, check to ensure the swap file is enabled and in use. 

sudo swapon --show

Check again with the free utility to see the swap. 

free -h

– Making the swap file permanent  #

In order to survive a reboot, the /etc/fstab file needs to be modified so that the system uses the swap file after a reboot. Run the following commands to save a copy of /etc/fstab and add the required parameters. 

sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

– Tuning system swap and cache pressure settings #

Swapiness determines how often the system swaps out of RAM to the swap file. You can further tune your system to utilize swap more effectively. The value is between 0 and 100. A value of 0 would mean to never swap, and a value of 100 would mean to swap as much as possible to keep RAM free. Since swap is orders of magnitude slower than RAM, we want to limit how much the system swaps. However, we also want to ensure the system does not crash due to a lack of RAM. The default for most Ubuntu systems is 60. We want to lower that number to use RAM as much as possible before using the swap. A good working value is 10. This will ensure the system uses RAM as much as possible and only swap when required.

Cache pressure is how often, or how aggressively, the kernel reclaims memory from virtual filesystem caches. The default in Ubuntu is 100, which is pretty high. This causes the system to perform some rather expensive requests more often. We can increase performance by lowering this value that provides a better balance between performance and RAM management. A value of 50 typically provides better overall performance results. 

To set the current running configuration, use the following commands:

sudo sysctl vm.swappiness=10
sudo sysctl vm.vfs_cache_pressure=50

To survive a reboot, we need to also set these values in /etc/sysctl.cnf

sudo vi /etc/sysctl.conf

Add this to the bottom and save:

vm.swappiness=10
vm.vfs_cache_pressure=50