Open In App

How to Add Swap Space on Ubuntu 20.04

Swap file takes space in the disk storage and later act as a memory for computation whenever the RAM in our system is full, it is important to create swap space in the Ubuntu system if you feel like your physical RAM is not enough to compute the work, the swap space can be created without any third party software because the ubuntu has a built-in feature for creating swap space files.

What is swap space in Ubuntu?

The swap file is used to fulfill the need for the physical RAM when the RAM is full but the system still needs additional computational power then it can use the swap space we create to fulfill this need, so in this article, we will understand how to add swap space on ubuntu system step by step.



Note: Only a user with sudo or root access can activate the swap space.

The size for the swap file is based on the size of RAM that you may currently have on your system, for example below is the general idea for the requirement of swap space but it can vary as well:



How to add swap space on Ubuntu:

Adding the swap space in Ubuntu follows two steps, in the first step we create the swap file and then in the second step we activate the swap space on Ubuntu, so let’s understand each of these steps one by one:

Create the swap space:

In the first step, we will have to create the swap space, for this, we will have to follow the steps given below:

Step 1: Create the file for swap:

This is the first step, we will create a swap file in the system, for this, we will have to enter the following command:

sudo fallocate -l 2G /swapfile

Lets understand what this command actually says:

Output:

sudo fallocate -l 2G /swapfile

If you are getting an error as “allocate failed: Operation not supported” then it means that your system does not have to allocate facility, in that case, you can also use the below command:

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

This command is also used in Ubuntu so that we can create a swap file, but it uses a different approach than the fallocate command, it has the following components:

Step 2: Set permissions for the file:

Now we will change the permission for this file to 600, this means that regular users can not read/write the file, for this we will use the following command:

sudo chmod 600 /swapfile

This command is used in Ubuntu to modify the permissions of a file named /swapfile, let’s understand the command:

Output:

sudo chmod 600 /swapfile

Step 3: Create Linux Swap area:

Now we will have to create an area for the Linux swap, you will have to use the below command:

sudo mkswap /swapfile

This command is used in Ubuntu so that we can set up the swap area on a specified file. lets understand the command:

Output:

sudo mkswap /swapfile

Activate the swap file & finalize:

Step 1: Activate the swap file:

Now we can finally activate the swap file, for this, we will have to use the following command:

sudo swapon /swapfile

This command is used in Ubuntu to enable a swap area on a specified file. lets understand the command:

Note: Other commands are same as above so no explanation is needed.

Output:

sudo swapon /swapfile

Step 2: Make permanent changes:

Now we will have to make the changes permanent in the system, we will have to open the path “/etc/fstab” For this enter the following command:

sudo nano /etc/fstab

This command opens the /etc/fstab file using the nano text editor with superuser privileges. lets understand what this command does:

Output:

sudo nano /etc/fstab

Step 3: Paste the code:

Now we will have to paste the following line into the file that we have just opened:

/swapfile swap swap defaults 0 0

The command we are using above is used for an entry in the /etc/fstab file. lets understand this command:

Output:

/swapfile swap swap defaults 0 0

Note: after this, save the file using the shortcut CTRL + T.

Step 4: Verify the swap file:

Now we will have to check whether the swap file is created and activated or not, by entering the following command:

sudo swapon --show

The swapon –show command is used to display a list of currently active swap devices or files, lets understand this command:

Output:

sudo swapon –show

This means that we have successfully created and activated the swap file on the Ubuntu system.

How to Add Swap Space on Ubuntu 20.04 – FAQs

How do I check if my Ubuntu 20.04 system has swap space enabled?

It is very easy to check if you have swap space enabled, you can use the following command in the terminal to check if swap space is enabled:

sudo swapon --show

What command should I use to create a swap file on Ubuntu 20.04?

To create a swap file, you can use the dd command to allocate space and the mkswap command to set it up as swap space. here is an example for this command:

sudo fallocate -l [size]G /swapfile && sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile

How can I add a swap partition to my Ubuntu 20.04 system??

You can use the mkswap command to set up an existing partition as swap space and the swapon command so that you can enable it, for example (Remove the partition to the partition identifier (example – /dev/sda3):

sudo mkswap /dev/[partition]
sudo swapon /dev/[partition]

What is the command to configure Ubuntu 20.04 to automatically mount swap space on boot?

To configure Ubuntu 20.04 to automatically mount swap space on boot, you can edit the /etc/fstab file, here is an example for this:

sudo nano /etc/fstab

Add the following line at the ending of the file:

/swapfile none swap sw 0 0

How can I resize an existing swap file on Ubuntu 20.04?

You can use the following commands to resize an existing swap file (you will have to replace the [new size] with the new size of the swap file you want to set in gigabytes):

sudo swapoff /swapfile
sudo fallocate -l [new size]G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Conclusion

Swap files are handy when you are running full of physical RAM, in this article we have understood how we can create the swap file and also activate it so that whenever the Ubuntu system runs out of physical RAM then the system can easily use the swap file as memory, follow this article step by step to understand how you can create swap files in ubuntu.


Article Tags :