How to add a swap file on Ubuntu 24.04

10 mag 2024 2 min di lettura
How to add a swap file on Ubuntu 24.04
Indice dei contenuti

Introduction

Are you dealing with low RAM on your Ubuntu system? This is a common problem on systems that require more memory to run applications smoothly. This can be solved by adding more memory, but this incurs 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 issues.

What is swap space

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

Swap space helps your system to:

  • 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 having 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: Check the existing swap space on your system

Before adding a new swap file, it is a good idea to check if your system already has any swaps configured. Linux offers some tools to see the details of the exchange. Try the swapon command for active swap details or for free to see your total and used swap space

sudo swapon --show

The command will return nothing if there is no swap added to the system.

Step 2: Create a swap file

Choose Size: First of all, you need to decide the size of swap space required on your system. It is generally recommended to equal or double the amount of RAM, depending on system usage and available disk space.

Create the file: You can use the fallocate command to create a swap file of the desired size. For example, to create a 4GB swap file, run:

sudo fallocate -l 4G /swapfile

Set the correct permissions: Set the correct permissions on your swap file to ensure that it is not readable by anyone except the root user:

sudo chmod 600 /swapfile

Step 3: Enabling the swap file

After creating the swap file, we need to format it as Swap:

sudo mkswap /swapfile

Now you can enable the swap file:

sudo swapon /swapfile

Check that swap is active on your system:

sudo swapon --show

This command should now show the newly created swap file.

Step 4: Make the swap file permanent

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

Edit the /etc/fstab file in your favorite text editor:

sudo nano /etc/fstab

Add the following line to /etc/fstab:


/swapfile none swap sw 0 0

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 optimize your system's performance with respect to RAM and swap usage.

Make it permanent by adding vm.swappiness=20 to /etc/sysctl.conf.

Check the current value of Swappiness:

cat /proc/sys/vm/swappiness

Change the Swappiness value (for example, to 20, which is a recommended value for desktop systems):

sudo sysctl vm.swappiness=20

Conclusion

You have successfully added and configured a swap file on your Ubuntu 24.04 system. This addition should help manage system resources more effectively, especially under heavy load conditions. Remember, while swapping can help manage memory pressure, it's no substitute for adding more physical RAM when you're constantly dealing with performance issues.

Support us with a

Successivamente, completa il checkout per l'accesso completo a Noviello.it.
Bentornato! Accesso eseguito correttamente.
Ti sei abbonato con successo a Noviello.it.
Successo! Il tuo account è completamente attivato, ora hai accesso a tutti i contenuti.
Operazione riuscita. Le tue informazioni di fatturazione sono state aggiornate.
La tua fatturazione non è stata aggiornata.