Increasing memory (RAM) on DigitalOcean Droplets for free*
This post explains how to setup swap area on Debian machines which do not already have it.
As pointed out by a fellow mentor that I forgot to put explanation for '*' in the title, I would like to put it here at the beginning but not as a foot note so that it becomes clearly visible.
*= It denotes that, you actually do not increase the RAM size but by adding a SWAP area you are able to give more working memory to your machine. OS can then offload inactive/less active memory pages to SWAP file on your disk, so that it can manage tasks requiring memory beyond the limited memory on $5 droplet. And, Swap is not RAM.
I like the tiny $5 droplets from DigitalOcean a lot. But the trouble I used to face was frequently running out of memory while compiling or installing dependencies or projects which simple MEAN setups for development/testing. In most cases, without digging things out from logs you won't even know why is your build/install failing. By the way, I use Gitlab CI to automate build pipelines and deployments.
Mostly, I faced memory issues while things are getting installed using apt (Debian Package Manager) or when Angular CLI is compiling the app bundle. I use Debian distros on my droplets. When I faced memory related issues first, I wondered if the droplet had swap file set up?
I quickly checked this by
$ free -m
And the output suggested that there is no swap file provisioned. This is how the output looked -
total used free shared buff/cache available Mem: 492 109 280 4 102 366 Swap: 0 0 0
Well, so we know the solution for this one, yes, we need to setup a swapfile. The no-frills procedure I followed to setup a swap file of 2 gigs is as below -
Allocate 2 Gb to swap file
$ sudo fallocate -l 2G /swapfile
Set appropriate permissions for the swap area file
$ sudo chmod 600 /swapfile
Setup the swap area using the space allocated above
$ sudo mkswap /swapfile
Enable the swap area
$ sudo swapon /swapfile
Veify that swap area has been enabled
$ sudo swapon -s
Filename Type Size Used Priority /swapfile file 2097148 44088 -1
To make the swap file permanent, we need to add it to
/etc/fstabfile. Add the following line to the
/swapfile none swap sw 0 0
Well, that’s it.
So, before you think about upgrading to 1 GB ($10) or 2 GB ($20) droplet for the trivial things which $5 can achieve very well, check if you have a swap file set up and give it a try before going ahead with the upgrade.