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

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.

Sponsored

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

Linux Sed Tutorial: Learn Text Editing with Syntax and Examples

This article was adapted from its original version on NixCraft. Sed is an acronym for…

51 minutes ago

How to Fix VMware’s “Could not open /dev/vmmon” Error on Ubuntu

You’ve recently installed VMware Workstation on your Ubuntu system and encountered the frustrating “Could not…

8 hours ago

How to Fix Ubuntu 404 Errors While Fetching Dependencies

Have you ever found yourself staring at a terminal full of 404 errors while trying…

8 hours ago

How to Fix ‘Please Install All Available Updates’ Error When Upgrading Ubuntu 18.04 to 20.04 LTS

One particularly frustrating error that many users face when trying to upgrade from Ubuntu 18.04 …

8 hours ago

How to fix “Release is not valid yet” Error in Docker Containers

In the world of containerization, time synchronization issues can create unexpected roadblocks when working with…

8 hours ago

How to fix “Externally Managed Environment” Pip Errors on Ubuntu

If you’ve recently upgraded to Ubuntu 23.04 or newer, you might have encountered a frustrating…

8 hours ago