Add Swap Space On Ubuntu 18.04

Step 1 – Checking the System for Swap Information

A 1gb swap file maybe required to run Simple Cloud Node on a Ubuntu 18.04 VPS depending on your VPS Spec.
To check if your current VPS has swap configured. Login via SSH to your VPS:
1
$ sudo swapon --show
Copied!
If you don’t get back any output, this means your system does not have swap space available currently.
1
$ free -h
Copied!
1
Output
2
total used free shared buff/cache available
3
Mem: 985M 84M 222M 680K 678M 721M
4
Swap: 0B 0B 0B
Copied!
As you can see in the Swap row of the output, no swap is active on the system.

Step 2 – Checking Available Space on the Hard Drive Partition

Before we create our swap file, we’ll check our current disk usage to make sure we have enough space. Do this by entering:
1
$ df -h
Copied!
1
Output
2
Filesystem Size Used Avail Use% Mounted on
3
udev 481M 0 481M 0% /dev
4
tmpfs 99M 656K 98M 1% /run
5
/dev/vda1 25G 1.4G 23G 6% /
6
tmpfs 493M 0 493M 0% /dev/shm
7
tmpfs 5.0M 0 5.0M 0% /run/lock
8
tmpfs 493M 0 493M 0% /sys/fs/cgroup
9
/dev/vda15 105M 3.4M 102M 4% /boot/efi
10
tmpfs 99M 0 99M 0% /run/user/1000
Copied!
The device with / in the Mounted on column is our disk in this case. We have plenty of space available in this example (only 1.4G used). Your usage will probably be different.
Although there are many opinions about the appropriate size of a swap space, it really depends on your personal preferences and your application requirements. Generally, an amount equal to or double the amount of RAM on your system is a good starting point. Another good rule of thumb is that anything over 4G of swap is probably unnecessary if you are just using it as a RAM fallback.

Step 3 – Creating a Swap File

Now that we know our available hard drive space, we can create a swap file on our filesystem. We will allocate a file of the swap size that we want called swapfile in our root (/) directory.
The best way of creating a swap file is with the fallocate program. This command instantly creates a file of the specified size.
Since the server in our example has 1G of RAM, we will create a 1G file in this guide. Adjust this to meet the needs of your own server:
1
$ sudo fallocate -l 1G /swapfile
Copied!
We can verify that the correct amount of space was reserved by typing:
1
$ ls -lh /swapfile
Copied!
1
$ -rw-r--r-- 1 root root 1.0G Apr 25 11:14 /swapfile
Copied!
Our file has been created with the correct amount of space set aside.

Step 4 – Enabling the Swap File

Now that we have a file of the correct size available, we need to actually turn this into swap space.
First, we need to lock down the permissions of the file so that only the users with root privileges can read the contents. This prevents normal users from being able to access the file, which would have significant security implications.
Make the file only accessible to root by typing:
1
$ sudo chmod 600 /swapfile
Copied!
Verify the permissions change by typing:
1
$ ls -lh /swapfile
Copied!
1
Output
2
-rw------- 1 root root 1.0G Apr 25 11:14 /swapfile
Copied!
As you can see, only the root user has the read and write flags enabled.
We can now mark the file as swap space by typing:
1
$ sudo mkswap /swapfile
Copied!
1
Output
2
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
3
no label, UUID=6e965805-2ab9-450f-aed6-577e74089dbf
Copied!
After marking the file, we can enable the swap file, allowing our system to start utilizing it:
1
$ sudo swapon /swapfile
Copied!
Verify that the swap is available by typing:
1
$ sudo swapon --show
Copied!
1
Output
2
NAME TYPE SIZE USED PRIO
3
/swapfile file 1024M 0B -2
Copied!
We can check the output of the free utility again to corroborate our findings:
1
$ free -h
Copied!
1
Output total used free shared buff/cache available
2
Mem: 985M 84M 220M 680K 680M 722M
3
Swap: 1.0G 0B 1.0G
Copied!
Our swap has been set up successfully and our operating system will begin to use it as necessary.

Step 5 – Making the Swap File Permanent

Our recent changes have enabled the swap file for the current session. However, if we reboot, the server will not retain the swap settings automatically. We can change this by adding the swap file to our /etc/fstab file.
Back up the /etc/fstab file in case anything goes wrong:
1
$ sudo cp /etc/fstab /etc/fstab.bak
Copied!
Add the swap file information to the end of your /etc/fstab file by typing:
1
$ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Copied!

Conclusion

Following the steps in this guide will allow you to install Simple Cloud on a Masternode running with 1GB RAM. Without swap configured Simple Cloud will fail with memory leak errors.
If you are running into OOM (out of memory) errors, or if you find that your system is unable to use the applications you need, the best solution is to optimize your application configurations or upgrade your server.
Last modified 1yr ago