Categories: Ubuntu

How to Add Swap Space on Ubuntu 22.04

Swap is a specific part of the hard disk that is reserved for the operating system to temporarily store data that is no longer required in RAM. With some exceptions, the swap space permits you to expand the amount of data that your server can hold in its current working memory. So, when there does not exist enough space in RAM for holding the data related to the currently used applications, the added swap space will be utilized in such a situation.

This blog will demonstrate the method to add swap space on Ubuntu 22.04.

Adding swap space on Ubuntu 22.04

Sponsored

For the purpose of adding swap space on Ubuntu 22.04, follow the given instructions.

Step 1: Check Swap information

First of all, run the provided command to check in there exists any pre-configured swap on the system:

$ sudo swapon –show

Alternatively, utilize the “free” command with the “-h” option to verify the existence of active swap space:

$ free -h

The output of the given commands indicates that a swap file is already present on our system. However, we will create a new swap file for demonstration and show you the method of adding swap space in Ubuntu 22.04.

Step 2: Check available hard drive space

In the next step, check the available space of the hard drive to ensure that it has enough space:

$ df -h

As you see, we have “9.5G” available space on our system:

Step 3: Create Swap file

For the purpose of creating a Swap file, write out the provided “fallocate” command in your Ubuntu 22.04 terminal with the “-l” option and specify the amount of space you want to allocate. In our case, we will create a swap file named “swapfile1” having “1G” space:

$ sudo fallocate -l 1G /swapfile1

Step 4: Verify Swap file allocated space

Verify the allocated space to the swap file by utilizing the provided command:

$ ls -lh /swapfile1

Step 5: Change Swap file permission

Utilize the provided “chmod” command to change the permissions of file to be only accessible by “root”:

$ sudo chmod 600 /swapfile1

Then, verify the assigned file permissions:

$ ls -lh /swapfile1

Step 6: Mark swapfile1 as swap space

Type out the following command for marking the created “swapfile1” as swap space:

$ sudo mkswap /swapfile1

Step 7: Enable Swap file

Enable the created swap file and permit the Ubuntu 22.04 system for its usage:

$ sudo swapon /swapfile1

Step 8: Check swap file availability

After enabling swap file, verify if it is available or not by using the given “swapon” command:

$ sudo swapon –show

Or, execute the “free” command with the “-h” option for the same purpose:

$ free -h

Sponsored

Step 9: Make Swap file permanent

The changes you have made till now are only applied to the current session. This states that as soon as the system reboot, the server will not automatically continue with the added settings. To make the swap file permanent, add the created file to the “/etc/fstab” file.

So, firstly, backup the “/etc/fstab” file as a precautionary measure:

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

Then, append the following information related to “swapfile1”:

$ echo ‘/swapfile1 none swap sw 0 0’ | sudo tee -a /etc/fstab

Now move to the last step.

Step 10: Tune Swap space settings

At this point, Swap space is permanently and ready to use. However, we will tune some additional swap space settings that will impact the performance of your Ubuntu 22.04 in a good way.

Firstly, check the value of the “swappiness” parameter, which indicates how often the system is going to swap data out of the RAM to the added swap space. The parameter’s value will lie between “0” and “100”. In order to check the current value of the “swappiness” parameter, run the following “cat” command:

$ cat /proc/sys/vm/swappiness

Then utilize the “sysctl” command to assign a new value to the “swappiness” parameter:

$ sudo sysctl vm.swappiness=20

To permanently save the added settings for the added swap space, add the same line in the “sysctl.confl” file:

$ sudo nano /etc/sysctl.conf

Another parameter’s value that we are going to tune is “vfs_cache_pressure”. This parameter controls the cache dentry and inode information. Check its current value with the help of the provided “cat” command:

$ cat /proc/sys/vm/vfs_cache_pressure

Similarly, set a new value for the “vfs_cache_pressure” parameter using the “sysctl” command:

$ sudo sysctl vm.vfs_cache_pressure=40

Add the same line in the “sysctl.conf” file to permanently set the value of “vfs_cache_pressure” parameter:

$ sudo nano /etc/sysctl.conf

Hit “CTRL+O” to save the added line and switch back to the terminal. The created Swap space is all ready to use with the configured settings:

We have compiled the easiest method for adding swap space on Ubuntu 22.04.

Conclusion

For the purpose of adding swap space in Ubuntu 22.04, firstly, check the available hard drive space, then execute the “$ sudo fallocate -l 1G /swapfile1” command while specifying the allocated space and path to the swap file that is going to be created. Then change the file permissions of the swap file, mark it as swap space, and enable it on your Ubuntu 22.04 system. This blog demonstrated the method to add swap space on Ubuntu 22.04.

Ubuntu Server Admin

Recent Posts

Building RAG with enterprise open source AI infrastructure

One of the most critical gaps in traditional Large Language Models (LLMs) is that they…

15 hours ago

Life at Canonical: Victoria Antipova’s perspective as a new joiner in Product Marketing

Canonical is continuously hiring new talent. Being a remote- first company, Canonical’s new joiners receive…

2 days ago

What is patching automation?

What is patching automation? With increasing numbers of vulnerabilities, there is a growing risk of…

3 days ago

A beginner’s tutorial for your first Machine Learning project using Charmed Kubeflow

Wouldn’t it be wonderful to wake up one day with a desire to explore AI…

4 days ago

Ubuntu brings comprehensive support to Azure Cobalt 100 VMs

Ubuntu and Ubuntu Pro supports Microsoft’s Azure Cobalt 100 Virtual Machines (VMs), powered by their…

4 days ago

Ubuntu Weekly Newsletter Issue 870

Welcome to the Ubuntu Weekly Newsletter, Issue 870 for the week of December 8 –…

4 days ago