How to Add Swap Space on Ubuntu 20.04

How to Add Swap Space on Ubuntu 20.04

How to Add Swap Space on Ubuntu 20.04

We hope this post helped you to find out  How to Add Swap Space on Ubuntu 20.04

Swap is an area on a disk that’s used when the quantity of bodily RAM reminiscence is full. When a Linux system runs out of RAM, inactive pages are moved from the RAM to the swap area.

Swap area can take the type of both a devoted swap partition or a swap file. Usually, when operating Ubuntu on a digital machine, a swap partition isn’t current, and the one choice is to create a swap file.

How to Add Swap Space on CentOS 8

This tutorial explains easy methods to add a swap file on Ubuntu 20.04 methods.

Earlier than You Start #

Swap shouldn’t be seen as a alternative to bodily reminiscence. Since swap area is a piece of the laborious drive, it has a slower entry time than bodily reminiscence. In case your system always runs out of reminiscence, you need to add extra RAM.

Typically, the dimensions of the swap file is determined by how a lot RAM your system has:

  • Techniques with lower than 2 GB RAM – 2 occasions the quantity of RAM.
  • Techniques with 2 to eight GB RAM – the identical dimension as the quantity of RAM.
  • Techniques with greater than eight GB RAM – not less than four GB of Swap.

Solely root or consumer with sudo privileges can activate the swap file.

Stop 1 Making a Swap File #

On this instance, we’ll create 2 GB swap file. If you wish to add extra swap, substitute 2G with the dimensions of the swap area you want.

Full the steps beneath so as to add swap area on Ubuntu 20.04:

Step 2. First, create a file that will be used as swap:

sudo fallocate -l 2G /swapfile

Step 3. If the fallocate utility is not present on your system, or you get an error message saying fallocate failed: Operation not supported, use the following command to create the swap file:

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

Step 4. Set the file permissions to 600 to prevent regular users to write and read the file:

sudo chmod 600 /swapfile

Step 5. Create a Linux swap area on the file:

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

Step 6. Activate the swap file by running the following command:

sudo swapon /swapfile 

To make the change permanent open the /etc/fstab file:

 sudo nano /etc/fstab

and paste the following line:

/swapfile swap swap defaults 0 0

Step 7. Verify that the swap is active by using either the swapon or the free command, as shown below:

sudo swapon --show
NAME TYPE SIZE USED PRIO /swapfile file 2G 0B -1
sudo free -h 
total used free shared buff/cache available Mem: 981Mi 97Mi 68Mi 0.0Ki 814Mi 735Mi Swap: 2.0Gi 10Mi 1.9Gi

Adjusting the Swappiness Value #

Swappiness is a Linux kernel property that defines how usually the system will use the swap area. It may possibly have a price between Zero and 100. A low worth will make the kernel to attempt to keep away from swapping at any time when doable, whereas the next worth will make the kernel to make use of the swap area extra aggressively.

On Ubuntu, the default swappiness worth is ready to 60. You possibly can examine the present worth by typing the next command:

cat /proc/sys/vm/swappiness

Whereas the swappiness worth of 60 is OK for many Linux methods, for manufacturing servers, you might must set a decrease worth.

For instance, to set the swappiness worth to 10, run:

sudo sysctl vm.swappiness=10

To make this parameter persistent across reboots, append the following line to the /etc/sysctl.conf file:


The optimum swappiness worth is determined by your system workload and the way the reminiscence is getting used. It’s best to modify this parameter in small increments to seek out an optimum worth.

Eradicating a Swap File #

To deactivate and delete the swap file, comply with these steps:

First, deactivate the swap area:

sudo swapoff -v /swapfile

Next, remove the swap file entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.

Finally, remove the actual swapfile file using the rm command:

sudo rm /swapfile

Conclusion #

We have now proven you easy methods to create a swap file and activate and configure swap area in your Ubuntu 20.04 system.


How do I allocate swap space in Ubuntu?

Start by creating a file which will be used for swap: sudo fallocate -l 1G /swapfile. …Only the root user should be able to write and read the swap file. …Use the mkswap utility to set up a Linux swap area on the file: sudo mkswap /swapfile


Is swap space necessary for Ubuntu?

On the off chance that you have a RAM of 3GB or higher, Ubuntu will naturally NOT USE the Swap space since it’s all that anyone could need for the OS. … You really don’t must have trade parcel, however it is suggested on the off chance that you do go through that much memory in typical activity

Does Ubuntu automatically create swap?

Truly, it does. Ubuntu consistently makes a trade parcel on the off chance that you pick programmed introduce. What’s more, it isn’t torment to include a trade segment. It is very simple to do.

How do I reduce swap space in Linux?

1. At a shell brief, execute the accompanying order to impair the trade document (where/swapfile is the trade record): # swapoff – v/swapfile.

2. Expel its entrance from the/and so on/fstab document.
3. Recover mount units so your framework enrolls the new setup: …
4. Expel the genuine record:

Does 16gb RAM need swap space?

On the off chance that you have a lot of RAM — 16 GB or somewhere in the vicinity — and you don’t require sleep yet need plate space, you could most likely pull off a little 2 GB trade parcel. Once more, it truly relies upon how much memory your PC will really utilize. Be that as it may, it’s a smart thought to have some trade space in the event of some unforeseen issue

Does Ubuntu 18.04 Need swap?

Ubuntu 18.04 LTS needn’t bother with an extra Swap parcel. Since it utilizes a Swapfile. A Swapfile is an enormous document which works simply like a Swap parcel. … In any case the bootloader might be introduced in an inappropriate hard drive and thus, you will be unable to boot into your new Ubuntu 18.04 working framework.

What is swap file Ubuntu?

Offer: Swap is a space on a circle that is utilized when the measure of physical RAM memory is full. At the point when a Linux framework comes up short on RAM, dormant pages are moved from the RAM to the trade space. … By and large when running Ubuntu on a virtual machine, a trade segment is absent, and the main alternative is to make a trade document

What happens when swap memory is full?

At the point when the framework needs more memory and the RAM is full, inert pages in the memory will be moved to the trade space. Trade isn’t a substitution to physical memory, it is only a little bit on hard drive; it must be made during the establishment. It is smarter to have trade space equivalent to 2X RAM.

How Big Should Swap be Linux?

On the off chance that RAM is under 1 GB, trade size ought to be at any rate the size of RAM and at most twofold the size of RAM. In the event that RAM is more than 1 GB, trade size ought to be in any event equivalent to the square foundation of the RAM size and at most twofold the size of RAM

We hope the How to Add Swap Space on Ubuntu 20.04 help you. If you have any query regarding How to Add Swap Space on Ubuntu 20.04 drop a comment below and we will get back to you at the earliest.

We hope this post helped you to find out  How to Add Swap Space on Ubuntu 20.04  . You may also want to see – How to Install Google Chrome Web Browser on Ubuntu 20.04

Share via
Copy link
Powered by Social Snap