Linux

How to Add SWAP Space on Ubuntu 20.04

How to Add SWAP Space on Ubuntu 20.04

How to Add SWAP Space on Ubuntu 20.04 on Google Cloud. Swap space is an extension of virtual memory from a system or real memory server (RAM). If you have swap space configured, your server will behave because it has more memory than it actually has.

This guide helps you configure swap space in Ubuntu 20.04.

This setting was tested on a Google Cloud Compute sample engine running Ubuntu 20.04 with 614 MB RAM. So, this setting will work on cloud or VPS servers or any special server.

Precondition

The calculating machine is running, see Setting up an Instance Calculating Machine with Ubuntu 20.04.

Before Beginning

You need to know how much RAM memory you have on your machine.

The general rules for allocating how much swap space is needed are as follows.

  • Systems with less than 2 GB RAM – twice the amount of RAM.
  • Systems with 2 to 8 GB RAM – the same size as the amount of RAM.
  • Systems with more than 8 GB of RAM – at least 4 GB swap.

Check Space Swap

Make sure your Ubuntu server has swap space. More often virtual machines do not have swap space enabled by default. Check the swap space using the following command.

sudo swapon --show

If you don’t get any output, then your server doesn’t have swap space configured. So, you can continue to add swap memory.

01. Create a Swap File

In the current situation with 614 MB RAM we will make a 1GB swap space.

Create a swap file to increase 1 GB (1G) space. You can also increase the amount of space you need.

sudo fallocate -l 1G /swapfile

02. Adjust the File Swap Permission

Now you have a swap file that is the correct size and can be activated as a swap space.

Continue to set the correct permissions.

sudo chmod 600 /swapfile

03. Configure Swap Space

You can now mark files as swap space with the mkswap command.

sudo mkswap /swapfile

You will receive output similar to this.

Output :

Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=f59595fb-754b-47ae-af6b-8dd6e98654d8

04. Activate Swap Space

Enable swap space and let the system take advantage of this space.

sudo swapon /swapfile

Verify that swap is available by typing:

sudo swapon --show
Output
NAME      TYPE  SIZE USED PRIO
/swapfile file 1024M   0B   -2

You can check the free utility output again.

free -h
Output
               total        used       free        shared      buff/cache  available
Mem:           581M         275M        62M        103M        243M        110M
Swap:          1.0G          0B        1.0G

05. Make Swap Permanent

Now swap space is only made for the current session. If you reboot the server, the current changes will be lost. So you can make this setting permanent by adding swap files to our / etc / fstab file.

sudo nano /etc/fstab

Add the following at the end of the file.

/swapfile swap swap defaults 0 0

Configure Swappiness Value

The swappiness parameter allows you to configure how often your system swaps data from RAM to swap space.

This value will be between 0 and 100 in percentage. The value is close to zero, the kernel will not exchange data to disk unless absolutely necessary. The higher the value is close to 100 will try to put more data into the swap in an effort to preserve more RAM space.

So, telling the system not to rely on swaps will generally make your system faster.

You can see the current value of swappiness by typing:

cat /proc/sys/vm/swappiness
Output
60

Now set the value to 10 with the following command.

sudo sysctl vm.swappiness=10
Output
vm.swappiness = 10

You can create this permanent value by adding a line to your /etc/sysctl.conf file.

sudo nano /etc/sysctl.conf

At the bottom, add the following line.

vm.swappiness=10

Press Ctrl + X followed by Y and Enter to save and close the file.

Removing the Exchange Room

To delete swap space, you need to swap space, delete entries from / etc / fstab file and finally delete swap file.

sudo swapoff -v /swapfile

Edit the / etc / fstab file and delete the default swap / swapfile swap file entry 0 0

Finally, delete the swap file.

sudo rm /swapfile

Conclusion

Now you have learned how to create swap space and configure it and delete swap space on your Ubuntu 20.04 server on Google Cloud.

Thank you for your time. If you encounter a problem or feedback, please leave a comment below.

Related posts

How to use BleachBit on Linux

Linux

How to Install Visual Studio Code on Ubuntu 20.04

Linux

How to restart the network on Ubuntu

Linux

How to Install Memcache on Debian 10

Linux

How to install Grafana Enterprise Edition on Ubuntu Server 20.04

Linux

How to Install Gitea on CentOS 8

Linux

How to Install DEB packages on Ubuntu & Linux Mint

Linux

How to Install Node.js and npm on Ubuntu 20.04

Linux

How to Install and Use FFmpeg on Ubuntu 20.04

Linux