Bobcares

Add swap space on Digitalocean Droplet | Simple Steps

by | Sep 12, 2022

In today’s article “Add swap space on Digitalocean Droplet”, let’s learn how to make the server more secure and responsive against out-of-memory errors with help of our DigitalOcean Managed Services support at Bobcares.

How To Add Swap Space on Droplet

 

Swap is an area of hard drive storage that has been set aside for the operating system to temporarily store data that it can no longer hold in memory RAM. This lets you increase the amount of information that your server can keep for working memory, with some caveats.

Add swap space on Digitalocean Droplet

Creating a Swap File

 

First, we will verify that you have available hard drive space on the system, then we can create a swap file on our filesystem. We will further allocate a file of the size that we want callingswapfile in our root (/) directory.

 

The suggested way of creating a swap file is with the ” fallocate ” program, this command creates a new file of the specified size.

 

In this guide, we will create a 1G file. You can adjust this to meet the needs of your server by below command:

 
sudo fallocate -l 1G /swapfile
 

Verify that the correct amount of space has reserved:

 
ls -lh /swapfile
 
-rw-r--r-- 1 root root 1.0G Apr 25 11:14 /swapfile
 

Now our file has been created with the correct amount of space set aside.

 

Enabling the Swap File

 

As we have a file of the correct size available, we need to actually convert this into swap space.

 

First, we need to create permissions for the file so that only users with root privileges have access to read the contents. This prevents other users to access the file, which locks with significant security.

 

Make the file only accessible to the root :

 
sudo chmod 600 /swapfile
 

Next, verify the permissions change:

 
ls -lh /swapfile
 
Output-rw-------1 root root 1.0G Apr 25 11:14 /swapfile
 

By this, only the root user has read and write flags enabled. We can next mark the file as swap space:

 
sudo mkswap /swapfile
 
OutputSetting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=6e965805-2ab9-450f-aed6-577e74089dbf
 

After marking the file we need to enable the swap file, to allow our system to start using it:

 
sudo swapon /swapfile
 

Verify that the swap is available:

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

We can again check the output of the free utility to see our findings, the result will look like this:

 
free -h
 
Output              total        used        free      shared  buff/cache   available
Mem:          981Mi       123Mi       644Mi       0.0Ki       213Mi       714Mi
Swap:         1.0Gi          0B       1.0Gi
 

Our swap have completely set up and the operating system will begin to use it as necessary.

 

Making the Swap File Permanent

 

Our recent changes should have enabled the swap file for the current session. However, if we reboot the server this will not retain the swap settings automatically. Further, we can change this by adding the swap file to our /etc/fstab file.

 

For the safer side first back up the /etc/fstab file if in case anything goes wrong:

 
sudo cp /etc/fstab /etc/fstab.bak
 

Finally, add the swap file information to the end of your /etc/fstab file:

 
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
 

By this, you will be successfully configured with a permanent swap file system.

 

[Looking for a solution to another query? We are just a click away.]

 

Conclusion

 

Following the steps in this Add swap space on Digitalocean Droplet guide, will show how you can avoid out-of-memory exception errors. If you find that your system is unable to use the applications, then the best solution is to optimize your application configurations or upgrade your server.

 

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.