Swap file – Beginners guide to Digital Ocean

Ernest Marcinko Hosting, Tutorials 3 Comments

If you reached this step, your server is up and running flawlessly. However after gaining some traffic it will run out of memory and some of the processes will fail. To avoid that, we need a swap file.

If you need more information I recommend reading the original swap file tutorial on digital ocean. This article is basically a shorter version of that.

Creating the swap file

Before creating a swap file, we need to figure out it’s size. The proper size of the swap file should be the amount of memory your droplet has. If you created a 1 core 1 gigs of ram droplet, you should create a swap file of 1 gigabytes. To do that, use the following command:

sudo fallocate -l 1G /swapfile

You can change the 1G to the desired amount.

To make this swapfile usable, you need to change it’s permissions:

sudo chmod 600 /swapfile

Nice. Now let’s tell the system to use this file as a swap file:

sudo mkswap /swapfile

It should print out some information about the allocated memory and stuff. Now, let’s turn on swapping:

sudo swapon /swapfile

Making the swap file permanent

Right now swapping is enabled, but after rebooting it’s not. We need to tell the system to use this swap file after rebooting.

Open up the fstab file in nano:

sudo nano /etc/fstab

Go to the end of the file and add this line:

/swapfile   none    swap    sw    0   0

Save it and it’s done. Now your system has a fully functional swap file.

Optional steps

There are two more options that can fine tune your swap file, you can read more about them on the swap file tutorial on digital oceans website.


The swappiness configures how often will the server change the data between the swap file and the memory. It’s a value between 0-100. On values close to 0 the server will try to fill the memory first before using the swap, with values around 100 it’s the other way around. For general use web servers 10 is a great value. Usually that’s the default.

But to make sure you can check it by running this command:

cat /proc/sys/vm/swappiness

This should return the value. To change the value permanently, let’s open up the file:

sudo nano /etc/sysctl.conf

and add this line at the bottom of the file:


Save the file and reboot the droplet completely:

sudo shutdown -r now

This will terminate your current connection as well.

VFS Cache pressure

By default ubuntu tries to cache some of the inode information – access data of your file system. Operations on the inode table are resource heavy, so caching it is a good option for most cases.

There is an option called vfs_cache_pressure which configures the caching other data over the inode table. It’s value is usually set to 100. For better performance we can try to change it to 50, which will prioritize caching of the inode table.

To change that open up the sysctl.conf file:

sudo nano /etc/sysctl.conf

and to the end of that file put this line:

vm.vfs_cache_pressure = 50

Save the file and reboot the droplet completely:

sudo shutdown -r now

This will terminate your current connection as well.


<< Chapter #7

Comments 3

  1. steve

    Hi Earnest,I followed your guide when setting up my server and it was really helpful, only bit I added not on here is webmin for users who don’t like working from terminal and would like from a familiar dash like cpanel.All in all,great post!

Leave a Reply

Your email address will not be published.