Creating swap space on Ubuntu

Here’s a quick intro to swap space and how you can create new swap space on Ubuntu 16.04. It will most likely work verbatim on other Ubuntu versions, as well as other distributions of GNU/Linux too.

What is swap space?

Swap space is the common word for memory that gets used by the computer when the main memory (the RAM) is full. It is also known as paging and is supported on most modern operating systems.

The actual swap space is usually allocated to a fixed portion of the disk (the secondary memory) and is generally only used as a last resort since the disk is several orders of magnitude slower than the RAM.

Why do I need it?

In case you ever run out of memory while doing something memory intensive, swap space can save you from the out-of-memory (OOM) killer in Linux that may randomly kill your processes in order to “save” the situation!

I’m running most of my online stuff using DigitalOcean and the VPS:es (Droplets in their terminology) they provide by default does not come with any configured swap at all. In the past I’ve found myself to have run out of memory in unexpected and definitely unwanted situations because of this.

Luckily, all droplets on DigitalOcean come with SSD disks, which makes them reasonably fast for use as extra memory when Linux thinks the time is right.

Do I have it already?

A quick way to see if you have any swap active on your machine is to run free in the console and look at the output:

 total        used        free      shared  buff/cache   available
 Mem:           992M        346M         88M         44M        557M        394M
 Swap:          1.0G        100M        923M

*Note: The above is actually the output of *free -h which shows the values in a more human readable format.

The last line marked Swap would have all three columns set to zero if there would be no swap space available.

How to create swap space

Setting up swap space consists of a few simple steps using builtin utilities.

We verify that we don’t have any swap activated by running free -h again:

 Mem:           992M        453M         64M         45M        474M        287M
 Swap:            0B          0B          0B

Then, we need to find a suitable path for the swap file. I’ve been using /var/swap.img in the past, and will be using it again in this tutorial.

We begin by creating an empty file at /var/swap.img like this:

 $ sudo touch /var/swap.img

It is very important to give the swap file the correct permissions. You do not want anyone except root have access to the swap file, which could potentially cause a huge security hole.

We will set the permissions to 0600 to ensure only root can read and write to the file.

 $ sudo chmod 0600 /var/swap.img

After that, we need to zero the swap file to make it as as big as the amount of swap we want to create. We need to calculate the desired size in bytes and use the dd utility to copy zeroes from the special device /dev/zero into /var/swap.img.

 $ sudo dd if=/dev/zero of=/var/swap.img bs=1024k count=1024

Using a block size of 1024k which is short for 1 024 000 bytes and repeating that 1 024 times will make a file of size 1 048 576 000 bytes or approximately 1048 megabytes.

How much swap space should I create?

That depends on your demands. I’ve found that using the same amount of swap as RAM has worked pretty well, effectively doubling the amount of memory available to the system.

Now that we have our file setup, we can start using the system utilities to mark it as such. mkswap will designate the file as a swap file and swapon will activate it.

 $ sudo mkswap /var/swap.img
 $ sudo swapon /var/swap.img

Running free -h we can see that the swap space has been activated:

 total        used        free      shared  buff/cache   available
 Mem:           992M        458M         77M         45M        455M        282M
 Swap:          1.0G        1.8M        1.0G

Persisting the swap space

Unfortunately these changes are not persisted when you reboot. We need to make an entry in /etc/fstab for that to happen. This is also fairly straight forward, just make sure you edit the file correctly!

We are going to add the line /var/swap.img none swap sw 0 0 at the end of the file. My fstab looks like this after the addition:

 LABEL=cloudimg-rootfs   /    ext4   defaults    0 0
 /var/swap.img    none    swap    sw    0    0

There will be other entries in the fstab and yours might not look exactly like mine. Be careful and just add the one single line pointing to /var/swap.img for everything to work as it should.

Now you can reboot your machine and verify with free that everything was setup correctly!