How to Add a Swap File on Ubuntu 26.04

Aidin

Memory stick illustration for an Ubuntu swap file guide

Hi everyone. Has your Ubuntu machine ever slowed to a crawl, or had apps close on their own, when you opened one browser tab too many? Running out of RAM is a common cause, and that is where swap comes in. Swap is space on your disk that Linux uses as backup memory. When real RAM is almost full, data you are not actively using gets moved to swap so the system does not freeze.

Swap is much slower than RAM, so it is not a replacement for it. It is a safety net, and a very useful one on machines with little memory or on cheap VPS plans. In this guide I add a 2 GB swap file on Ubuntu 26.04, including a warning you will see if you skip a step that many guides forget.

Distro : Ubuntu 26.04.1 LTS (Desktop)
RAM    : 7.2 GB
Swap   : /swap.img 4 GB (default) + /swapfile2 2 GB (new)

Check the Swap You Already Have

Before adding swap, check whether your system already has some and how big it is. Plenty of people create a new swap file without realizing Ubuntu already set one up.

I used two commands: swapon –show to list active swap, and free -h to see total RAM and swap.

See your current swap and RAM

The default 4 GB /swap.img on Ubuntu 26.04
swapon --show
free -h

Ubuntu 26.04 already created a 4G swap file called /swap.img during installation (number 1). free -h confirms 4.0Gi of swap, none of it in use yet (number 2). So if you have plenty of RAM, you do not strictly need more swap. If your machine runs out of memory often, adding some is still worth it.

Create a New Swap File

Swap can be a dedicated partition or just a regular file. A file is the easiest and safest option for beginners, because you do not have to touch your disk partitions.

I created a new file called /swapfile2 at 2 GB. You can name it whatever you like.

Create a 2 GB file and turn it into swap

Creating a 2 GB swap file with fallocate and mkswap
sudo fallocate -l 2G /swapfile2
ls -lh /swapfile2
sudo mkswap /swapfile2

fallocate creates an empty 2.0G file (number 1). Notice its permissions are -rw-r–r–, which means any user can read it. As soon as mkswap runs, it warns: insecure permissions 0644, fix with: chmod 0600 (number 2). Swap can hold pieces of whatever was in RAM, including sensitive data, so only root should be able to read this file.

The same warning from swapon

insecure permissions 0644 warning from swapon

I went ahead and tried to enable it anyway, just to see what happens.

sudo swapon /swapfile2

The same warning shows up again: insecure permissions 0644, 0600 suggested (number 1). The swap still turns on, but in an unsafe state. This is the step most other guides skip.

Fix the Permissions and Enable the Swap

To fix it, turn that swap off, change its permissions to 600, and turn it back on. 600 means only the owner, root, can read and write the file.

If permission numbers like 600 and 644 are new to you, see the guide on the chmod command in Linux.

Change the permissions to 600

Fixing the swap file permissions to 600
swapon --show
sudo swapoff /swapfile2; sudo chmod 600 /swapfile2
sudo swapon /swapfile2 && swapon --show

The first swapon –show proves swapfile2 was active even with the unsafe permissions (number 1). After switching it off and running chmod 600 (number 2), it turns back on without any warning. Now there are two active swap files, /swap.img and /swapfile2 (number 3).

Check total swap

Total swap now 6 GB
free -h

Total swap is now 6.0Gi (number 1): the 4 GB default plus the new 2 GB.

Keep the Swap After a Reboot

Swap you enable with swapon only lasts until you shut down. After a reboot, swapfile2 will not come back unless you add it to /etc/fstab.

fstab lists the disks and swap that get mounted automatically at every boot. Be careful with it, because a mistake in this file can stop your computer from booting.

Add it to /etc/fstab

Adding the swap file to /etc/fstab
echo '/swapfile2 none swap sw 0 0' | sudo tee -a /etc/fstab
grep swap /etc/fstab

The -a on tee means append, so it adds a line at the end instead of overwriting the file. grep shows fstab now has two swap entries: Ubuntu’s own /swap.img and the new /swapfile2 (number 1).

Warning: Always use tee -a, never plain tee. Without -a, the whole of /etc/fstab gets replaced by that one line and your computer may not boot.

Test fstab without rebooting

Testing fstab with swapoff -a and swapon -a

Rather than rebooting and hoping for the best, I tested fstab by turning all swap off and back on from the fstab list.

sudo swapoff -a && sudo swapon -a && swapon --show

Both swap files came back (number 1), so the fstab line is correct and the swap will survive a reboot.

Adjust Swappiness

Swappiness is a number from 0 to 100 that controls how eagerly Ubuntu uses swap. The higher it is, the sooner data gets moved to swap, even while there is still free RAM.

Because swap is so much slower than RAM, on a desktop it usually feels better to use swap only when RAM is genuinely running low.

Set swappiness to 20

Changing Ubuntu swappiness to 20
cat /proc/sys/vm/swappiness
sudo sysctl vm.swappiness=20
echo vm.swappiness=20 | sudo tee /etc/sysctl.d/99-swappiness.conf

Ubuntu’s default is 60 (number 1). I lowered it to 20 with sysctl, which takes effect immediately (number 2). To keep it after a reboot, I saved the value in /etc/sysctl.d/99-swappiness.conf (number 3).

Common Problems and How to Fix Them

Swap problems usually come down to wrong file permissions or a mistake in fstab. These are the cases I see most.

If you are unsure about an fstab change, always test it with sudo swapon -a before rebooting.

insecure permissions 0644

The swap file is readable by everyone. Run sudo chmod 600 on it, then switch the swap off and on again.

swapon: Invalid argument

The file has not been formatted as swap yet. Run sudo mkswap on it before swapon.

The swap disappears after a reboot

It is not in /etc/fstab yet. Add the line with tee -a as shown above.

The computer is still slow even with lots of swap

Swap is a safety net, not extra RAM. If swap is constantly busy, the real fix is more RAM or closing memory-hungry apps. You can find those apps with the tools in the guide on monitoring processes with top and htop.

That is how to add a swap file on Ubuntu 26.04: check the default swap, create a new file with safe permissions, make it permanent in fstab, and tune swappiness. To check that there is enough free disk space before creating swap, use the df and du commands. If you hit an error while setting up swap, share it in the comments.

Leave a Comment