How to Add Swap Space on Ubuntu 18.04

Add Swap Space on Ubuntu 18.04

When operating system no longer can put data on RAM then it uses some area of hard disk space to store data that area is called Swap space. When the system goes on low memory mode then it uses Swap space to store data. Basically, it swaps the data on RAM of an ideal program to swap area. In this tutorial, we are going to learn how to add swap space on Ubuntu 18.04.


Before you start to create sudo user on Ubuntu. You must have the root user account credentials of your system.

Now you should check if on your system swap space is already enabled or not running below command:

sudo swapon --show

The output should look like:

/dev/sda7 partition  2.3G   0B   -2

If you get no output means swap space is not enabled on your system. Check following steps to add swap space on your system.

Create Swap File

Here in this tutorial, we will add 512 MB of swap space to the system. To add more space simply replace 512 with another number.

Now run following command to create a swap file of 512 MB.

sudo dd if=/dev/zero of=/swapfile bs=1024 count=512k

Set the permissions for the swap file running below command:

sudo chmod 600 /swapfile

Run below command to setup swap space on your system using mkswap:

sudo mkswap /swapfile

Activate the created swap file as swap space using the following command:

sudo swapon /swapfile

Make the swap file permanent

When the system reboots the server will not save above changes permanently. To make above changes permanent use following steps

First backup /etc/fstab file to /etc/fstab.bak:

sudo cp /etc/fstab /etc/fstab.bak

Now run below command to make the swap file permanent:

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

Verify Swap Space

To confirm and verify swap space run below command:

sudo swapon --show

The output should look like:

/swapfile file  512M   10.9M   -1

You can also check the space by running the following command:

free -h

The output should look like:

               total      used        free       shared     buff/cache   available
Mem:           2048M      657M        283M       102.3M     246M         1010M
Swap:          512G       100M        256M

Changing Swappiness Parameter

The swappiness parameter is used to configure how often the swap space should be used. The swappiness parameter value lies between 0 to 100. As the value is set to smaller the system will avoid using swap space and if the value is bigger then the system will use swap space much often. The default swappiness value on the Ubuntu system is 60. The value 60 is ok for desktop system while you should change it for the production system.

However to check swapiness value run following command:

cat /proc/sys/vm/swappiness

To set the swappiness value to 15, run below command:

sudo systemctl vm.swappiness=15

The above swappiness value will not persist after a reboot. So to make the changes permanent edit /etc/systemctl.conf file:

sudo nano /etc/systemctl.conf

Then add the following line to the file:


Deactivate swap file

Use following instructions to deactivate swap space.

To deactivate swap space run following command:

sudo swapoff -v /swapfile

Edit /etc/fstab file and remove /swapfile swap swap defaults 0 0 line from it:

sudo nano /etc/fstab

Now delete the swap file:

sudo rm /swapfile


You have successfully learned how to add Swap Space on Ubuntu 18.04. If you have any queries regarding this then please don’t forget to comment below.