Create a swap file on Ubuntu 18.04

Learn how to create a swap file on Ubuntu 18.04. A swap file can be an important addition to your RAM for your VM and can help to avoid system crashes due to running out of memory. In other words if you run out of physical memory then the system will use the virtual memory in our case a swap file.

Check if Swap is Enabled

As standard most Ubuntu VMs do not come with a swap file enabled. You can check if you have swap enabled with the following command

sudo swapon -s

If you get an empty response then this means you do not have swap enabled and will need to create the swap file in order to use this virtual memory setup.

Create the Swap File

In order to have swap enabled on the VM we must create a file and allocate storage space to this file. Though there is some debate on how large a swap file should be, I tend to use double the RAM. For example if my VM has 1GB RAM then I would generally create a 2GB swap file. We also need to set the correct permissions for the swap file as well.

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile

Make Swap

We need to tell the system that we want to use this file as a swap with mkswap

sudo mkswap /swapfile

Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=1989dc48-339e-4033-a314-10f734114c5a

Enable Swap

We need to enable swap on the VM with the swapon command

sudo swapon /swapfile

Let’s check the swap status once more to make sure that we have configured this properly

sudo swapon -s

If you have successfully created the swapfile and have enabled you should get an output similar to below

Filename                                Type            Size    Used    Priority
/swapfile                               file            2097148 0       -2

Make the Swap File Permanent

Congratulations you have now enabled the swap file system but this will be lost whenever the VM is restarted, so we will need to add this to the /etc/fstab file to mount this file as a swap on each boot of the VM.

sudo nano /etc/fstab

We need to add the following to the end of the the file and save.

/swapfile   none    swap    sw    0   0


Swappiness is the kernel parameter that defines how much (and how often) your Linux kernel will copy RAM contents to swap. This parameter’s default value is “60” and it can take anything from “0” to “100”. The higher the value of the swappiness parameter, the more aggressively your kernel will swap.

We need to now set up the swappiness for the swap file in the kernel. I am not too keen on using the swap file too often and tend to use a value of around 15-20, for this guide I will be using 20. Edit /etc/sysctl.conf

sudo nano /etc/sysctl.conf

Scroll to the bottom of this file and append the following


Save the file and finally reboot the system. Once the VM has rebooted check the swap status once more with

sudo swapon -s

Make sure you have something similar as follow

Filename                                Type            Size    Used    Priority
/swapfile                               file            2097148 0       -2

Congratulations, you have successfully configured and enabled swap for your VM.

Related posts