How to Add Swap Space on Ubuntu 22.04

Swap Space is a space used to hold data that may not be able to fit in RAM. The Operating system supervises the swap space. It temporarily saves the data that is no longer required by the RAM. Also, the size of RAM is limited, and it is only utilized for the system’s required functions. Instead, use Swap Space to temporarily store data using a command-line interface.

We will explain the procedure to add and remove Swap Space on Ubuntu 22.04 in this article.

Adding Swap Space on Ubuntu 22.04

To add Swap Space on Ubuntu 22.04, follow the below-given instructions.

Step 1: Check existing Swap Files

In the first step, confirm the already existence of any Swap file in the system:

$ sudo swapon --show

As you can see, we are already using the swap file named “swapfile”. However, we will create a new Swap file for the demonstration:

The alternative method to check the Swap file existence is provided below:

$ free -h

Step 2: Check hard drive available space

Let’s check out the hard drive available space of the system using the mentioned command:

$ df -h

You can see that from the above-given output, we have “8.4” GB of available system space. Let’s move ahead to create a new Swap file.

Step 3: Create Swap file

Create a Swap file “swapfile1” by executing the “fallocate” command:

$ sudo fallocate -l 1G /swapfile1

Now, confirm that “swapfile1” get the exact drive space as you have defined:

$ ls -lh /swapfile1

Step 4: Set permissions of Swap file

In the next step, set the “swapfile1” permissions as it is only accessible by “root”:

$ sudo chmod 600 /swapfile1

Lets move ahead toward next step.

Step 5: Add Swap Space

Use the “mkswap” command to set “swapfile1” as a Swap Space:

$ sudo mkswap /swapfile1

Step 6: Enable Swap Space

Next, enable the Swap Space on your Ubuntu 22.04 system:

$ sudo swapon /swapfile1

Step 7: Verify Swap File activation

Verify the creation and activation of new Swap Space:

$ sudo swapon --show

Step 8: Set Swap file Permanent

To set the Swap file for permanent use, follow the command given below. First, add backup file into the “/etc/fstab”:

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

Next, in the “/etc/fstab” directory, add “swapfile1”:

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

Move to the next step for space tuning.

Step 9: Swap space tuning

We will perform some Swap space tuning. First, check the default “swappiness” settings:

$ cat /proc/sys/vm/swappiness

swappiness” defines how many times RAM data will get hit and move to Swap Space. Change the “swappiness” of the system by utilizing the provided command:

$ sudo sysctl vm.swappiness=20

Add the same line “vm.swappiness=20” into the “sysctl.conf” file to permanently set the given value and press “CTRL+O” to save changes:

$ sudo nano /etc/sysctl.conf

Check out the default settings of cache pressure:

$ cat /proc/sys/vm/vfs_cache_pressure

The above output indicates the default cache pressure setting is set as “100”. Let’s replace it with “60”:

$ sudo sysctl vm.vfs_cache_pressure=60

Again, add the same line “vfs_cache_pressure=60” into the “sysctl.conf” file to permanently set the given value and press “CTRL+O” to save changes:

$ sudo nano /etc/sysctl.conf

After completing the tuning, reboot your system and start using Swap Space on Ubuntu 22.04.

Let’s move ahead toward how to remove Swap Space from Ubuntu 22.04.

Removing Swap Space on Ubuntu 22.04

To remove Swap Space, first, use the provided command to disable the Swap file:

$ sudo swapoff -v /swapfile1

After that, remove the Swap file by typing the following command:

$ sudo rm /swapfile1

We have thoroughly elaborated the method to add and remove Swap Space from Ubuntu 22.04.

Conclusion

On Ubuntu 22.04, to add new Swap Space, check out the available hard drive space. Then, use the “$ sudo fallocate -l 1G /swapfile1” command to create a new Swap file. Make changes in Swap file permissions using the “chmod” command. Lastly, tune the created Space Space with the help of the “sysctl.conf” file. We have demonstrated the procedure to add and remove Swap Space on Ubuntu 22.04.