How to create a swap file in Ubuntu 16

Try it in our public cloud & Get $50 Credit
CLAIM NOW

A swap file is a method of extending virtual memory. Unix and Linux systems commonly have a dedicated swap partition available for virtual memory, but occasionally you may be tasked with running a server without one, or with one that isn’t large enough for the server’s tasks. In that case, it’s a good idea to create a swap file as a replacement for or aid to a swap partition.

Getting started

To follow our guide to setting up a swap file, you’ll need:
• 1 Node (Cloud Server or Dedicated Server) running Ubuntu 16.
• Root access to the server

Tutorial

First, verify your system’s current SWAP space configuration by using the swapon command:
swapon -s

With swap available, you’ll see something like this output.
swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 8388604 9820 -1

If running this command produces no output, then your system unfortunately doesn’t have a swap file. In order to add swap space to your system, you can proceed with the following steps.

The first task is to check the available space that your system has. To do so, use the “disk free” command with the “human readable” flag, or df -h.
df -h

df -h
Filesystem Size Used Avail Use% Mounted on
udev 491M 0 491M 0% /dev
tmpfs 100M 4.4M 95M 5% /run
/dev/sda1 9.7G 877M 8.8G 9% /
tmpfs 497M 0 497M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 497M 0 497M 0% /sys/fs/cgroup
tmpfs 100K 0 100K 0% /run/lxcfs/controllers
tmpfs 100M 0 100M 0% /run/user/0

Choose a partition with plenty of space, as swap files are nowadays a gigabyte or more in size. You can now use the fallocate command to create a swap file. In our example below, we’re making a 2GB swap file in the root directory with a filename of swap2.
fallocate -l 2G /swap2

If you system have more than one disk, you may put the swapfile on a disk that have low Input/Output usage. SWAP on a primary disc may slowdown a lot your system in case the Input/Output usage is already high.

A vital step that you should be sure not to skip is to set the new swap file permissions readable and writable by root only.
chmod 600 /swap2

Verify the size and permissions of the file.
ls -lh /swap2

Here’s the output of our example swap file.
ls -lh /swap2
-rw------- 1 root root 2.0G Aug 2 23:04 /swap2

Now that you’ve got the file, you can use it to create a new swap space.
mkswap /swap2

Mount the new swap space as you would a swap partition.
swapon /swap2

You can check that it was mounted successfuly by using the swapon command.
swapon -s

swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 8388604 9820 -1
/swap2 file 2097148 0 -2

It’s time to make this a permanent change by adding the new swap file to your system’s fstab in order for it to mount on boot.

Open the fstab in your favorite file editor.
nano /etc/fstab

Add the following line at the end of the fstab, then save and exit.
swap2 swap swap sw 0 0

Conclusion

You’ve now successfully created a swap file and added to your system. This is a useful skill to know whenever you don’t have access to a proper swap partition. Be sure to invite your friends, and to share this article with them if they’re interested in creating swap file.