How To Add Swap Space on Ubuntu 18.04

This tutorial will explain how to setup swap ram on your machine (Namely Ubuntu 18.04)

Swap is an area on a hard drive that has been designated as a place where the operating system can temporarily store data that it can no longer hold in RAM. Basically, this gives you the ability to increase the amount of information that your server can keep in its working “memory”, with some caveats. The swap space on the hard drive will be used mainly when there is no longer sufficient space in RAM to hold in-use application data.

The information written to disk will be significantly slower than information kept in RAM, but the operating system will prefer to keep running application data in memory and use swap for the older data. Overall, having swap space as a fallback for when your system’s RAM is depleted can be a good safety net against out-of-memory exceptions on systems with non-SSD storage available.

  • To see if we have any swap ram setup, we use the following command: free -h usually give some output like:
              total        used        free      shared  buff/cache   available
Mem:           ....        ....        ....      ......  ..........   .........
Swap:            0B          0B          0B

Or if there is actually any swap ram setup:

              total        used        free      shared  buff/cache   available
Mem:           ....        ....        ....      ......  ..........   .........
Swap:          8.0G          0B        8.0G

Incase you do happen to have any swap memory you can always use: swapon --show to show you any active swapfiles.

Now to create a new swapfile theres a sequence of commands we must execute to dedicate space on your harddrive:

  1. Lets first see how much available storage we have to know how much space we can assign to the swapfile using: df -h Often with output resulting in:
Filesystem      Size  Used Avail Use% Mounted on
udev            3.9G     0  3.9G   0% /dev
tmpfs           798M   85M  713M  11% /run
/dev/vda1        87G  9.9G   73G  13% /          <-- Normally we use the root mounted filesystem
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
tmpfs           798M     0  798M   0% /run/user/0
  1. As you can see ther, in the example, we have 73 GiB available to use however we like, including the for swapfile, to create one, we must make a new file with its size allocated to the amount of M (MB), G (GB), K (KB), we would use: sudo fallocate -l 1G /location_of_swapfile
  2. Next we edit permissions to make sure only the root user can read/write from the swapfile as such: sudo chmod 600 /location_of_swapfile
  3. Now we run mkswap to prep the file for usage as swap ram: sudo mkswap /location_of_swapfile often outputting:
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=9d357042-245b-4123-8fa2-1e7e8b1eb1ab
  1. Now to enable the swapfile we run the command: sudo swapon /location_of_swapfile
  2. Running swapon --show should now show the new swapfile:
NAME                  TYPE  SIZE USED PRIO
/location_of_swapfile file 1024M   0B   -2
  1. To make this change persist through restarts of your machine, we must edit the /etc/fstab file on your machine, running: echo '/location_of_swapfile none swap sw 0 0' | sudo tee -a /etc/fstab This effectively adds it to startup and initialize whenever your machine boots up.

To disable swapfiles simple type in: swapoff /location_of_swapfile, you can freely delete such file now, but be warned you must also remove the newly added line from /etc/fstab so that your machine does not try to initialize the swapfile upon reboot.

1 Like

Just be in case that your drive UUID most likely will not be the same across different systems.