Quick tip, vagrant with less RAM

Vagrant is a popular solution to isolate your development environment, while there are many images to use with vagrant the most popular is an Ubuntu server image cooked by HashiCorp. Although with the current computer specs having a virtual machine that uses 512MB in RAM is almost nothing (I bet Google Chrome is wasting more than that now) if you are like me you don’t like to have a Virtual Machine wasting resources.

The main reason of why vagrant images uses that amount of RAM is because they don’t have SWAP, so, if your program/script is using more RAM than what is in the VM the program/script is killed.

You can add a SWAP file by using dd

$ dd if=/dev/zero of=/swapfile bs=1024 count=655360
$ chmod 00600 /swapfile
$ swapon -a

Then add it to /etc/fstab. Pretty simple, but you have a big file that may or may not be used in totality.

A better approach is to use swapspace daemon, it will create a swap space that will grow/shrink as needed. You can install the service in your VM if you already have it working or, add this to your Vagrantfile to have it done automagically.

config.vm.provision "shell", inline: "sudo apt install swapspace -y"

If you do this with a VM already working just shut it down, use vagrant up --provision to run the provision instruction and you are done!.

Having a Swap space allows you to reduce the amount of RAM dedicated to your VM.