How to Add | Increase Swap Space to Ubuntu 18.04 | 16.04

This brief tutorial shows students and new users how to add swap space on Ubuntu 18.04 | 16.04.

Swap space is a location on the system’s disk drive that is used when the system’s physical memory (RAM) is full. When this happens, pages in this systems’ RAM that are inactive are moved to a swap space to make room for active pages.

You can either have a swap file or dedicated swap partition on the system’s disk.

If you’re a student or new user looking for a Linux system to start learning on, the easiest place to start is Ubuntu Linux OS…. It’s a great Linux operating system for beginners and folks looking for easier Linux distribution to use.

Ubuntu is an open source Linux operating systems that runs on desktops, laptops, server and other devices…

When using Ubuntu, you will find that Linux isn’t so different than Windows and other operating systems in so many ways, especially when it comes to using the system to get work done.…

Both Ubuntu and Windows systems allow you to be productive, easy to use, reliable and enable you to install and run thousands of programs from gaming to productivity suite software for individuals and businesses.

If you don’t already a swap file or want to create one, follow the steps below:

View Current Swapfile

To get started, first use the commands below to show your system’s current file swapfile settings.

sudo swapon --show

If you run the command above and the output is emptied, then your system doesn’t currently have a swapfile configured.

A system with swap space enabled and configure should display similar lines as below:

/swapfile   file    1.4G    0B     -2

If your system does have a swapfile but want to increase it, you can use the same steps below to increase it.

First, turn off the current swapfile by running the commands below:

sudo swapoff -v /swapfile

After turning it off, go and remove its entry from the /etc/fstab file.

Run the commands below to open the /etc/fstab file.

sudo nano /etc/fstab

Then comment out the highlighted line or remove it entirely.

# /etc/fstab: static file system information.
# Use 'blkid' to print the universally unique identifier for a
# / was on /dev/sda1 during installation
UUID=b08fba81-8766-4c77-a965-05dd2224c125 /               ext4    errors=remount-ro 0       1
#/swapfile                        none            swap    sw              0       0

After that, save the file and exit.

When you’re done, run the commands below to delete current swapfile from your system.

sudo rm /swapfile

Creating New Swapfile

Now that the system’s current swapfile is removed, run the commands below to create a new one.

For this tutorial, we’re going to be creating a new swapfile of 2G space.

To do that, run the commands below:

sudo fallocate -l 2G /swapfile

Next, run the commands below to create a new swapfile.

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

You should see similar output as below when successful:

2097152+0 records in
2097152+0 records out
2147483648 bytes (2.1 GB, 2.0 GiB) copied, 7.10013 s, 302 MB/s

When you’re done, run the commands below to make sure only the root user is able to read and write to the swapfile.

sudo chmod 600 /swapfile

Next, use Linux mkswap command utility to make that location on the disk a swapfile.

sudo mkswap /swapfile

You should then see a similar output as below:

Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=cc5d179d-4c4c-497a-82f2-c1dc8eb68e26

Finally, activate the swapfile by running the commands below:

sudo swapon /swapfile

Next, go back to the /etc/fstab file and un-comment the line to enable the new swapfile or add a new line:

sudo nano /etc/fstab

Next, verify that the new swapfile is active by running the commands below:

sudo swapon --show

If everything is successful, you should see a new swapfile as below:

/swapfile    file     2G     0B     -2

That’s how one creates a new or increase a swapfile on Ubuntu


This post shows you how to create or increase a swapfile on Ubuntu 18.04 | 16.04 or other Linux systems. If you find any errors above, please use the comment form to report them.

You may also like the post below: