Quick and Dirty Swap File (Linux)

Quick and Dirty Swap File (Linux)

Phill Luby
17th September 2025

Home Insights Quick and Dirty Swap File (Linux)

Need a swap file on a Virtual Machine that didn’t come with one? Don’t care about the size, name or location? This is for you…

#!/bin/bash
fallocate -l 1G /swap
chmod 0600 /swap
mkswap /swap
swapon /swap
echo "/swap none swap sw 0 0" >> /etc/fstab

I use lots of tiny virtual servers for development and testing and on most hosting providers they don’t come with a swap file by default. Sometimes I need a little more memory to get over a hump, such as installing or compiling software, but not for general operation so a bigger machine is overkill. I found lots of step-by-step guides online but I couldn’t find a full cut & paste script very easily online so I’ve posted one here.

Not for use in production, unless the size and location all happen to match your requirements.

Share Article

Insights.

How to Run a Code Club
How to Run a Code Club

We’re hosting a Code Club community training evening to allow anyone who is thinking of becoming a Code Club volunteer an opportunity to find out more about Code Club and get an insight into what to expect and how to… 

Discover More
Building RaceBest
Building RaceBest

This year saw the launch of a new service from Leeds-based start-up RaceBest Ltd. NewRedo were responsible for building their online system and we continue to host and support it. In building the system we utilised some of the latest… 

Discover More
ASP.NET MVC on Ubuntu with NGINX
ASP.NET MVC on Ubuntu with NGINX

We’ve just published an early working version of an on-line service created by NewRedo, however, although written in ASP.NET MVC 2, it’s not running on a Microsoft server. Instead we’re running on Ubuntu 11.10 using the NGINX web server. First,… 

Discover More