How to Add Swap Memory in Ubuntu 26.04

Updated on 24 April, 2026
Add and configure swap memory on an Ubuntu 26.04 using a swap file with automatic mounting at boot and swappiness tuning adjustments.
How to Add Swap Memory in Ubuntu 26.04 header image

Swap memory is a designated area on a storage device that the operating system uses as virtual memory when the physical RAM is fully utilized. Instead of terminating processes when memory runs out, the system moves less active data from RAM to the swap space, keeping the server stable under high memory pressure.

This article explains how to add swap memory on an Ubuntu 26.04 server using a swap file, configure the system to mount it automatically at boot, and tune the swappiness value to control how aggressively the system uses swap.

Prerequisites

Before you begin, you need to:

View Existing Swap Memory

Ubuntu 26.04 servers may include a default swap allocation. Check the current memory status before adding new swap space.

View the available RAM and swap memory on the server.

console
$ free -h

The output displays the total, used, and free memory for both RAM and swap. Note the current swap size before proceeding.

Create a Swap File

A swap file is a regular file on the server's filesystem that functions as swap space. The following steps create a 2 GB swap file, set the correct permissions, and format it for use as swap.

  1. Create a 2 GB swap file in the root directory. Adjust the size as needed for your workload.

    console
    $ sudo fallocate -l 2G /swapfile
    
  2. Restrict the file permissions to allow only the root user to read and write.

    console
    $ sudo chmod 600 /swapfile
    
  3. Format the file as swap space.

    console
    $ sudo mkswap /swapfile
    

    The output displays the swap size and a UUID for the new swap space.

Enable Swap Memory

The swapon command activates swap space for immediate use. The following steps enable the swap file and verify that the system recognizes it.

  1. Enable the swap file.

    console
    $ sudo swapon /swapfile
    
  2. Verify that the new swap space is active.

    console
    $ sudo swapon --show
    

    The output lists all active swap entries including the new /swapfile with its size and type.

  3. Confirm the updated memory allocation.

    console
    $ free -h
    

    The swap total should reflect the additional space from the new swap file.

Configure Automatic Mounting at Boot

The File System Table (/etc/fstab) defines which filesystems are mounted automatically when the server starts. Without an fstab entry, the swap file is lost after a reboot.

  1. Back up the current fstab configuration.

    console
    $ sudo cp /etc/fstab /etc/fstab.bak
    
  2. Append the swap file entry to the fstab configuration.

    console
    $ echo '/swapfile swap swap defaults 0 0' | sudo tee -a /etc/fstab
    

    Within the configuration:

    • /swapfile: Specifies the path to the swap file.
    • swap swap: Sets the filesystem type and mount point for swap.
    • defaults: Applies the default mount options.
    • 0 0: Disables backup and filesystem check for the swap entry.

Configure Swappiness

The swappiness value controls how aggressively the kernel moves data from RAM to swap. The value ranges from 0 to 100:

  • 0: The system avoids swapping unless absolutely necessary.
  • 10-30: Low swapping, suitable for servers with sufficient RAM.
  • 50: Balanced approach between RAM and swap usage.
  • 60: The default value on most Ubuntu systems.
  • 100: The system swaps aggressively, moving data to swap even when RAM is available.
  1. View the current swappiness value.

    console
    $ cat /proc/sys/vm/swappiness
    
  2. Set a custom swappiness value. For example, 10 for a server workload that should prioritize RAM.

    console
    $ echo "vm.swappiness = 10" | sudo tee -a /etc/sysctl.conf
    
  3. Apply the configuration change.

    console
    $ sudo sysctl -p
    

Remove Swap Memory

The swapoff command deactivates swap space. The following steps disable the swap file and remove it from the system.

  1. Disable the swap file.

    console
    $ sudo swapoff /swapfile
    
  2. Verify that the swap file is no longer active.

    console
    $ sudo swapon --show
    
  3. Remove the swap file entry from /etc/fstab to prevent mounting at boot.

    console
    $ sudo nano /etc/fstab
    

    Delete the /swapfile swap swap defaults 0 0 line. Save and close the file.

  4. Delete the swap file from the filesystem.

    console
    $ sudo rm /swapfile
    

Conclusion

You have added swap memory on an Ubuntu 26.04 server using a swap file, configured automatic mounting through fstab, and tuned the swappiness value. Swap memory provides a safety net against out-of-memory conditions, though it operates slower than physical RAM. For more options, run man swapon to view the swap management manual.

Comments