A Guide to Adding Swap Space on Ubuntu 20.04

When it comes to safeguarding your server against out-of-memory errors, adding swap space can be a lifesaver. In this comprehensive guide, we will walk you through the process of creating and configuring swap space on an Ubuntu 20.04 server.

Understanding Swap Space

Swap is an essential component of your server’s memory management. It’s a dedicated portion of your hard drive used by the operating system to temporarily store data that doesn’t fit in RAM. This enables your server to handle more data than its physical memory allows, albeit at a slower speed. Swap becomes crucial when your RAM is fully utilized, preventing your server from crashing due to memory limitations.

Step 1: Checking the System for Existing Swap Space

Before we begin, it’s a good idea to check if your system already has any configured swap space. You can do this by running the following command:

sudo swapon --show

If there’s no output, it means your system currently has no active swap space.

You can also verify the presence of swap space using the free utility:

free -h

Step 2: Assessing Available Disk Space

To proceed, we need to ensure that there’s enough disk space to create the swap file. You can check your current disk usage by running:

df -h

Step 3: Creating a Swap File

With disk space confirmed, we can now create a swap file. We recommend using the fallocate command, as it’s efficient and swift. In this example, we’ll create a 1GB swap file:

sudo fallocate -l 1G /swapfile

You can verify the file’s size by running:

ls -lh /swapfile

Step 4: Enabling the Swap File

Now that we have the swap file ready, we need to enable it. However, before doing so, we must secure the file’s permissions to prevent unauthorized access. Only root users should be able to read its contents. Execute the following commands to set the correct permissions:

sudo chmod 600 /swapfile

To verify the permissions change, use:

ls -lh /swapfile

Now, we can mark the file as swap space and activate it:

sudo mkswap /swapfile sudo swapon /swapfile

You can check if the swap is available by running:

sudo swapon --show

Step 5: Making the Swap File Permanent

The changes we’ve made enable the swap file for the current session only. To ensure it persists after a server reboot, we need to add it to the /etc/fstab file. Before proceeding, create a backup of the fstab file:

sudo cp /etc/fstab /etc/fstab.bak

Then, add the swap file information to the end of the fstab file using this command:

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

Step 6: Fine-Tuning Swap Settings

To optimize your server’s performance related to swap space, consider adjusting two key settings:

Swappiness Property

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

The swappiness parameter controls how often your system swaps data from RAM to the swap space. It’s a value between 0 and 100, representing a percentage.

  • A lower value (close to 0) minimizes disk swapping, which can improve performance on servers.
  • A higher value (closer to 100) encourages the system to use swap space more frequently, which can be useful for desktop systems.

You can check the current swappiness value with:

cat /proc/sys/vm/swappiness

To change it temporarily, use the sysctl command. For example, to set swappiness to 10:

sudo sysctl vm.swappiness=10

To make this change permanent, add the following line to the /etc/sysctl.conf file:

sudo nano /etc/sysctl.conf

Add the line at the bottom:

/etc/sysctl.conf vm.swappiness=10

Save and close the file.

Cache Pressure Setting

The vfs_cache_pressure setting determines how aggressively the system caches inode and dentry information over other data. This information is crucial for file system operations and should be cached efficiently.

You can check the current value with:

cat /proc/sys/vm/vfs_cache_pressure

To change it temporarily, use the sysctl command. For example, to set it to 50:

sudo sysctl vm.vfs_cache_pressure=50

To make this change permanent, add the following line to the /etc/sysctl.conf file:

sudo nano /etc/sysctl.conf

Add the line at the bottom:


Save and close the file.


Adding swap space to your Ubuntu 20.04 server can significantly improve its stability and performance. By following these steps, you’ve successfully created, configured, and optimized your swap space. Your server is now equipped to handle memory-intensive tasks with ease.

Leave a Comment