Contact Info

8 The Green Ste R, Dover, DE 19901

(+1) 470 - 809-9620

[email protected]

Get Started
Blog Post

    As your applications grow and you start running more demanding tasks on your Linux server, you might find yourself needing more memory. A simple and efficient solution to this is creating a swap file. Swap files are a dedicated area on your storage that the operating system can use when physical RAM is full. This guide will walk you through the steps to create a swap file on a Linux system.

    What is a Swap File?

    Swap space in Linux is used when the amount of physical RAM (Random Access Memory) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. Swap space can be a dedicated swap partition or a swap file. Using a swap file is often more flexible than a swap partition because it can be resized as needed.


    • A Linux-based system, we did this how to on a Cloud VPS (Ubuntu, CentOS, Debian, etc.) or any Linux System like a Dedicated Server
    • Root or sudo privileges

    Step-by-Step Guide to Create a Swap File

    Step 1: Check for Existing Swap Space

    Before creating a new swap file, it’s good to check if any swap space is already enabled. You can do this by running the following command, connected to your server terminal.

    sudo swapon --show

    If there is no output, it means there is no swap space currently enabled.

    Step 2: Create a Swap File

    You need to create a file that will be used for swapping. In this example, we’ll create a 2GB swap file, but you can adjust the size based on your system’s requirements. As a general guideline, it’s often recommended to allocate swap space equal to half of your system’s RAM. However, we suggest creating only a 2GB swap file. Why? Because if your server requires more than 2GB of swap space, it’s likely time to upgrade your VPS or Dedicated Server specifications instead of relying on slower swap memory. Even with NVMe drives, RAM is still significantly faster!

    sudo fallocate -l 2G /swapfile

    If fallocate is not available on your system, you can use dd instead:

    sudo dd if=/dev/zero of=/swapfile bs=1M count=2048

    Step 3: Secure the Swap File

    The swap file should only be accessible by the root user. Set the correct permissions using

    sudo chmod 600 /swapfile

    Step 4: Mark the File as Swap Space

    We need to mark the file we just created as swap space using the mkswap command in Linux.

    sudo mkswap /swapfile

    The result of this command will be something like that:

    Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
    no label, UUID=9e03f576-d64c-4493-8624-5343d6e30e8b

    Step 5: Enable the Swap File

    To start using the swap file, we will have to enable it with the swapon command

    sudo swapon /swapfile

    Step 6: Verify the Swap File

    To verify that the swap file is active, we need to run

    sudo swapon --show

    You should see output similar to:

    /swapfile file 2G 0B -2

    Step 7: Make the Swap File Permanent

    To ensure that the swap file is used after a reboot, add it to the /etc/fstab file. Open the file with your preferred text editor

    sudo nano /etc/fstab

    Add the following line at the end of the file

    /swapfile none swap sw 0 0

    Step 8: Adjust Swappiness (Optional)

    Swappiness is a property that defines how often the swap space is used. It’s a value between 0 and 100. Lower values will make the kernel avoid swapping as much as possible, while higher values will make it use the swap space more aggressively. The default value is 60. To check the current swappiness value try the following command.

    cat /proc/sys/vm/swappiness

    To temporarily set swappiness to a different value (e.g., 10) type in your terminal:

    sudo sysctl vm.swappiness=10

    To make this change permanent, add the following line to /etc/sysctl.conf by using your favorite editor.

    nano /etc/sysctl.conf 

    Step 9: Adjust Cache Pressure (Optional)

    You might also want to adjust the `vfs_cache_pressure` setting, which controls the tendency of the kernel to reclaim the memory used for caching directory and inode objects. The default is 100. A lower value means the kernel will retain these caches more aggressively.

    You can check the current value by using the following command in your terminal:

    cat /proc/sys/vm/vfs_cache_pressure

    To temporarily set it to a different value (e.g., 50), type in your terminal:

    sudo sysctl vm.vfs_cache_pressure=50

    To make this change permanent, add the following line to /etc/sysctl.conf:



    Creating a swap file in Linux is a straightforward process that can greatly enhance the performance of your system when running out of RAM. By following these steps, you can easily create and configure a swap file to suit your needs. Whether you’re running a server with Gozen Host or managing your local machine, adding swap space can help ensure smooth operation under heavy loads.

    Share this Post