When I first deployed my Ubuntu VPS, the server was completely clean — nothing extra, just the operating system and SSH access under a user with sudo privileges. The very first thing I decided to do was add a swap file, because on VPS servers with limited RAM, the system can easily hit memory limits without swap and start freezing or even crashing.
In this article, I’ll show how I set up a 2 GB swap using the simplest, safest, and most correct approach — a swap file instead of a separate partition. For VPS environments, this is the most optimal solution.
Why Do You Need Swap at All?
Swap is a file (or a partition) that the system uses as additional virtual memory when physical RAM runs out. Yes, it’s slower than real RAM, but in critical situations it can save your server from freezing or being killed by the OOM killer.
Step 1. Checking If Swap Is Already Enabled
Before doing anything, I always check whether swap is already enabled by default. To do this, I run:
free -h
If the Swap line shows zeros everywhere, it means there is no swap configured, and we can safely create our own.
You can also double-check with:
swapon --show
If this command returns no output, swap is definitely not active.
Step 2. Creating a 2 GB Swap File
Now I create a swap file with a size of 2 gigabytes. The fastest way is using fallocate:
sudo fallocate -l 2G /swapfile
This file will be located in the root directory and used exclusively for swap.
⚠️ Important: on some file systems, fallocate may not work properly. If that happens, I use a more universal alternative:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
This method is slower, but it works everywhere.
Step 3. Setting Correct Permissions (Very Important)
The swap file must be accessible only by root. Otherwise, the system will refuse to use it. That’s why I immediately set the correct permissions:
sudo chmod 600 /swapfile
Then I verify them:
ls -lh /swapfile
The output should look like this:
-rw------- 1 root root 2.0G /swapfile
If the permissions are different, something went wrong.
Step 4. Formatting the File as Swap
Next, I turn the regular file into a swap area:
sudo mkswap /swapfile
The system marks the file as swap space, and after that it’s ready to be enabled.
Step 5. Enabling Swap
I activate the swap file with:
sudo swapon /swapfile
And immediately check that everything is working:
free -h
or
swapon --show
If I see 2.0G, it means swap has been successfully enabled and is ready to use 🔥
Step 6. Making Swap Persistent (Survives Reboots)
By default, swap will be disabled after a VPS reboot, so I make sure to add it to autostart.
I open the fstab file:
sudo nano /etc/fstab
And add this line at the very end:
/swapfile none swap sw 0 0
Then I save the file:
Ctrl + OEnterCtrl + X
Now swap will be enabled automatically every time the system boots.
Step 7. Tuning Swappiness (Recommended)
By default, Ubuntu uses swap quite aggressively, which is not always ideal for VPS servers. I prefer the system to use RAM as much as possible before touching swap.
First, I check the current value:
cat /proc/sys/vm/swappiness
Usually, it’s set to 60. For servers, I change it to 10:
sudo sysctl vm.swappiness=10
To make this setting permanent, I open the config file:
sudo nano /etc/sysctl.conf
And add this line at the end:
vm.swappiness=10
Final Result
In the end, I get:
- Ubuntu VPS
- A 2 GB swap file
- Swap working immediately
- Swap survives reboots
- The system doesn’t choke due to memory shortages
This is a simple, proven, and safe method that I use on all of my VPS servers.
If needed, swap can be increased, reduced, or completely removed at any time — without reinstalling the operating system.