How to Add SWAP on Ubuntu 18.04/20.04

03 September 2020 | Category : Ubuntu

How to Add SWAP on Ubuntu 18.04/20.04

SWAP is a virtual memory on Linux which it’s used disk space as a memory and the swap will active or used when the size amount of RAM or memory is full of process. In some cases Cloud Provider when you creating a new VM the default swap is not set, if you want to activate the swap you must create the partition of swap first.

The swap will helpfully the Server when the Server memory is full. When some process is running and the memory in condition full of process, the new process or old process will move to swap. So, in this article we share about How to Add SWAP on Ubuntu 18.04/20.04, to do that you can check this step.

Add SWAP on Ubuntu 18.04/20.04

Before we start to continue to create a swap on your Ubuntu, you can check first the swap is already enabled or not. To do that you can follow this command to checking on your environment :

sudo swapon --show

If the command above nothing showing the output it’s mean the swap is still disabled or not created. Or you can check with this command :

sudo free -m

If the resulting output of the above command like this :

sudo free -m
              total        used        free      shared  buff/cache   available
Mem:            985          90         267           0         627         725
Swap:             0           0           0

It’s mean the result same of command on before, where the swap is still disabled or not created.

Create a SWAP

After you ensure if the swap is still disabled, now you can create the swap with following this step. But, you must determine the size of the swap first. Usually, the swap size is 2x of memory or the same size of memory but in this case, if your environment ubuntu has memory 1 GB you can set the swap just 1 GB. To do create a swap this is the step by step.

  1. Create a swap file

To create a swap file you can run this command :

sudo fallocate -l 1G /swapfile

Or if the command fallocate showing error, you can use another command to create the swapfile which it can be created with command dd like this :

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

And if successfully created a swapfile will show the output this :

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
1048576+0 records in
1048576+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 3.25036 s, 330 MB/s
  1. Change permission swapfile

Then, if the swapfile has been created you can set the permission of that file to 600 with this command :

sudo chmod 600 /swapfile
  1. Set the swap area

Now, you can set the swap area on file swapfile with command mkswap, to run that command you can follow this format command :

sudo mkswap /swapfile

And the above command will show the output this :

sudo mkswap /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=5c3eb91c-ff12-4070-a819-be5f3eea2162
  1. Enable the swap

After the swap area has been set, then you can activate the swap with this command :

sudo swapon /swapfile

The above command means you activate the swap on the swapfile. To permanently activate the swap you can set to the file /etc/fstab with this command :

sudo vim /etc/fstab

At the end of line you can add this line configuration :

/swapfile	swap		swap	defaults	0 0
  1. Verify the configuration

And the last step to verify the configuration you need reboot your Ubuntu server with command :

sudo shutdown -r now

After your ubuntu has been rebooted you can run this command to check if the swap has been enabled :

sudo swapon --show

And the command above will be showing the output this :

sudo swapon --show
NAME      TYPE  SIZE USED PRIO
/swapfile file 1024M   0B   -2

Or you can run this command to check :

sudo free -m

And the command will show this output :

sudo free -m
              total        used        free      shared  buff/cache   available
Mem:            985          73         723           0         187         774
Swap:          1023           0        1023

The two above commands to verify the swap it shows the same result in which the swap has been enabled.

Set Adjusting Swappiness Value

Swappiness is the Linux Kernel Property which this property defines how often the Linux system will use the swap space. The swappiness has a value for adjusting between 0 until 100, when you set a lower value it will make the kernel try to avoid swapping whenever possible but if you set a bigger value it will make the kernel more aggressive to swapping use swap space.

By default, the swappiness has adjusted at value 60, to check it you can run with the command :

sudo cat /proc/sys/vm/swappiness

To change the value and define the swappiness value a lower or bigger, you can edit the configuration on file /etc/sysctl.conf with the command :

sudo vim /etc/sysctl.conf

At the end of the file you can add this configuration line :

vm.swappiness=10

Or you can add the line configuration with command :

sudo echo “vm.swappiness=10” >> /etc/sysctl.conf

To verify the configuration you can run the command :

sudo sysctl -p

And the command will show the output :

sudo sysctl -p
vm.swappiness = 10

Conclusion

SWAP is the virtual memory in Linux System, this swap is an important partition and must have in every Linux system to help the memory when processing some process which is running. With the swap as virtual memory, it will make the memory have the best performance because the swap will be helping memory to process the other process which will be running

Share this article :