swap

How To Add Swap on Ubuntu 24.04

In today’s digital landscape, managing system resources efficiently is paramount, especially for servers and workstations that demand high performance. One effective way to maximize your system’s memory capabilities is by utilizing swap space. Swap acts as an overflow area for your system’s RAM, allowing your applications to run smoothly even when physical memory is scarce.

In this blog post, we will guide you through the steps to add swap space on Ubuntu 24.04, whether you prefer a swap file or a swap partition. Let’s dive in!

What is Swap Space?

Swap space is an area on your hard drive that serves as virtual memory. When the RAM is fully utilized, the system will start using the swap space to free up memory. This process can prevent crashes and slowdowns, but it is worth noting that accessing data from the swap is significantly slower than RAM.

Why Use Swap?

  1. Preventing Memory Shortage: Swap allows for smooth multitasking by providing additional memory when RAM is full.
  2. Improving System Stability: It helps to manage spikes in memory consumption and keeps your applications running.
  3. Hibernation Support: Swap is necessary if you want to use the hibernation feature on your laptop.

Checking Existing Swap Space

Before we add more swap space, let’s check the existing swap configuration on your Ubuntu system. Open a terminal and execute the following command:

sudo swapon --show

If no output is returned, it means that there is currently no active swap space.

You can also check the current memory and swap usage with:

free -h

Adding Swap Space in Ubuntu 24.04

Option 1: Creating a Swap File

Creating a swap file is the most straightforward method. Here are the steps:

  1. Create a Swap File

Choose the swap size you need — for example, 2 GB. You can create this file with the following command:

sudo fallocate -l 2G /swapfile

If the fallocate command doesn’t work for your filesystem, use the dd command instead:

sudo dd if=/dev/zero of=/swapfile bs=1G count=2
  1. Set the Correct Permissions

It’s important to restrict permissions to the swap file for security reasons:

sudo chmod 600 /swapfile
  1. Set Up the Swap Space

Next, set up the swap area by executing:

sudo mkswap /swapfile
  1. Activate the Swap File

To activate the newly created swap file, run:

sudo swapon /swapfile
  1. Verify the Swap Space

Check if the swap is active:

sudo swapon --show
  1. Make the Swap Permanent

To ensure that the swap file is used after a reboot, add it to the /etc/fstab file. Open it in your preferred text editor:

sudo nano /etc/fstab

Add the following line at the end:

/swapfile none swap sw 0 0

Save and exit (Ctrl + X, then Y, then Enter).

Option 2: Creating a Swap Partition

If you prefer to create a dedicated partition for swap, follow these instructions:

  1. Create a New Partition

Use gparted (GUI) or fdisk (command line) to create a partition. Here’s how to create it with fdisk:

sudo fdisk /dev/sdX

Replace /dev/sdX with your drive (like /dev/sda). Create a new partition and set its type to Linux swap.

  1. Format the Partition

Execute this command to format it as swap:

sudo mkswap /dev/sdXn

Replace /dev/sdXn with your new swap partition designation.

  1. Activate the Swap Partition

Activate the swap partition:

sudo swapon /dev/sdXn
  1. Verify Activation

Check that the swap partition is active:

sudo swapon --show
  1. Configure /etc/fstab

To make this change permanent, edit /etc/fstab:

sudo nano /etc/fstab

Add the following line, replacing /dev/sdXn appropriately:

/dev/sdXn none swap sw 0 0

Save and exit the file.

Conclusion

In this guide, we’ve covered how to add swap space on Ubuntu 24.04 using both a swap file and a swap partition. Implementing swap space can significantly improve your system’s stability and performance, especially under heavy workloads.

As always, monitor your system’s performance and adjust your memory settings accordingly. If you encounter any issues or have questions, feel free to leave a comment below!

Happy computing!