How to Create and Manage a Swap File in Ubuntu Linux

When you spin up a budget-friendly cloud server on DigitalOcean, AWS, or Linode, you typically start with a modest 1GB or 2GB of RAM. While this is sufficient for basic tasks, attempting to compile software, run a hungry database, or suddenly experiencing a surge in web traffic will rapidly exhaust your physical memory. When Ubuntu runs completely out of RAM, the kernel aggressively steps in and begins terminating processes to survive, often killing your web server or database in the process. To prevent these catastrophic out-of-memory (OOM) crashes without paying for a hardware upgrade, you need to learn how to create a swap file in Ubuntu Linux.

What is Swap Space and Why Do You Need It?

Swap space is essentially an emergency overflow buffer created on your physical hard drive (or SSD). When your system’s RAM is entirely full, the Linux kernel identifies inactive memory pages (data belonging to applications that have not been used recently) and quietly moves them from the fast RAM onto the slower swap space. This frees up RAM for the critical applications that actively need it.

Historically, system administrators created a dedicated swap partition during the initial installation of the operating system. However, modifying disk partitions after the system is live is incredibly risky. Creating a swap file provides the exact same benefits, but it is just a standard file sitting on your existing filesystem, meaning you can easily create, resize, or delete it at any time without risking data loss.

Step 1: Check Existing Swap Space

Before creating a new file, verify whether your system already has swap configured.

  1. Open your terminal or SSH into your server.
  2. Type the command: sudo swapon --show and press Enter.
  3. If the command returns nothing, you do not have any active swap space.
  4. Alternatively, you can type free -h. Look at the bottom row labelled “Swap.” If it reads “0B” across all columns, you are ready to proceed.

Step 2: Create the Swap File

For this example, we will create a 2GB swap file. A general rule of thumb for modern systems is to create a swap file equal to your total RAM (up to 4GB). If you need an 8GB swap file, you would substitute “2G” with “8G” in the command below.

  1. We will use the fallocate program to instantly carve out space on the hard drive. Type: sudo fallocate -l 2G /swapfile
  2. Verify the correct amount of space was reserved by checking the file size: ls -lh /swapfile. You should see it listed as 2.0G.

Step 3: Secure the Swap File

By default, the newly created file might be readable by standard users on the system. Because swap space can contain highly sensitive information dumped from RAM (including passwords and encryption keys), you must lock down its permissions so only the root user can access it.

  1. Type the command: sudo chmod 600 /swapfile and press Enter.
  2. Verify the permissions by typing: ls -lh /swapfile. The output should begin with -rw-------, indicating only root has read and write access.

Step 4: Format and Enable the Swap File

Now that the file is created and secured, you must format it specifically so the Linux kernel recognises it as swap space, and then activate it.

  1. Format the file by typing: sudo mkswap /swapfile. The terminal will output a UUID confirming the file is ready.
  2. Turn the swap space on by typing: sudo swapon /swapfile.
  3. Verify it is working by typing sudo swapon --show or free -h. You should now see your 2GB allocation listed.

Step 5: Make the Swap File Permanent (fstab)

If you reboot your server right now, the swap file will not turn back on automatically. You must instruct the system to mount it during the boot sequence by editing the file systems table (fstab).

  1. Before editing, create a backup of the fstab file in case you make a mistake: sudo cp /etc/fstab /etc/fstab.bak
  2. Echo the swap file configuration directly into the fstab file by typing:
    echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Your swap file is now permanent and will survive server reboots.

Best Practices: Adjusting Swappiness

The “swappiness” parameter dictates how aggressively the Linux kernel will move data out of RAM and into the swap file. Because hard drives are significantly slower than RAM, relying too heavily on swap will make your system feel sluggish. The value ranges from 0 to 100.

  • A value of 100 tells the kernel to aggressively use the swap file at all times.
  • A value of 10 tells the kernel to avoid using the swap file until RAM is almost completely exhausted.

For a typical desktop or cloud server, a swappiness of 10 is generally recommended to preserve performance. To check your current swappiness, type: cat /proc/sys/vm/swappiness (the default in Ubuntu is usually 60).

By implementing a swap file, you provide your Ubuntu system with a critical safety net, ensuring that sudden spikes in resource demand result in slight performance slowdowns rather than catastrophic, data-destroying crashes.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.

Receive our best articles and tips delivered straight to your inbox.