Welcome to our comprehensive guide on how to add swap space on Ubuntu 22.04. If you’re experiencing sluggishness and performance issues on your Ubuntu system, increasing the swap space could be the solution you’re looking for.
In this article, we’ll walk you through the step-by-step process of adding swap space on Ubuntu 22.04. We’ll cover different methods that range from creating a swap file to partitioning your hard drive.
Whether you’re a beginner or an experienced Ubuntu user, this guide has got you covered. Our instructions are easy to follow, ensuring that you can successfully add swap space to your system without any hassle.
By the end of this article, you’ll have a clear understanding of how to optimize your Ubuntu system’s performance by increasing the swap space. So, let’s dive in and unlock the full potential of your Ubuntu 22.04 system!
Remember to bookmark this guide, as you might want to refer back to it in the future. Let’s get started!
Understanding Swap Space and Its Importance
Swap space, also known as virtual memory, is a designated space on your computer’s hard drive that acts as an extension of your system’s physical memory (RAM). It allows the operating system to temporarily transfer data from the RAM to the hard drive when the physical memory is running low.
The importance of swap space cannot be overstated, especially if you have limited RAM or frequently run memory-intensive applications. Without sufficient swap space, your system may experience slowdowns, crashes, or even fail to run certain programs.
Check If Your System Needs Additional Swap Space
Before adding swap space to your Ubuntu 22.04 system, it’s essential to determine whether your system actually needs it. Ubuntu automatically creates a swap partition during installation, but its size might not be sufficient for your requirements.
To check if your system needs additional swap space, you can use the free command in the terminal. Open the terminal and run the following command:
bash free -h
This command will display information about your system’s memory usage, including the available swap space. If the available swap space is consistently low or reaches zero, it’s a clear indication that you need to add more swap space.
Determining the Size of Swap Space Needed
The size of the swap space you need largely depends on your system’s usage and the amount of RAM installed. As a general rule of thumb, it is recommended to have at least as much swap space as your installed RAM, and possibly more if you frequently use memory-intensive applications or run virtual machines.
To determine the size of swap space needed, you can use the following formula:
- If your system has less than 2GB of RAM, set the swap space size to twice the amount of RAM.
- If your system has between 2GB and 8GB of RAM, set the swap space size to the same as the amount of RAM.
- If your system has more than 8GB of RAM, set the swap space size to half the amount of RAM.
For example, if your system has 4GB of RAM, the recommended swap space size would be 4GB. However, if you frequently use memory-intensive applications or run virtual machines, you may want to increase the swap space size to 8GB.
Creating a Swap File on Ubuntu 22.04
One way to add swap space on Ubuntu 22.04 is by creating a swap file. A swap file is a file on your hard drive that acts as virtual memory. It provides flexibility as you can easily adjust the size of the swap file as needed.
To create a swap file, follow these steps:
Step 1: Open the terminal on your Ubuntu system.
Step 2: Run the following command to create an empty file with the specified size (replace swapfilesize with the desired size, e.g., 4G for 4GB):
sudo fallocate -l swapfilesize /swapfile
Step 3: Set the correct permissions for the swap file by running the following command:
sudo chmod 600 /swapfile
Step 4: Format the file as swap space by running the following command:
sudo mkswap /swapfile
Step 5: Enable the swap file by running the following command:
sudo swapon /swapfile
Congratulations! You have now successfully created a swap file on your Ubuntu 22.04 system.
Enabling the Swap File
To enable the swap file permanently, you need to make an entry in the /etc/fstab file. This ensures that the swap file is automatically activated during system boot.
Step 1: Open the /etc/fstab file using a text editor with administrative privileges. For example:
sudo nano /etc/fstab
Step 2: At the end of the file, add the following line:
/swapfile none swap sw 0 0
Step 3: Save the file and exit the text editor.
Verifying the Swap Space Allocation
To verify that the swap space allocation has been successful, you can use the free command again. Open the terminal and run the following command:
bash free -h
The output should now show the allocated swap space along with the physical memory (RAM) information.
Making the Swap Space Permanent
By default, Ubuntu 22.04 automatically sets the swappiness value to 60, which determines the tendency of the system to use swap space. However, you can adjust this value to better suit your needs.
To adjust the swappiness value, follow these steps:
Step 1: Open the /etc/sysctl.conf file using a text editor with administrative privileges.
sudo nano /etc/sysctl.conf
Step 2: Add the following line at the end of the file to set the swappiness value (0-100, with 0 being the least and 100 being the most):
vm.swappiness=desired_value
Step 3: Replace desired_value with the desired swappiness value. For example, a value of 10 would reduce the tendency to use swap space.
Step 4: Save the file and exit the text editor.
Adjusting Swap Settings for Optimal Performance
In addition to the swappiness value, you can further optimize your swap settings for better performance. The sysctl command allows you to adjust various kernel parameters related to swap space.
To adjust swap settings using the sysctl command, follow these steps:
Step 1: Open the terminal and run the following command to view the current swap settings:
bash sudo sysctl vm.swappiness
Step 2: To change a specific swap setting, use the following command (replace setting_name and desired_value with the desired values):
bash sudo sysctl -w setting_name=desired_value
Step 3: To make the changes permanent, open the /etc/sysctl.conf file and add the following line for each desired setting:
setting_name=desired_value
Step 4: Save the file and exit the text editor.
Monitoring Swap Space Usage
It’s important to monitor your swap space usage to ensure that it is sufficient and not being overly utilized. Ubuntu provides various tools that allow you to monitor swap space usage in real-time.
Step 1: One such tool is htop, a powerful command-line process viewer. To install htop, open the terminal and run the following command:
bash sudo apt install htop
Step 2: Once installed, you can launch htop by running the command htop in the terminal. The swap space usage is displayed at the bottom of the screen.
Conclusion
In this comprehensive guide, we have explored the step-by-step process of adding swap space on Ubuntu 22.04. Understanding the importance of swap space, checking if your system needs additional swap space, and determining the appropriate size are crucial steps in optimizing your Ubuntu system’s performance.
We have covered the process of creating a swap file, enabling it, making it permanent, adjusting swap settings, and monitoring swap space usage. By following these instructions, you can ensure that your Ubuntu 22.04 system has sufficient swap space to handle memory-intensive tasks and applications.
Remember, adding swap space is just one aspect of optimizing your system’s performance. Regular maintenance, updating software, and using efficient resource management techniques will further enhance your Ubuntu 22.04 experience.

Nishant Verma is a senior web developer who love to share his knowledge about Linux, SysAdmin, and more other web handlers. Currently, he loves to write as content contributor for ServoNode.