Apr 1, 2023 5 min read

How to Add Swap Space on Ubuntu 20.04: A Step-by-Step Tutorial

Add swap space on Ubuntu 20.04 with our easy-to-follow tutorial. Increase your system's performance and prevent crashes with these simple steps.

How to Add Swap Space on Ubuntu 20.04
Table of Contents

Introduction

Before we begin talking about how to add Swap Space on Ubuntu, let’s briefly understand – What is a Swap Space?

Swap space is a crucial component of the Ubuntu operating system, which is used to support virtual memory management. It is a dedicated area of the hard disk that can be used as a temporary storage space when the computer's physical memory (RAM) is full. Swap space provides a way for the operating system to extend the amount of available memory beyond the physical limitations of the computer.

When a computer runs out of physical memory, it begins to use swap space to temporarily store data that would typically be stored in RAM. This process is called swapping, and it helps prevent the system from crashing or freezing due to a lack of memory. The swap space is divided into small, fixed-size blocks called pages. The operating system swaps these pages in and out of physical memory as needed, allowing the system to keep running smoothly.

Ubuntu, like many other Linux-based operating systems, creates a swap partition during the installation process. This partition is usually located on the hard disk and is used exclusively for swap space.

Ubuntu also allows users to create a swap file, which can be used instead of a swap partition. This file can be located anywhere on the file system, which makes it a more flexible option for users who need to manage their disk space carefully. However, using a swap file can be slower than using a swap partition because the file is stored on a regular file system, which can be fragmented.

In this tutorial, you will add a Swap file on Ubuntu 20.04. We will also address some FAQs related to the Swap Space on Linux.

Few Things to Know before Beginning

Swap has a slower access time than the physical memory, hence it can't be seen as a replacement for physical memory. If your system frequently runs out of memory, you should be adding more RAM.

The size of the Swap file is dependent on the RAM of your system:

  1. less than 2 GB RAM: Double the amount of RAM
  2. 2 – 8 GB RAM: Size similar to that of the RAM
  3. More than 8 GB RAM: Minimum 4 GB of Swap

Only a root or user with sudo privilege can activate the Swap file.

Creating a Swap File

Here, we will create 2 GB a Swap file. If you want to add more Swap, change the 2G number per your requirement.

Follow the below steps to add Swap Space on Ubuntu 20.04:

1) Firstly, you need to create a file that will be used as a Swap:

sudo fallocate -l 2G /swapfile

If the fallocate utility is not present in your system, use the below command instead:

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

2) If you want to prevent a regular user from writing and editing the file, set the file permission to 600.

sudo chmod 600 /swapfile

3) Now, create a Linux Swap area on the file:

sudo mkswap /swapfile
Output

Setting up swapspace version 1, size = 3 GiB (2147479552 bytes)
no label, UUID=fde7d2c8-06ea-400a-9027-fd731d8ab4c8

4) After that, run the following command to activate the Swap file:

sudo swapon /swapfile

If you want to make a permanent change, you need to open the /etc/fstab file:

sudo nano /etc/fstab

and paste the following line:

/swapfile swap swap defaults 0 0

5) To verify if the Swap is active, either run the swapon or the free command:

sudo swapon --show
Output

NAME      TYPE      SIZE  USED PRIO
/swapfile file        2G    0B   -1
sudo free -h
Output

              total        used        free      shared  buff/cache   available
Mem:          981Mi        97Mi        68Mi       0.0Ki       814Mi       735Mi
Swap:         2.0Gi        10Mi       1.9Gi

Changing the Swappiness Value

The Swappiness value can vary between 0 and 100. 0 Swappiness value means it will tell the kernel to aggressively avoid swapping out for as long as possible. On the contrary, a value of 100 will aggressively be swapping the processes out of the physical memory.

Accessing Swap memory is much slower than accessing the physical memory directly. Lower swappiness parameters will improve overall system performance. For normal desktop installation 10 swappiness is recommended. For database servers, 0 or 1 is recommended.

The optimal swappiness value will depend on your system workload as well as the size of the RAM memory. You should adjust this parameter in small increments, which will help to find an optimal value.

Generally the swappiness value of 60 is fine but for a production server, you may want to lower the value.

3) If you want to set the swappiness value to 10 at runtime. Type the following command as a root or sudo user:

sudo sysctl vm.swappiness=10

4) Then, to make the swappiness parameter persistent across the reboots. Open /etc/sysctl.conf file with your text editor:

sudo nano /etc/sysctl.conf

5) After that, locate vm.swappiness parameter and proceed to change its value. If this parameter does not exist then, append the below line to the file:

vm.swappiness=10

Steps to Remove a Swap File

If you want to deactivate or delete the swap file, follow the below steps:

  1. Firstly, you need to deactivate the Swap space:
sudo swapoff -v /swapfile

2. After that, remove the swap file entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.

3. Use the rm command to remove the Swap file:

sudo rm /swapfile

FAQs to Add Swap Space on Ubuntu 20.04

How to reduce Swappiness?

The best thing is changing the swappiness to a lower level, like 10. Then, the user will save the file and close it. After rebooting, swappiness will be set to 10. The command is sudo cat /proc/sys/vm/swappiness.

Does Swappiness need a reboot?

The value for swappiness can be set with a single terminal command. Users should only run the command if they are familiar with running terminal commands. A reboot is not necessary after running the command.

How do I check if I have swap space on my Ubuntu 20.04 system?

You can use the free command to check if you have any swap space allocated on your Ubuntu 20.04 system. Simply open the terminal and type free -h. If you have swap space, it will be listed under the Swap: column.

Can I change the size of my swap space on Ubuntu 20.04 after installation?

Yes, you can change the size of your swap space on Ubuntu 20.04 after installation. To do so, you need to either resize the swap partition or create a new swap file with the desired size and activate it using the swapon command. Remember to update the /etc/fstab file to reflect the changes.

How can I monitor my Ubuntu 20.04 system's swap usage?

You can monitor your Ubuntu 20.04 system's swap usage using the free command or the top command. Both commands display information about the system's memory usage, including swap usage. You can also use the vmstat command to get more detailed information about the system's memory usage.

What happens if I run out of swap space on my Ubuntu 20.04 system?

sIf you run out of swap space on your Ubuntu 20.04 system, the system may become unresponsive or even crash. The operating system will start to kill processes to free up memory, which can lead to data loss or corruption. Therefore, it is essential to ensure that your system has enough swap space to prevent such scenarios.

Conclusion

We hope this detailed tutorial helped you to add Swap space on Ubuntu 20.04.

If you have any queries or doubts, please leave them in the comment below. We'll be happy to address them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.