• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Add Swap Space on Ubuntu 20.04

Written by Admin, Updated On May 16, 2020
ram, swap, ubuntu
How to Add Swap Space on Ubuntu 20.04

Swap is a reserved space on a hard drive that is used when the physical RAM memory becomes full. When there is no longer sufficient space in RAM to hold in-use application data then Swap space will be used. This tutorial explains how to add a swap space on Ubuntu 20.04 Focal Fossa.

The information written to disk will be significantly slower than information kept in RAM. Overall, having a swap space can be good when your system’s RAM is depleted and throw out-of-memory exceptions on systems.

Make sure you are logged in as root or user with sudo privileges.

Step 1 – Checking Swap Information#

Before you start, Check if the system already has some swap space available. You can check it by running below command:

sudo swapon --show

If the output is empty, it means your system don’t have swap space.

Step 2 – Creating a Swap File#

In this tutorial, we will create 1 GB swap file. You can replace 1G with the size of the swap space you need.

Create a file which will be used as swap:

sudo fallocate -l 1G /swapfile

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:

We can verify that the correct amount of space was reserved by typing:

ls -lh /swapfile
-rw-r--r-- 1 root root 1.0G Apr 25 11:14 /swapfile

Now our file has been created with the correct amount of space set aside.

Step 3 – Set Permission#

We need to lock down the permissions of the file so that prevent regular users to write and read the file:

sudo chmod 600 /swapfile

Step 4 – Enabling the Swap File#

Create a Linux swap area on the file:

sudo mkswap /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=4e665809-2gb3-370f-aed6-5627e74089dbf

After that, enable the swap file, allowing our system to start using it:

sudo swapon /swapfile

Step 5 – Make Swap File Permanent#

Our previous changes have enabled the swap file for the current session only. To make the change permanent edit the /etc/fstab file:

sudo nano /etc/fstab

Append the swap file information at the end of your /etc/fstab file:

/swapfile swap swap defaults 0 0

Step 6 – Tuning your Swap Settings#

At this point, we will make few configuration which will have an impact on your system’s performance while dealing with swap.

Adjusting the Swappiness Value#

The swappiness parameter defines how often your system swaps data out of RAM to the swap space. It can have a percentage value between 0 and 100.

A higher value will make the kernel to try to put more data in to swap aggressively, while a low value will make the kernel to try to avoid swapping whenever possible.

You can check the current swappiness value by typing:

cat /proc/sys/vm/swappiness

By default, Ubuntu system have swappiness value is 60. It’s OK for desktop user, for a production server, it might be lower.

We can change this swappiness value using sysctl command. For instance, to set the swappiness value to 15, type:

sudo sysctl vm.swappiness=15
vm.swappiness = 15

This change will persistent until next reboot. To make this change permanent append the following line to the /etc/sysctl.conf file:

sudo nano /etc/sysctl.conf
vm.swappiness=15

Save and close the file.

Removing a Swap File#

Perform the following steps to deactivate and remove swap file.

First, deactivate the swap space:

sudo swapoff -v /swapfile

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

At last, delete the physical swapfile file using the rm command:

sudo rm /swapfile

Conclusion#

This tutorial shown you how to create a swap file and configure swap space on your Ubuntu 20.04 system.

If you have any problem or feedback, please leave a comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Install Yarn on CentOS 8
Next Article   How to Install And Use Tmux

Related Posts

  • How to Install Apache, MySQL, PHP (LAMP) on Ubuntu 22.04

    How to Install LAMP on Ubuntu 22.04

    March 20, 2023
  • How to Install LEMP Stack on Ubuntu 22.04

    How to Install LEMP Stack on Ubuntu 22.04

    March 18, 2023
  • How to Install Memcached on Ubuntu 22.04

    How to Install Memcached on Ubuntu 22.04

    March 16, 2023

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook