How to add swap space on Ubuntu 18.04

s2 0

Swap is space on a hard drive that is used when the physical RAM memory is full. When a Linux system runs out of memory, inactive pages are moved from memory to the swap area.

Swap space can either take the form of a dedicated swap partition or a swap file. In general, when running Ubuntu on a virtual machine, there is no swap partition and the only option is to create a swap file.

This tutorial covers the steps required to add a paging file on Ubuntu 18.04 systems.

before you start

Before proceeding with this tutorial, check to see if swap is already enabled in your Ubuntu installation by typing:

sudo swapon --show

If the output is blank, it means that no swap space is activated on your system.

Otherwise, if you get something like the one below, you have already enabled swap on your computer.

NAME      TYPE      SIZE USED PRIO
/dev/sda2 partition 1.9G   0B   -2

While possible, it is not common to have multiple swap areas on a single computer.

Create a swap file

The user you are logged in with must have sudo permissions to enable swap. In this example we will add 1G To deceive. If you want to add more swap replace it 1G with the amount of swap space you need.

Follow the steps below to add swap space on Ubuntu 18.04.

  1. Start by creating a file that will be used for the swap:

    sudo fallocate -l 1G /swapfile

    if fallocate is not installed or you receive an error message stating fallocate failed: Operation not supported Then use the following command to create the paging file:

    sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
  2. Only the root user should be able to write and read the paging file. Set the correct permissions by typing:

    sudo chmod 600 /swapfile
  3. Use the mkswap Utility to set up a Linux swap area for the file:

    sudo mkswap /swapfile
  4. Activate the paging file with the following command:

    sudo swapon /swapfile

    To make the change permanent, open that /etc/fstab File:

    sudo nano /etc/fstab

    and insert the following line:

    / etc / fstab
    /swapfile swap swap defaults 0 0
  5. Make sure 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 1024M 507.4M   -1
    sudo free -h
                  total        used        free      shared  buff/cache   available
    Mem:           488M        158M         83M        2.3M        246M        217M
    Swap:          1.0G        506M        517M

Adjusting the swap value

Swappiness is a property of the Linux kernel that defines how often the system uses the swap area. Swappiness can have a value between 0 and 100. A lower value will cause the kernel to try to avoid swapping whenever possible, while a higher value will cause the kernel to use the swap area more aggressively.

The default value for swappiness is 60. You can check the current swappiness value by entering the following command:

cat /proc/sys/vm/swappiness
60

While the swappiness value of 60 is fine for most Linux systems, you may need to set a lower value for production servers.

For example, to set the swappiness value to 10, do the following:

sudo sysctl vm.swappiness=10

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

/etc/sysctl.conf
vm.swappiness=10

The optimal swappiness value depends on your system utilization and the use of memory. You should adjust this parameter in small steps to find an optimal value.

Remove a paging file

Proceed as follows to deactivate and remove the paging file:

  1. Start by disabling the swap area by typing:

    sudo swapoff -v /swapfile
  2. Next, remove the paging file entry /swapfile swap swap defaults 0 0 of the /etc/fstab File.

  3. Finally, remove the actual paging file with the rm
    Command:

    sudo rm /swapfile

diploma

You have learned how to create a paging file and how to enable and configure the paging area on your Ubuntu 18.04 system.

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