© 2021 www.richardwalz.com
Richard Walz
All rights reserved.

How to create a swap file in Ubuntu Linux


Have you ever wondered how to create a Linux Swap File? Well here is how to do it quickly.

I generally set the swapfile to at least 2GB+ more space then the memory (ram) size.

Machine Memory (RAM) SpecificationsSwap File Size

Create the swap-file

  1. Create the swap-file size you want (8GB demonstrated)
    • sudo fallocate -l 8G /swapfile
  2. Set the permissions of the swap-file
    • sudo chmod 600 /swapfile
  3. Formats the swap-file directory
    • sudo mkswap /swapfile
  4. Enables the swap-file
    • sudo swapon /swapfile
  5. Configures the swap-file to be used on startup
    • sudo sh -c ‘echo “/swapfile none swap sw 0 0” >> /etc/fstab’
  6. Verify your swap file configuration is correct
    • cat /etc/fstab
  7. If you see the following “/swapfile none swap sw 0 0” on the last line you completed the steps correctly.