Monday, October 2, 2023
- Advertisement -

How to Add Swap on Ubuntu

Assalamu Alaikum.
Welcome to Uddoyon, in this article we try to know How to Install Swap (Virtual Ram) on Ubuntu System so keep reading…

Swap space is an essential part of a Linux system that comes into play when the available physical RAM is fully utilized. In such cases, inactive memory pages are transferred from RAM to the swap area located on the disk.

Two options for configuring swap space exist: a dedicated swap partition or a swap file. When utilizing Ubuntu on a virtual machine, the presence of a swap partition is usually absent, leaving the creation of a swap file as the sole alternative.

This guide outlines the process of setting up a swap file on Ubuntu 22.04.

Before You Proceed:
It is crucial to recognize that swap space should not be seen as a replacement for physical memory. As the swap area resides on the hard drive, it features slower access times compared to physical RAM. If your system consistently encounters memory shortages, the recommended solution is to augment the physical RAM capacity.

The size of the swap file is typically determined by the following guidelines:

* Systems with less than 2 GB RAM: Allocate a swap size twice that of the available RAM.
* Systems with 2 to 8 GB RAM: Set the swap file size equal to the amount of available RAM.
* Systems with more than 8 GB RAM: Allocate a minimum of 4 GB for the swap space.

The activation of the swap file requires root access or a user with sudo privileges.

Creating the Swap File:In this example we create a 2GB swap file. To adjust the swap size, replace “2G” with your desired swap space size.Follow these steps to create swap space on Ubuntu 22.04:Checking Existing Swap Space:Before creating a new swap file, determine whether your system already has existing swap space by executing the following command in the terminal:

swapon --show

Creating the Swap File:
To create a 2GB swap file, run the following commands in the terminal:

sudo fallocate -l 2G /swapfile

In case the fallocate utility is absent or produces an error message like “fallocate failed: Operation not supported,” employ the following command instead:

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

Set File Permission:
Safeguard the swap file by restricting access to root users only:

sudo chmod 600 /swapfile

Activating the Swap File:
Initialize the swap file with the following command:

sudo mkswap /swapfile

Activate the swap file to make it available for use:

sudo swapon /swapfile

To make the Swap for permanent open the “/etc/fstab” file:

sudo nano /etc/fstab

Paste following line:

/swapfile swap swap defaults 0 0

Make sure that the swap is active by using either the ‘swapon’ or the ‘free’ command, as shown below:

sudo swapon --show
sudo free -h

Swap Creation Completed…

Adjusting Swappiness:
Swappiness a kernel property in Linux, influences the frequency of swap space utilization. It operates on a scale of 0 to 100. A lower value encourages the kernel to minimize swap usage, while a higher value prompts more aggressive swap space utilization.

The default swappiness value on Ubuntu is set at 60. To inspect the present value, execute the following command:

cat /proc/sys/vm/swappiness

To maintain this parameter across reboots, append the following line to the “/etc/sysctl.conf” file.
open /etc/sysctl.conf file in Nano:

sudo nano /etc/sysctl.conf

Copy and paste vm.swappiness=10 in the end of file:


Identifying the optimal swappiness value hinges on your system’s workload and memory utilization. Incrementally adjust this parameter to ascertain the most suitable value for your circumstances.

Removing the Swap File:
First need to Disabling the Swap File:
Before deleting the swap file, you need to deactivate it:

sudo swapoff /swapfile

Deleting the Swap File:
Remove the swap file using the following command:

sudo rm /swapfile

Updating “/etc/fstab”:
To prevent the swap file from being automatically enabled during system boot, open the “/etc/fstab” file:

sudo nano /etc/fstab

Comment out or remove the line that references the swap file:

/swapfile swap swap defaults 0 0

Swap Successfully Deleted…

In conclusion, swap space plays a vital role in maintaining system stability and performance. By following the steps outlined in this article, you can easily create a 2GB swap file on your Ubuntu 22.04 system and manage it effectively, including its removal when necessary. Remember that swap space should complement physical memory and not serve as a substitute.

Thanks for Reading, stay with us to know more things as like this.

- Advertisement -
Related Articles
- Advertisement -

Popular Articles

- Advertisement -