Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Tutorials»How to Add Swap Space in Ubuntu 18.04

    How to Add Swap Space in Ubuntu 18.04

    RahulBy RahulJuly 23, 20152 Mins ReadUpdated:March 15, 2021

    Swap is very useful for that system which required more RAM that physical available. If memory is full and system required more RAM to run applications properly it check for swap space and transfer files there. In general terms, swap is a part of the hard disk used as RAM on the system.

    I have a virtual machine running which don’t have swap on it. Many times services got crashed due to insufficient memory. In this situation creation of Swap file is better to keep them up. This article will help you to create a swap file on Linux system after installation.

    • Check System Swap – Before working make sure that system has already swap enabled. If there is no swap, you will get output header only.
      sudo swapon -s
      
    • Create Swap File – Lets create a file to use for swap in system of required size. Before making file make sure you have enough free space on disk. Generally, it recommends that swap should be equal to double of installed physical memory.

      I have 2GB memory in my system. So I am creating swap of 4GB in size.

      sudo fallocate -l 4G /swapfile
      chmod 600 /swapfile
      
    • Make It Swap – Now make is swap usable file using mkswap command.
      sudo mkswap /swapfile
      
    • Enable Swap – Now setup the swap for system using swapon command.
      sudo swapon /swapfile
      

      Now again check that swap is enabled or not. You will see results something like below.

      sudo swapon -s
      
      Filename                Type        Size    Used    Priority
      /swapfile               file        4194300 0       -1
      
    • Setup Swap Permanent – Append the following entry in /etc/fstab file to enable swap on system reboot.
      vim /etc/fstab
      
      /swapfile   none    swap    sw    0   0
      
    • Setup Kernel Parameter – Now change the swappiness kernel parameter as per your requirement. It tells the system how often system utilize this swap area.

      Edit /etc/sysctl.conf file and append following configuration in file.

      sudo vim /etc/sysctl.conf
      
      vm.swappiness=10
      

      Now reload the sysctl configuration file

      sudo sysctl -p
      

    At this point, you have successfully enabled swap on your Ubuntu system.

    swap swap memory swapfile swappiness
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Remove/PURGE BINARY LOGS in MySQL & MariaDB
    Next Article How to Add Swap on CentOS & RHEL 8/7/6

    Related Posts

    What is the /etc/aliases file

    2 Mins Read

    What is the /etc/nsswitch.conf file in Linux

    2 Mins Read

    How to Setup Squid Proxy Server on Ubuntu and Debian

    Updated:June 17, 20225 Mins Read

    How to Delete a Let’s Encrypt Certificate using Certbot

    Updated:June 3, 20222 Mins Read

    How to Install Latest Git on Ubuntu 22.04

    Updated:May 31, 20222 Mins Read

    How To Install LibreOffice on Ubuntu 22.04

    Updated:May 23, 20222 Mins Read

    5 Comments

    1. Cayman on July 10, 2020 5:31 pm

      Many thanks for this.

      Reply
    2. Adam on October 26, 2019 1:54 pm

      Very useful, thanks a lot!

      Reply
    3. Adam Muiz on December 2, 2018 5:17 am

      Thanks dude, i really love use ubuntu
      thats easy to use

      Reply
    4. GW on January 16, 2018 4:35 pm

      Have partition on usb formatted as Linux swap, but it is not recognized by the Linux running in VBox. May be anyone has a suggestion ?

      Reply
    5. Paul on November 29, 2017 8:34 am

      This was very helpful to me. Thank you!

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.