How to add swap space on Ubuntu 18.04
We have all had it happen to us. You are in the middle of some server operations and suddenly you get an Out of memory error. A very frustrating error. But what if there is an easy solution for it?
In a typical computer we usually have two basic types of memory.
One being the random access memory (RAM). RAM is used to store data and programs while they are being actively used. If a program or data is not stored in RAM they can not be used. When you turn off your computer the data stored in RAM is lost. This is why we call RAM volatile memory.
The second type of memory, when talking about Linux systems, is swap space.
Swap space
Swap space fulfills one major function and that is substituting disk space for RAM when the actual RAM is filling up and needs more space.
Let us say you have a Computer with 8GB of RAM. Now you start opening many programs. If your RAM does not fill up all the way you are good to go. But what if you open so many programs that your RAM does fill up? This is where swap space comes into play. Without swap space you would need to free up your RAM by closing unnecessary programs until your computer starts responding again. What swap space will do is use pre-allocated disk space and allow the RAM to use that part as “extra” RAM.
How to create swap space on Ubuntu 18.04
Step 1 – Check if swap space already exists
Before we create swap space we should check if the server already has some swap space available. While it is possible to have multiple swap files or swap partitions, one should be enough.
To check if the system has any configured swap space execute the following command:
$ sudo swapon --show
If you do not see any output it means your system does not have any swap space configured. To completely verify this we can use the free
command.
$ free -h
Output total used free shared buff/cache available Mem: 10G 2,3G 5,5G 270M 2,9G 7,9G Swap: 0B 0B 0B
Our swap row says it currently has zero bytes allocated, which means no swap is active.
Step 2 – Creating the swap file
Before we being, let us check if we have enough disk space available to create a swap file.
$ df -h
Output Filesystem Size Used Avail Use% Mounted on udev 5,4G 0 5,4G 0% /dev tmpfs 1,1G 1,4M 1,1G 1% /run /dev/sda1 393G 302G 72G 81% / tmpfs 5,4G 173M 5,3G 4% /dev/shm tmpfs 5,0M 4,0K 5,0M 1% /run/lock tmpfs 5,4G 0 5,4G 0% /sys/fs/cgroup tmpfs 1,1G 28K 1,1G 1% /run/user/121 tmpfs 1,1G 24K 1,1G 1% /run/user/1000
We are looking at our main mount which is /
. We still have plenty of space in our case, 72G. When deciding on the amount of swap space to allocate a good rule of thumb is, double the amount of RAM on your system. Another pretty good rule is that anything above 4G is probably unnecessary. For a nice overview see the table below.
Amount of system RAM |
Recommended swap space |
Recommended swap with hibernation |
---|---|---|
less than 2 GB |
2 times the amount of RAM |
3 times the amount of RAM |
2 GB – 8 GB |
Equal to the amount of RAM |
2 times the amount of RAM |
8 GB – 64 GB |
0.5 times the amount of RAM |
1.5 times the amount of RAM |
more than 64 GB |
workload dependent |
hibernation not recommended |
Now, let us finally start creating the actual swap space. In this guide we will make a swap space of 2G in this guide.
$ sudo fallocate -l 2G /swapfile
Let us verify if the correct amount was reserved
$ ls -lh /swapfile
Output
-rw-r–r– 1 root root 2,0G sep 13 2018 /swapfile
Step 4 Enabling our swap file
In the last step we have reserved space for our swap file, now we need to turn this into swap space.
First, since Linux is all about permissions, we will need to change the permissions of the file we made so that only root
users can read it. If we do not execute this step we could have pretty severe security risks.
$ sudo chmod 600 /swapfile
$ ls -lh /swapfile
Output -rw------- 1 root root 2,0G sep 13 2018 /swapfile
Next, we can tell our file that it is a swap file
$ sudo mkswap /swapfile
Now let us enable our newly created swapfile
$ sudo swapon /swapfile
Check whether the swap is available
$ sudo swapon --show
Output NAME TYPE SIZE USED PRIO /swapfile file 2048M 0B -2
Step 5 Making a permanent swap file
Currently we have a swap file that only exists on this sessions. Which means that if we were to turn off our system, our swap file will disappear.
We can make our swap file by adding it to the </etc/fstab/code>
file.
$ sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab