How to Set Up a Swap File on Ubuntu VPS

If you run an Ubuntu VPS, you might have heard of something called swap. It helps your server when it runs out of RAM. In this guide, I’ll show you how to set up a swap file step by step.

I’ve used this method on many of my small servers. It’s helped avoid crashes and slowdowns. You don’t need to be a Linux expert to follow along. Just some basic terminal skills.

Let’s get started.


What Is Swap?

Swap is like a backup memory. When your RAM (the server’s memory) gets full, your system moves extra data to the swap space. It’s not as fast as RAM, but it keeps your server running instead of crashing.

There are two types of swap:

  • Swap partition: A part of your hard drive just for swap.
  • Swap file: A regular file that acts like memory.

We’ll use a swap file because it’s easier and doesn’t need changes to disk partitions.


Why Add Swap?

Many small VPS servers come with low RAM—like 512MB or 1GB. That can fill up fast, especially if you’re:

  • Running a web server like Apache or Nginx
  • Using MySQL or MariaDB
  • Hosting WordPress or other apps

Without swap, your system might:

  • Kill processes (including your website)
  • Freeze or reboot
  • Refuse to run updates

Swap helps with all that. It’s not perfect, but it’s better than running out of memory.


Check if You Already Have Swap

Before adding swap, check if one already exists.

Run:

swapon --show

If it returns nothing, there’s no swap.

You can also check memory usage:

free -h

Look under the “Swap” row. If it’s all 0s, you don’t have one yet.


Step 1: Create the Swap File

Let’s say you want 1GB of swap. You can use a different size if you want.

Run this to create the file:

sudo fallocate -l 1G /swapfile

If fallocate doesn’t work, use:

sudo dd if=/dev/zero of=/swapfile bs=1M count=1024

This will also create a 1GB file.

Check that the file was created:

ls -lh /swapfile

You should see something like:

-rw------- 1 root root 1.0G /swapfile

Step 2: Set the Right Permissions

Swap files need secure permissions. Run:

sudo chmod 600 /swapfile

Check again:

ls -lh /swapfile

Now it should show -rw-------.


Step 3: Turn the File into Swap

Now we’ll set it as swap:

sudo mkswap /swapfile

This formats the file as swap.

Then enable it:

sudo swapon /swapfile

To check that it’s working:

swapon --show

Or:

free -h

Now you should see swap listed.


Step 4: Make It Permanent

By default, the swap will go away after a reboot. To fix that, we need to add it to the system’s fstab file.

Run:

sudo nano /etc/fstab

Add this line at the bottom:

/swapfile none swap sw 0 0

Save and close the file (CTRL+O, then ENTER, then CTRL+X).

Now swap will stay active after every reboot.


Step 5: Tweak Swappiness (Optional)

Swappiness tells the system how often to use swap. The default is usually 60, but 10 is better for most VPS setups.

To change it:

sudo sysctl vm.swappiness=10

To make it permanent:

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

You can also check the current value with:

cat /proc/sys/vm/swappiness

Lower numbers mean the system uses swap less.


Benefits of Using Swap

Here’s what I like about adding swap to my Ubuntu VPS:

  • It helps avoid crashes when RAM is full
  • It keeps web apps running smoother
  • It’s easy to set up and change
  • You don’t have to resize disks or reinstall anything

It’s a small change that makes a big difference, especially on low-RAM servers.


Things to Watch Out For

Swap is useful, but it’s not perfect. Here are a few things to keep in mind:

  • Swap is slower than RAM
  • If your server uses swap too much, it may feel slow
  • Don’t use swap as a way to avoid upgrading RAM forever

Some people use swap on SSD VPS. It works, but too much writing to SSD can wear it out over time. That said, I’ve done it, and it’s fine for light use.


Two Handy Lists

Good Times to Add Swap

  • Running WordPress or a CMS on a 1GB VPS
  • Using MySQL or MariaDB with low memory
  • Experiencing “Out of Memory” errors
  • Having trouble with updates or installs
  • Running a game server or app server

Not-So-Good Times to Rely on Swap

  • Using a database under heavy load
  • Running multiple memory-heavy apps
  • Ignoring memory upgrades completely
  • Seeing constant swap use in free -h

A Quick Comparison

Feature RAM Swap File
Speed Very fast Much slower
Cost VPS RAM is limited Uses disk space
Useful When… Enough for most tasks RAM runs out
Limitations Needs more $$ to upgrade Can slow system

As you can see, swap is not a RAM replacement. It’s a backup.


Final Thoughts

If you have a small VPS, adding a swap file is one of the first things I recommend. It’s quick, doesn’t cost anything, and can prevent downtime.

You’ve now learned:

  • What swap is and why it helps
  • How to create a swap file
  • How to make it permanent
  • How to tune swappiness

Want to test your server now? Try running a few memory-hungry tasks or restarting a service. Then run free -h to see if swap is being used.

Do you feel more confident setting it up now?

Leave a Reply