How to Create a Swap on Ubuntu: A Step-by-Step Guide

If you’re an Ubuntu user looking to optimize your system’s performance, creating a swap space can be a valuable solution. In this article, we’ll walk you through the process of creating a swap on Ubuntu, ensuring that your system runs smoothly.

Understanding the Importance of Swap Space

Before diving into the steps, let’s briefly understand what swap space is and why it’s crucial for your Ubuntu system.

Swap space, often referred to as a swap partition, is an area on your hard drive that serves as virtual memory when your physical RAM is fully utilized. This virtual memory allows your system to continue running smoothly by temporarily moving data from RAM to the swap space. It prevents your system from slowing down or crashing when it runs out of physical memory.

Checking Your System’s Current Swap

Before creating a new swap space, it’s essential to check if your Ubuntu system already has an existing swap partition. You can do this by opening your terminal and using the following command:

sudo swapon --show

If you don’t see any output, it means there is no active swap space on your system.

Creating a Swap File

Now, let’s proceed with creating a swap file. Follow these steps carefully:

Step 1: Determine the Size of Your Swap File

Decide how much swap space you want to allocate. A common rule of thumb is to create a swap file that is at least equal to the amount of RAM you have. To create a swap file of 4 GB, for example, you can use the dd command:

sudo fallocate -l 4G /swapfile

Step 2: Set Appropriate Permissions

Next, you need to set appropriate permissions for your swap file:

sudo chmod 600 /swapfile

Step 3: Mark the File as Swap Space

Now, let Ubuntu know that this file should be used as swap space:

sudo mkswap /swapfile

Step 5: Enable the Swap File at Boot

To ensure your swap file is automatically enabled at boot, add it to the /etc/fstab file:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Your swap file is now set up and active. You can verify this by using the swapon --show command again.


In this article, we’ve explored how to create a swap on Ubuntu, an essential step in optimizing your system’s performance. By following these simple steps, you can ensure that your Ubuntu system runs smoothly, even when your physical RAM is fully utilized. Now that your swap space is set up, enjoy a faster and more responsive Ubuntu experience!

Remember that maintaining your system’s performance is crucial for efficient work, and creating a swap space is just one of the many ways to achieve that. So, go ahead and give your Ubuntu system the boost it deserves!