Skip to content

Instantly share code, notes, and snippets.

@reidjs
Created August 5, 2025 15:51
Show Gist options
  • Save reidjs/ff029d637dd1cf94ef6b86b19bef1158 to your computer and use it in GitHub Desktop.
Save reidjs/ff029d637dd1cf94ef6b86b19bef1158 to your computer and use it in GitHub Desktop.
Add swapfile to ubuntu 22.04 server

Swapfiles are important because they prevent a memory constrained device from crashing due to load. From https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-22-04

Introduction

One way to guard against out-of-memory errors in applications is to add some swap space to your server. In this guide, we will cover how to add a swap file to an Ubuntu 22.04 server.

What is Swap?

Swap is a portion of hard drive storage that has been set aside for the operating system to temporarily store data that it can no longer hold in RAM. This lets you increase the amount of information that your server can keep in its working memory, with some caveats. The swap space on the hard drive will be used mainly when there is no longer sufficient space in RAM to hold in-use application data.

The information written to disk will be significantly slower than information kept in RAM, but the operating system will prefer to keep running application data in memory and use swap for the older data. Overall, having swap space as a fallback for when your system’s RAM is depleted can be a good safety net against out-of-memory exceptions on systems with non-SSD storage available.

Step 1 – Checking the System for Swap Information

Before we begin, we can check if the system already has some swap space available. It is possible to have multiple swap files or swap partitions, but generally one should be enough.

We can see if the system has any configured swap by typing:

sudo swapon --show

If you don’t get back any output, this means your system does not have swap space available currently.

You can verify that there is no active swap using the free utility:

free -h

Output:

              total        used        free      shared  buff/cache   available
Mem:          981Mi       122Mi       647Mi       0.0Ki       211Mi       714Mi
Swap:            0B          0B          0B

As you can see in the Swap row of the output, no swap is active on the system.

Step 2 – Checking Available Space on the Hard Drive Partition

Check your current disk usage:

df -h

Output:

Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        25G  1.4G   23G   7% /

Although there are many opinions about the appropriate size of a swap space, it really depends on your personal preferences and your application requirements. Generally, an amount equal to or double the amount of RAM on your system is a good starting point.

Step 3 – Creating a Swap File

Create a 1G swap file:

sudo fallocate -l 1G /swapfile

Verify:

ls -lh /swapfile

Expected Output:

-rw-r--r-- 1 root root 1.0G Apr 25 11:14 /swapfile

Step 4 – Enabling the Swap File

Secure the swap file:

sudo chmod 600 /swapfile
ls -lh /swapfile

Output:

-rw------- 1 root root 1.0G Apr 25 11:14 /swapfile

Mark and enable the file:

sudo mkswap /swapfile
sudo swapon /swapfile

Check it’s working:

sudo swapon --show
free -h

Step 5 – Making the Swap File Permanent

Backup and modify fstab:

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

Step 6 – Tuning your Swap Settings

Adjusting the Swappiness Property

cat /proc/sys/vm/swappiness
sudo sysctl vm.swappiness=10
sudo nano /etc/sysctl.conf

Add:

vm.swappiness=10

Adjusting the Cache Pressure Setting

cat /proc/sys/vm/vfs_cache_pressure
sudo sysctl vm.vfs_cache_pressure=50
sudo nano /etc/sysctl.conf

Add:

vm.vfs_cache_pressure=50

Conclusion

Following the steps in this guide will give you some breathing room in cases that would otherwise lead to out-of-memory exceptions. Swap space can be incredibly useful in avoiding some of these common problems.

If you are running into OOM (out of memory) errors, or if you find that your system is unable to use the applications you need, the best solution is to optimize your application configurations or upgrade your server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment