Categories: UbuntuUbuntu Feed

Adding a Swap File to Ubuntu 24.04: A Complete Tutorial

Are you dealing with low RAM on your Ubuntu system? This is a common issue on systems required more memory to running applications smoothly. This can be resolved by adding more memory, but that involves additional costs. If you don’t want to spend extra money, you can easily create a swap file and add it to your system to avoid low memory problems. If you are facing issue first time you can also try clearing the cached memory.

What is Swap Space

Sponsored

A swap file in Linux is the space allocated on disk utilized by system on low RAM. When RAM fills up, inactive data gets moved to this file on your disk, freeing up RAM for important processes. It’s like using extra space on your desk when your main workspace gets cluttered.

The Swap space helps your system for:

  • Performance: On systems with limited RAM, swap space can prevent the system from crashing under heavy load.
  • Flexibility: Easily adjust the amount of virtual memory available without needing to physically add RAM.

Prerequisites

  • A system running Ubuntu 24.04
  • Sufficient disk space for the desired swap file size
  • Root or sudo privileges

Step 1: Checking the System for Existing Swap Space

Before adding a new swap file, it’s good practice to check whether your system already has some swap configured. Linux offers a few tools to see the swap details. Try swapon command for active swap details, or free to see total and used swap space

sudo swapon --show

The command will return nothing in case of no swap added to system.

Step 2: Creating a Swap File

  1. Choose the Size:
  2. First of all, you need to decided the size of swap space required on your system. Generally it is recommended to equal size or double the amount of RAM, depending on your system usage and available disk space.

  3. Create the File: You can use fallocate command to create a swap file with your desired size. For example, to create a 4GB swap file, execute:
    sudo fallocate -l 4G /swapfile
    
  4. Sponsored
  5. Set Proper Permissions: Set the correct permissions on your swap file to ensure it’s not readable by anyone except the root user:
    sudo chmod 600 /swapfile
    

Step 3: Enabling the Swap File

  1. After creating the swap file, we need to format it as Swap:
    sudo mkswap /swapfile
    
  2. Now, you can enable the Swap file:
    sudo swapon /swapfile
    
  3. Verify the swap is active on your system:
    sudo swapon --show
    

    This command should now show your newly created swap file.

Step 4: Making the Swap File Permanent

You have already enabled swap memory on your system. However, you also need to make sure that the swap file turns on automatically when your system starts. To do this, add it to the system’s /etc/fstab file.

  1. Edit /etc/fstab file in your favorite text editor:
    sudo nano /etc/fstab
    
  2. Append the following line to /etc/fstab:
    
    /swapfile none swap sw 0 0
    
    
  3. Save and close the file.

Step 5: Adjusting Swappiness

The swappiness parameter controls how often your system uses swap space. You can adjust it to tune your system’s performance with respect to RAM and swap usage.

  1. Check Current Swappiness Value:
    cat /proc/sys/vm/swappiness
    
  2. Change Swappiness Value (e.g., to 20, which is a recommended value for desktop systems):
    sudo sysctl vm.swappiness=20
    
  3. Make It Permanent by adding vm.swappiness=20 to /etc/sysctl.conf.

Conclusion

You’ve successfully added and configured a swap file on your Ubuntu 24.04 system. This addition should help in managing your system’s resources more effectively, especially under heavy load. Remember, while swap can help in managing memory pressure, it’s not a substitute for adding more physical RAM when consistently facing performance issues.

The post Adding a Swap File to Ubuntu 24.04: A Complete Tutorial appeared first on TecAdmin.

Ubuntu Server Admin

Recent Posts

Migrating from CentOS to Ubuntu: a guide for system administrators and DevOps

Photo by Sonja Langford, Unsplash CentOS 7 is on track to reach its end-of-life (EoL)…

1 day ago

Install LibreOffice Office Suite in Ubuntu 24.04

This is a beginner’s guide shows how to install (or Remove) the popular LibreOffice Office…

1 day ago

Canonical at Dell Technologies World 2024

Canonical, the company behind Ubuntu and the trusted source for open source software, is thrilled…

2 days ago

Deploy Kubernetes Cluster on Ubuntu VPS

This article provides a guide for how to deploy a Kubernetes cluster on Ubuntu VPS.…

2 days ago

Enable Automatic Login in Ubuntu 24.04 & Flavors

This simple tutorial shows how to automatically login a user account in Ubuntu 24.04 and…

3 days ago

Ubuntu Pro for EKS is now generally available

May 14, 2024 – Canonical, the publisher of Ubuntu, is delighted to announce the general…

4 days ago