How to Increase the Swap File Size in Ubuntu to Improve System Performance

Ubuntu allocates swap space during installation to act as overflow memory when physical RAM runs low. On a default installation, Ubuntu creates a swap file — typically 2 GB — which is often insufficient for memory-intensive workloads such as compiling large codebases, running virtual machines, processing large datasets, or using applications that demand significantly more RAM than your system has installed.

Increasing your Ubuntu swap file size is a straightforward process that does not require reinstalling the operating system or repartitioning your drive. This guide covers the complete process: checking your current swap, disabling it safely, creating a larger swap file, and making the change permanent across reboots.

Check Your Current Swap Configuration

Before making any changes, confirm your current swap size and type. Open a terminal and run:

swapon --show

This displays active swap devices with their type (file or partition), size, and usage. You will see output similar to:

NAME      TYPE SIZE USED PRIO
/swapfile file   2G   0B   -2

To view total swap alongside RAM in a human-readable format, use:

free -h

Note the path shown under NAME in the swapon output — this is typically /swapfile on Ubuntu. You will need this path in the steps below.

How Much Swap Do You Need?

The appropriate swap size depends on your workload and RAM:

  • 4 GB RAM or less: A swap file of 4–8 GB is reasonable.
  • 8–16 GB RAM: A swap file of 4–8 GB is generally sufficient for most desktop and development workloads.
  • More than 16 GB RAM: A swap file of 4 GB is often adequate unless you suspend to disk (hibernate), which requires swap equal to or larger than the amount of RAM.

For systems that use hibernation, the swap file must be at least as large as your total installed RAM. For example, a system with 16 GB of RAM requires at least a 16 GB swap file to hibernate successfully.

Step 1: Disable the Existing Swap File

You cannot resize a swap file directly. Instead, you must disable it, delete it, create a new larger file, and re-enable it.

Disable the current swap file with:

sudo swapoff /swapfile

If your swap file has a different path (as shown in the swapon --show output), replace /swapfile with the correct path.

After running this command, verify swap is disabled:

free -h

The Swap row should show 0B used and 0B total.

Step 2: Remove the Existing Swap File

Delete the old swap file to free the disk space it occupies:

sudo rm /swapfile

Step 3: Create a New Larger Swap File

Create a new swap file at your desired size. The following example creates a 8 GB swap file. Replace 8G with your preferred size:

sudo fallocate -l 8G /swapfile

If fallocate is not available or fails on your filesystem (such as Btrfs, which does not support fallocate for swap), use dd instead:

sudo dd if=/dev/zero of=/swapfile bs=1M count=8192 status=progress

This writes 8192 MB (8 GB) of zeroes to the swap file. Adjust count to match your target size (1024 MB = 1 GB).

Step 4: Set Correct Permissions

The swap file must be readable and writable only by root. Incorrect permissions will prevent it from being used and generate security warnings:

sudo chmod 600 /swapfile

Verify the permissions are correct:

ls -lh /swapfile

You should see output similar to: -rw------- 1 root root 8.0G

Step 5: Format the File as Swap

Initialise the file as a Linux swap area:

sudo mkswap /swapfile

You will see output confirming the swap space has been set up, including the UUID assigned to it.

Step 6: Enable the New Swap File

Activate the swap file immediately:

sudo swapon /swapfile

Confirm it is active:

swapon --show

You should see the new swap file listed with the increased size. Also verify with:

free -h

Step 7: Make the Swap File Permanent

Enabling swap with swapon is temporary — it will not survive a reboot unless it is registered in /etc/fstab. Check if an entry already exists:

grep swap /etc/fstab

If you see a line such as /swapfile none swap sw 0 0, it is already configured correctly and no further action is needed.

If no swap entry exists, add one:

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

After adding the entry, verify it was written correctly:

cat /etc/fstab | grep swap

Optimise Swap Usage with the Swappiness Setting

The swappiness kernel parameter controls how aggressively Ubuntu moves memory pages to swap. The default value is 60, meaning the kernel begins using swap when approximately 40% of RAM is still available.

For desktop and developer workstations with plenty of RAM, a lower swappiness value keeps more data in RAM and only moves to swap when genuinely necessary:

cat /proc/sys/vm/swappiness

To temporarily set swappiness to a lower value (for example, 10):

sudo sysctl vm.swappiness=10

To make this permanent, add it to /etc/sysctl.conf:

echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

Recommended values:

  • Desktop systems (8 GB+ RAM): 10–20
  • Servers with heavy I/O: 1–10
  • Low-RAM systems (2–4 GB): Keep the default of 60 or higher to ensure swap is used proactively

Verify Everything Is Working Correctly

Reboot your system to confirm the swap file mounts automatically on startup:

sudo reboot

After logging back in, run:

swapon --show
free -h

Both commands should reflect the new, larger swap size. Your system will now handle memory-intensive tasks more gracefully, reducing the likelihood of out-of-memory (OOM) errors and keeping processes stable when RAM pressure increases.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.