Create or Resize Swap file/Partition

To create a swap file or resize the swap file.

  1. Check current swap usage.
    Open a terminal and enter the following command to check if you already have a swap file or partition in use:

    swapon --show

  2. Create a new swap file (if you don't have one):

    sudo fallocate -l 2G /path/to/swapfile
    sudo chmod 600 /path/to/swapfile
    sudo mkswap /path/to/swapfile

    Replace /path/to/swapfile/partition with the actual location where you want to create the swap file (e.g. /swapfile, /Partition-ID).

  3. Activate the swap file:
    Once the swap file is created, activate it using the following command:

    sudo swapon /path/to/swapfile

    "Again, replace /path/to/swapfile/partition with the actual location of your swap file".

  4. Verify the swap.

    swapon --show

  5. Make the swap file permanent.
    By default, the swap file will be active until you reboot the system. To make it permanent, you need to add an entry in the /etc/fstab file.

    sudo nano /etc/fstab

    Add the following line at the end of the file:

    /path/to/swapfile none swap sw 0 0

    Save the file.

  • Resize the swap file.
    If you want to resize the swap file later, you can follow these steps:

    • Disable the current swap file:

      sudo swapoff /path/to/swapfile

  • Resize the swap file using the fallocate command (as shown in step 2) with a different size.

    • Enable the newly resized swap file:

      sudo swapon /path/to/swapfile

Swap files are generally used when there is not enough physical RAM available for running applications. It's essential to consider your system's specific requirements when creating or resizing swap files. Also, ensure you have enough free disk space to accommodate the swap file size.

3 Likes