How to Add Swap Memory on Ubuntu 24.04

By Anurag Singh

Updated on Dec 16, 2024

How to Add Swap Memory on Ubuntu 24.04

In this tutorial, we'll learn how to add Swap memory on Ubuntu 24.04.

Swap memory is a space on a storage drive used when the system's physical RAM is fully utilized. It acts as an overflow space to hold inactive memory pages, allowing the system to operate smoothly under high memory demands. This tutorial will guide you through adding swap memory on an Ubuntu 24.04 server, covering both basic and advanced configurations.

Prerequisites

Before getting started, ensure you have the following:

  • A KVM VPS or dedicated server with Ubuntu 24.04 OS installed.
  • A non-root user with sudo privileges.
  • Basic knowledge of Linux commands.

How to Add Swap Memory on Ubuntu 24.04

Understanding Swap Memory

Swap memory can be configured as:

1. Swap Partition: A dedicated partition on the storage device.

2. Swap File: A file on the filesystem that acts as swap space.

In modern systems, a swap file is more flexible and easier to manage, making it the preferred choice. This tutorial focuses on creating and configuring a swap file.

Check Existing Swap Space

Before adding swap memory, check if swap is already configured on your system:

sudo swapon --show

If no output is displayed, it means no swap is configured. You can also verify the system’s total memory and swap usage:

free -h

Step 1: Create a Swap File

1. Determine the Size of Swap:

Decide the size of the swap file based on your server's workload. A common rule is:

  • For systems with less than 2 GB RAM, use swap size equal to RAM.
  • For systems with 2-8 GB RAM, use half the size of RAM.
  • For systems with more than 8 GB RAM, use 4 GB or less.

2. Create the Swap File:

Use the fallocate command to create a swap file. Replace 2G with your desired swap size:

sudo fallocate -l 2G /swapfile

Verify the file creation:

ls -lh /swapfile

If fallocate is unavailable or you encounter issues, use dd:

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

Step 2: Secure the Swap File

Set the correct permissions to prevent unauthorized access:

sudo chmod 600 /swapfile
ls -lh /swapfile

The file should now be readable and writable only by the root user.

Step 3: Set Up the Swap File

Mark the File as Swap:

sudo mkswap /swapfile

You should see output confirming the swap file setup.

Enable the Swap File:

sudo swapon /swapfile

Verify the swap is active:

sudo swapon --show
free -h

Step 4: Make the Swap Permanent

To ensure the swap file is enabled at boot, add it to the /etc/fstab file:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Step 5: Configure Swap Settings (Optional)

1. Adjust Swappiness:

The swappiness parameter controls how aggressively the kernel uses swap. Values range from 0 to 100:

  • Lower values reduce swap usage (recommended for systems with sufficient RAM).
  • Higher values increase swap usage.

Check the current value:

cat /proc/sys/vm/swappiness

Temporarily set a new value (e.g., 10):

sudo sysctl vm.swappiness=10

Make it permanent by adding the setting to /etc/sysctl.conf:

echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

2. Adjust Cache Pressure:

The vfs_cache_pressure parameter controls how the system prioritizes inode/dentry caches. Lower values improve performance for file-intensive applications:

sudo sysctl vm.vfs_cache_pressure=50
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf

Step 6: Verify the Configuration

Reboot the server to ensure the swap is correctly configured and persists after reboot:

sudo reboot

After reboot, verify the swap status:

sudo swapon --show
free -h

Advanced Considerations

1. Monitoring Swap Usage:

Use htop or top to monitor swap usage in real time:

sudo apt install htop
htop

2. Zswap for Compressed Swap:

For better performance, enable zswap—a compressed cache for swap pages:

sudo vim /etc/default/grub

Add the following parameter to GRUB_CMDLINE_LINUX:

zswap.enabled=1

Update GRUB:

sudo update-grub
sudo reboot

3. Removing Swap File:

If you need to remove the swap file, first disable it:

sudo swapoff /swapfile

Remove the entry from /etc/fstab and delete the file:

sudo rm /swapfile

Conclusion

In this tutorial, we'll learnt how to add Swap memory on Ubuntu 24.04. Adding swap memory enhances system stability and performance under heavy workloads. By following the steps in this guide, you can configure swap efficiently on your Ubuntu 24.04 server, ensuring optimal resource management and adaptability to varying workloads.