Grid Guides

Explore How System On Grid Can Work For You

Grid Guide Topics

How to Add Swap Space on Ubuntu 18.04

Table Of Contents


    ## Table of Contents

    – [Introduction](#introduction
    – [Prerequisites](#prerequisites
    – [Check the System for Swap Space](#check-the-system-for-swap-space
    – [Check the Hard Disk for Available Space](#check-the-hard-disk-for-available-space
    – [How much Swap Space do I need](#how-much-swap-space-do-i-need
    – [Create a Swap File](#create-a-swap-file
    – [Enabling the Swap File](#enabling-the-swap-file
    – [Making the Swap File Permanent](#making-the-swap-file-permanent
    – [Change your Swap Settings](#change-your-swap-settings

    ## Introduction

    In this guide, I will explain to you how to add swap space on Ubuntu 18.04.

    Swap is the area on a hard disk and a part of Virtual Memory, a combination of RAM and Swap space, of your computer. Swap space is used when your machine’s physical memory (RAM is full. When the RAM is full, the system needs more memory resources, all the inactive pages in the memory will be moved to Swap space.

    It is always recommended to have a dedicated swap partition for the swap space. But, the swap space can also be a swap file or a combination of swap partitions and swap files.

    Swap space gives your server the ability to store more amount of data in its working memory but with a few cautions. And, this space will only be used when the memory space in RAM is not sufficient for data.

    The information in the Swap space is comparatively slower than the information in the RAM. And, the operating system will use swap space to store the older data.

    Adding a swap space to your applications is the best and easiest way to increase the responsiveness of your server and protect against memory errors in the applications.

    ## Prerequisites

    A Running Server

    ## Check the System for Swap Space

    Make sure to check whether your system has swap space already or not. You can check it by using the following command.
    “`
    $ sudo swapon –show
    “`
    Output:

    ![swaponshow](https://grid.media/assets/images/swapon-show.png

    If you get the header of the table, as shown above, it means you do not have any swap space in your system.

    And, another way to check space is by using the free utility, which shows the memory usage of your system. We can know the swap usage and current memory by using the below command.
    “`
    $ free -h
    “`
    Then the output looks like this:

    ![](http://

    Now, you can see the above output. Your swap space in your system is 0.

    ## Check the Hard Disk for Available Space

    Also, check your hard disk for the current disk usage. You can use the below command to know that.
    “`
    $ df -h
    “`

    ![dfh](https://grid.media/assets/images/available-space-in-hard-disk.png

    Now, you can see the first line in the above table that the hard disk partition has 1GB of available memory. It means we have an adequate amount of space to work with, however, your actual usage might be different.

    ## How much Swap Space do I need

    The appropriate size of your swap space depends on the requirement of your applications and your personal choice. But, it is recommended to have the size of your swap space equal to or double the space of RAM on your system.

    For example, your system has 8 GB of RAM. Now, it is recommended to have either 8GB or 16GB of swap space.

    ## Create a Swap File

    Now, as you know the amount of available space in your hard disk, you can create a swap file within your file system.

    You must create a file, swapfile, in your root directory. This file must be allocated the amount of space that you wish to. This can be done using the following method.

    The swap file can be created by using fallocate programme. This command generates a file with a specified size.

    For example, a server has 1G of RAM, we will create a 1G file in this guide.
    “`
    $ sudo fallocate -l 1G /swapfile
    “`
    We can verify the amount of space reserved by the following command.
    “`
    $ sudo ls -lh /swapfile
    “`

    Output:

    ![](http://

    Now the file is created with the required amount of space.

    ## Enabling the Swap File

    Before enabling the swap file, you need to change the settings so that no one can read the file except the root user. We can restrict other users from reading or editing the swap file by using the following command.
    “`
    $ sudo chmod 600 /swapfile
    “`
    To verify the permissions whether they are changed or not, use the following command.
    “`
    $ sudo ls -lh /swapfile
    “`
    Now you can see the root user has read and write flags enabled. Now we can mark the file as swap space by entering the below command.
    “`
    $ sudo mkswap /swapfile
    “`
    We can enable the swap file by allowing the system to start utilizing it, after marking the file.
    “`
    $ sudo swapon /swapfile
    “`
    Enter the following command to verify swap is available or not.
    “`
    $ sudo swapon –show
    “`
    You can verify the output of free utility by entering the following command.

    “`
    $ sudo free -h
    “`
    Successfully set up swap and our operating system will begin to use it if required.

    ## Making the Swap File Permanent

    Now the swap file is enabled, but if you reboot then swap file is disabled unless you made it permanent. We can change it by adding this swap file to /etc/fstab File.

    Enter the following command to back up the /etc/fstab file.
    “`
    $ sudo cp /etc/fstab /etc/fstab.bak
    “`
    Now edit the swap file at the end of this file by entering the following command.
    “`
    $ sudo echo ‘/swapfile none swap sw 0 0’| sudo tee -a /etc/fstab
    “`
    ## Change your Swap Settings

    The swappiness parameter, a value between 0 and 100 represents the percentage, configures how frequently your system swaps the data out of the RAM to the swap space.

    If the value is closer to 0, it means the kernel will not swap the data to the hard disk unless it is required.

    If the value is closer to 100, it means the kernel will put more data into swap to make more RAM space free.

    The ideal for a server is close to 0. So, set your swappiness value to 10 by using the below command.
    “`
    $ sudo sysctl vm.swappiness=10
    “`
    Make this swap file permanent by entering the following command.
    “`
    $ sudo nano /etc/sysctl.conf
    “`
    Then, add the next line at the end of the file.
    “`
    $ sudo vm.swappiness=10
    “`