Here you will learn how to add swap space in Ubuntu 18.04 LTS. You may want to do this because you are getting more than your fair share of out-of-memory errors on your server.
Swap is a portion of your hard drive that has been set aside by the operating system to temporarily store data that cannot be stored in RAM due to space.
The problem with swap is that it is significantly slower than RAM so it is not ideal for the operating system to use it for running programs but rather it is usually used to hold older data that is accessed less frequently by other services.
Nevertheless when your server RAM is depleted the operating system is more than happy to use swap space as a fallback.
Warning: Using swap space on SSDs is not recommended as it can cause degradation of the disk over time. Therefore swap is only recommended on systems with traditional spinning hard disk drives.
Get Started With Adding Swap Space in Ubuntu 18.04 LTS
Step 1: Check if Swap is Already Enabled
The first thing you will want to do is to check if swap space has already been added to your system. You can check for swap by typing:
$ sudo swapon --show
If you do not get any output it means your system does not have swap space otherwise you will get something similar to this:
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 1G 364M -2
You can also use the free tool to check for active swap.
$ free -h
The output will look something similar to this:
total used free shared buff/cache available
Mem: 981Mi 129Mi 640Mi 0.0Ki 211Mi 714Mi
Swap: 1G 364M 0B
Step 2 – Check for Potentially Available Space on the Hard Drive Partition
Swap has to be created on the physical disk so it is important that you make sure the space exists in the first place. You can do so by entering:
$ df -h
Your output will look something similar to this.
Filesystem Size Used Avail Use% Mounted on
udev 474M 0 474M 0% /dev
tmpfs 99M 932K 98M 1% /run
/dev/vda1 40G 1.4G 38G 3% /
tmpfs 491M 0 491M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 473M 0 491M 0% /sys/fs/cgroup
/dev/vda15 104M 3.9M 101M 4% /boot/efi
/dev/loop0 56M 55M 0 100% /snap/core18/1705
tmpfs 99M 0 99M 0% /run/user/1000
Take note of the device with /
in the Mounted on the column. Ideally, that is where you will want to get your space from. Be diligent in determining how much space you will want to use for swap space.
A good starting point to the amount of swap you want is usually the same amount as RAM or double the amount up to 4G. If you need 4G of swap it might be a sign that you need to add physical RAM to your system instead of adding such a large amount of swap.
Step 3: Add Swap Space
Seeing it is possible to have multiple swap files or swap partitions, we will not be featuring such a setup. In this guide, we will stick to one swap file.
We are going to use the fallocate program to create a swap file called swapfile in the root /
directory.
In this example, we are creating a 2G file by running this command.
$ sudo fallocate -l 1G /swapfile
Next, we verify that the correct amount of space was reserved by typing:
$ ls -lh /swapfile
The output should be similar to this depending on the size you chose.
$ -rw-r--r-- 1 root root 1.0G Jun 10 11:14 /swapfile
Step 4: Enable the Swap File
Before we actually turn the file into swap space, we need to restrict permissions to the space to users with root privileges.
$ sudo chmod 600 /swapfile
Next, we mark the file as swap space by running the following:
$ sudo mkswap /swapfile
The following is the output:
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=ce45582-0a2a-450f-cdd4-527e54026db7
Finally, we enable the swap file so that our system can use it
$ sudo swapon /swapfile
Once again verify that the swap is available:
$ sudo swapon --show
Here’s the output:
NAME TYPE SIZE USED PRIO
/swapfile file 1024M 0B -2
Using the free utility:
$ free -h
Here’s the confirmed output:
total used free shared buff/cache available
Mem: 981Mi 115Mi 652Mi 0.0Ki 213Mi 714Mi
Swap: 1.0Gi 0B 1.0Gi
Step 5: Make the Swap File Permanent
It is important to note that the swap we created is only valid for the current session. Therefore if we reboot, the server will lose the swap settings. To resolve this we need to add the swap file to our /etc/fstab
file.
First thing, as good practice, back up the /etc/fstab
file.
$ sudo cp /etc/fstab /etc/fstab.bak
Add the swap information to the tail end of your /etc/fstab using this command:
$ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Step 6: Optimize your Swap Settings
You will need to change the swappiness parameter. This parameter determines how often your system swaps data out of RAM to the swap space. It should be set as a percentage anywhere between 0 and 100. The default is 60.
Values closer to zero, the system will not swap data to the disk unless absolutely necessary. Values that are closer to 100 put as much data as possible into swap in an effort to keep more RAM space free. You can test your swappiness by running the following:
$ cat /proc/sys/vm/swappiness
By default it will output:
60
The general rule is for a Desktop, a swappiness setting of 60 is a good value. For a server environment values closer to zero are ideal.
You can change swappiness by running the following:
$ sudo sysctl vm.swappiness=10
$ sudo sysctl vm.swappiness=10
With the successful output being:
vm.swappiness = 10
This value is only valid for the current session until the server is rebooted. To make it more permanent you need to edit the /etc/sysctl.conf file by running:
$ sudo nano /etc/sysctl.conf
Then add this line at the end of the file:
vm.swappiness=10
Save and close the file to make the changes permanent. Now, that is how to add swap in Ubuntu 18.04 LTS.
Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.