← Back to all posts

The Complete Beginner’s Guide to Swap on Ubuntu Server

April 14, 2026 · 15 min read · Raymond

UbuntuServerserverswapBeginner Developersself-hosted
The Complete Beginner’s Guide to Swap on Ubuntu Server

When people first set up a small Ubuntu server, they usually focus on the fun parts first: installing apps, securing SSH, setting up a bot, deploying a website, or hosting a project. What many beginners do not think about is memory management.

That is where swap comes in.

Swap is one of those boring system features that does not sound exciting until the day your server runs out of RAM and starts killing processes, freezing, or behaving strangely. On a small server, especially a VPS with 1 GB or 2 GB of RAM, swap can be the difference between a machine that stays alive under pressure and one that falls over the moment memory usage spikes.

In this guide, I am going to explain swap in plain English, show you how to check whether you already have it, walk through the exact Ubuntu setup process, explain why we chose the settings we did, cover common mistakes, and give practical recommendations for different RAM sizes.

This is written for beginners, so I am not going to assume you already know Linux internals.


What swap actually is

Your server uses RAM for active working memory. RAM is fast, but limited. When RAM starts filling up, Linux has to decide what to do next.

One option would be to simply keep using memory until there is none left. But that leads to a problem: if a process needs more memory and the system has nowhere to put it, the kernel may step in and kill a process. This is often called the OOM killer, short for Out Of Memory.

Swap helps prevent that.

Swap is disk space that Linux can use like emergency memory.

It is much slower than RAM because it lives on storage rather than memory chips, but it gives the operating system breathing room. If some data in RAM has not been used recently, Linux can move it to swap, freeing RAM for the things you are actively doing.

A simple way to think about it:

  • RAM is your desk

  • swap is a storage box next to the desk

You want to keep what you are actively using on the desk because it is faster. But if the desk gets too crowded, it helps to move less important stuff into the box rather than throwing things away.

That is what swap does.


Why swap matters so much on small servers

If you have a big machine with 32 GB or 64 GB of RAM, swap may not matter much day to day. But on small Ubuntu servers, it matters a lot.

A 1 GB or 2 GB server can run fine most of the time and still hit memory trouble unexpectedly. Some common examples:

  • a bot or background script starts using more memory than expected

  • a package upgrade temporarily needs extra RAM

  • a sudden burst of traffic hits your app

  • a brute-force SSH attack increases system load

  • you open multiple services at once

  • a memory leak slowly eats available RAM

  • you try building software or running AI tools on a tiny VPS

Without swap, these events can push the system over the edge. With swap, Linux has somewhere to put overflow data.

Important point: swap is not a performance upgrade. It is a stability upgrade.

That is why people add swap even when their system looks healthy.

For example, you might check memory and see something like this:

free -h

Output:

               total        used        free      shared  buff/cache   available
Mem:           1.9Gi       326Mi       1.5Gi       4.0Mi       230Mi       1.6Gi
Swap:             0B          0B          0B

At first glance, this looks great. Very little RAM is being used. But that only tells you what is happening right now. It does not protect you from what happens during spikes.

That is why adding swap is still smart.


Swap file vs swap partition

There are two main ways Linux can use swap:

Swap partition

This is a dedicated partition created on the disk specifically for swap. It is common in traditional installations and can work well, but it is less flexible.

Swap file

This is a normal file on your filesystem that Linux treats as swap.

For most Ubuntu server users today, a swap file is the easiest and best choice because:

  • it is simple to create

  • it does not require repartitioning the disk

  • it can be resized later

  • it works well on most VPS setups

That is why we used a swap file.


How to check whether swap already exists

Before creating swap, always check if you already have some.

The two most useful commands are:

free -h

and

swapon --show

What free -h tells you

This command shows memory and swap in a human-readable format.

If you see:

Swap: 0B  0B  0B

that means you currently have no swap active.

What swapon --show tells you

This shows active swap devices or files.

If it prints nothing, there is no active swap.

This step matters because you do not want to accidentally create duplicate swap configurations.


A real beginner mistake: pasting output as a command

A very common beginner mistake is to copy a line like this:

Swap: 0B  0B  0B

and paste it back into the shell. That will produce an error like:

Swap:: command not found

That does not mean anything is broken. It just means you pasted output instead of entering a real command. Everyone does stuff like this when learning. It is normal.


How much swap should you use?

This is where a lot of guides become confusing because they repeat very old advice. You may still see rules like “swap should always equal double your RAM,” but modern systems are more nuanced than that.

Here are practical recommendations.

For 1 GB RAM

Use 1 GB to 2 GB of swap.

If the server is doing very light work, 1 GB may be enough. If it is exposed to real workloads or occasional spikes, 2 GB is safer.

For 2 GB RAM

Use 2 GB to 4 GB of swap.

This is one of the most common VPS sizes, and 4 GB is a very reasonable choice if you want extra headroom.

For 4 GB RAM

Use 2 GB to 4 GB of swap.

You usually do not need more unless you run memory-heavy workloads.

For 8 GB RAM

Use around 2 GB of swap, sometimes 4 GB if the workload is unpredictable.

For 16 GB RAM and above

Swap becomes less critical, but having 1 GB to 2 GB can still be helpful as a safety net.

General rule

Use swap to improve stability, not to pretend your server has more RAM than it really does.

If your system is constantly using lots of swap, that is not a sign everything is fine. It is a sign you may need to optimize memory use or upgrade the server.


Why we ended up using 4 GB of swap on a 2 GB server

In our case, the server had about 2 GB of RAM. Originally, a 2 GB swap file would have been perfectly reasonable. But when the swap file was created and formatted, the output showed:

Setting up swapspace version 1, size = 4 GiB

Then after enabling it, the system showed:

Swap: 4.0Gi  0B  4.0Gi

That is actually a solid outcome.

For a 2 GB server, 4 GB of swap is a good amount if:

  • you want a stronger safety net

  • you may run occasional heavier tasks

  • you understand that swap is slower than RAM

We would not call that excessive for a small VPS. It is a practical choice.


Step-by-step: how to create a swap file on Ubuntu

Now let’s walk through the actual setup.

Step 1: Confirm no swap is active

swapon --show

If nothing appears, continue.

You can also verify with:

free -h

If the swap line says zero, you are clear to proceed.


Step 2: Create the swap file

To create a 2 GB swap file, use:

sudo fallocate -l 2G /swapfile

If you want 4 GB instead:

sudo fallocate -l 4G /swapfile

This creates a file named /swapfile of the specified size.

If fallocate does not work

Some systems may not support fallocate the way you expect. In that case, you can use dd:

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

This example creates a 4 GB swap file.

dd is slower, but reliable.


Step 3: Lock down permissions

Swap files should not be readable by normal users. Set strict permissions:

sudo chmod 600 /swapfile

This makes the file readable and writable only by root.


Step 4: Format the file as swap

Now tell Linux this file should be treated as swap:

sudo mkswap /swapfile

You should see output showing the swapspace version and size.


Step 5: Enable the swap file

Turn it on immediately:

sudo swapon /swapfile

At this point, swap is active for the current session.


Step 6: Verify that it is working

Check with:

free -h

and

swapon --show

You should now see your swap file listed, and free -h should show the new swap size.

For example:

               total        used        free      shared  buff/cache   available
Mem:           1.9Gi       336Mi       1.1Gi       4.0Mi       645Mi       1.6Gi
Swap:          4.0Gi          0B       4.0Gi

and:

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

That means it is working.


Making swap survive a reboot

If you stop here, the swap file is active now, but it may not come back automatically after reboot. To make it permanent, add it to /etc/fstab.

Use:

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

This appends the correct line.


The duplicate /etc/fstab mistake

One thing that happened in our process is that the same swap entry got added twice.

The file looked like this:

/swapfile none swap sw 0 0
/swapfile none swap sw 0 0

This is not ideal. Duplicate entries can cause confusion and may lead to boot-time warnings or mount issues.

The correct version should only include the line once.

A clean /etc/fstab looked like this:

LABEL=cloudimg-rootfs   /        ext4   discard,commit=30,errors=remount-ro     0 1
LABEL=BOOT      /boot   ext4    defaults        0 2
LABEL=UEFI      /boot/efi       vfat    umask=0077      0 1
/swapfile none swap sw 0 0

That is correct.

If you ever suspect /etc/fstab has a mistake, test it safely with:

sudo mount -a

If that command returns no errors, your fstab syntax is probably fine.


Tuning swap for better behavior

Out of the box, Linux may use swap more or less aggressively depending on defaults. A small amount of tuning helps.

The two settings we changed were:

  • vm.swappiness

  • vm.vfs_cache_pressure

Open the config file:

sudo nano /etc/sysctl.conf

Add these lines at the bottom:

vm.swappiness=10
vm.vfs_cache_pressure=50

Then apply the settings:

sudo sysctl -p

What vm.swappiness=10 means

This tells Linux to prefer using RAM and only move things into swap when it is more necessary.

A lower value generally means:

  • less aggressive swap use

  • better responsiveness

  • swap acts more like a safety net

This is a good choice for small general-purpose servers.

What vm.vfs_cache_pressure=50 means

This tells Linux to be a bit less aggressive about reclaiming filesystem cache.

That can help overall responsiveness and caching behavior.

These are reasonable, conservative settings for a beginner-friendly Ubuntu server.


Why we tried zram and why it failed

At one point we tried a more advanced option called zram.

What zram is

zram creates a compressed block device in RAM and can use it for swap. In some cases, this is faster than using disk-based swap because compression can reduce how much actual memory is consumed.

For low-memory systems, zram can be excellent.

Why it did not work here

When the service was started, the error was:

modprobe: FATAL: Module zram not found in directory /lib/modules/6.8.0-110-generic

That tells us the running kernel did not have the zram module available.

This is important because it means the failure had nothing to do with a typo in the config. The kernel simply did not support loading zram on that system.

Lesson for beginners

If zram fails with a missing kernel module, do not keep fighting it. A normal swap file is the right fallback, and it works almost everywhere.


Should you reboot after setting up swap?

Usually, you do not have to reboot immediately.

Once you run:

sudo swapon /swapfile

the swap file is already active.

And once you add it to /etc/fstab, it should survive future boots.

That said, rebooting once can be a good final test because it confirms:

  • the system boots cleanly

  • swap activates automatically

  • your config persists as expected

Before rebooting a remote server, it is smart to verify that your fstab is clean and that SSH access is working.

Then you can reboot:

sudo reboot

After reconnecting, check again with:

free -h
swapon --show

If you still see /swapfile, everything is working correctly.


How to know whether your swap setup is healthy

After setup, there are three basic things to look for.

1. Swap exists

Check:

swapon --show

You should see /swapfile.

2. The system reports the expected amount

Check:

free -h

Look for the swap line.

3. Swap is not constantly maxed out

If swap use remains near zero most of the time, that is usually a good sign. It means the system has the safety net available, but it is not leaning on it heavily.

If swap use grows a lot and stays high, you may need to:

  • reduce workload

  • optimize apps

  • upgrade RAM

  • move some services elsewhere

Again, swap is there to help the system survive pressure. It is not a substitute for enough RAM.


Suggested swap sizes by server type

Here are practical recommendations you can actually use.

Tiny VPS for SSH, light bots, or a static site

1 GB RAM

Use 1 GB to 2 GB swap.

A conservative choice is 2 GB if the server is internet-facing.

Small general-purpose VPS

2 GB RAM

Use 2 GB to 4 GB swap.

If you run a few services, 4 GB is a very sensible choice.

Small app server or personal projects

4 GB RAM

Use 2 GB to 4 GB swap.

If the workload is moderate, 2 GB is often enough. If you expect bursts or builds, use 4 GB.

Medium VPS

8 GB RAM

Use 2 GB swap, or 4 GB if the machine hosts unpredictable workloads.

Larger servers

16 GB and up

Use 0 to 2 GB depending on your use case.

Some people still keep a small swap file just for edge cases.


Common beginner questions

“Do I need swap if I still have lots of free RAM?”

Yes, often you still want it. Free RAM right now does not protect you from future memory spikes.

“Is swap bad for SSDs?”

Modern SSDs can handle normal swap use fine in most personal and small server scenarios. You should not abuse swap as fake RAM, but having a swap file is not some automatic SSD death sentence.

“Will swap make my server faster?”

No. It usually makes the system slower than RAM, but more stable than crashing.

“Should swap equal RAM?”

Not always. That old rule is too simplistic. Choose based on workload and server size.

“What if my system starts using lots of swap?”

That is a sign to investigate memory usage. The swap setup is still helping, but it may also be telling you the machine is too small for what you are asking it to do.


The final configuration we ended up with

For this specific 2 GB Ubuntu server, the final outcome was:

  • a working /swapfile

  • 4 GB of active swap

  • a clean /etc/fstab with one swap entry

  • vm.swappiness=10

  • vm.vfs_cache_pressure=50

  • zram disabled because the kernel did not support it

That is a clean, stable, beginner-friendly setup.


Final thoughts

Swap is one of those things beginners often ignore because nothing seems wrong at first. Then one day the server crashes during a memory spike and suddenly swap feels a lot more important.

The good news is that setting it up on Ubuntu is not hard.

The process is simple:

  1. check whether swap already exists

  2. create a swap file

  3. secure it

  4. format it

  5. enable it

  6. add it to fstab

  7. tune the kernel a little

  8. verify everything works

That is it.

For a small VPS, this is one of the highest-value maintenance tasks you can do. It does not make your setup flashy, but it makes it tougher, calmer, and more reliable.

And on servers, reliability beats flashy every time.

← Back to all posts