Is it possible to create a swap file for a Linux guest VM managed by Vagrant?
Simply add this line to your vagrant file
Vagrantfile
# Enable Dynamic Swap Space to prevent Out of Memory crashes
config.vm.provision "shell", inline: "sudo apt install swapspace -y"
And then reprovision
vagrant up --provision
How it works
This is a dynamic swap space daemon. There are several, but here is a popular one.
sudo apt install swapspace
To verify it's running
sudo service swapspace status
Excerpt from http://pqxx.org/development/swapspace/
This system daemon for the Linux kernel aims to do away with the need for large, fixed swap partitions or swap files.
When installing a Linux-based system (invariably GNU/Linux) with Swapspace, the usual swap partition can be omitted, or it can be kept quite small. Whenever Swapspace finds during normal system usage that more virtual memory is needed, it will automatically claim space from the hard disk. Conversely, swap space that is no longer needed is freed up again for regular use by the filesystem.
This means that with Swapspace installed, sizing the system's available swap space during installation is no longer a life-or-death choice. It now becomes practical to run GNU/Linux off just a single, big partition--with no disk space lost to regrettable installation choices. The system should also be able to handle the occasional memory-intensive task that takes much more swap space than was originally foreseen, without leaving the same swap space unused and unusable during normal operation as is normally the case.
The memory setting you see is only used to configure the VM's RAM. However, the swap space definition is part of the disk image. This image is provided to you as part of the config.vm.box
definition in Vagrantfile
. In my specific case I noticed that the swap space cannot easily be reconfigured (I only have 1 GByte of swap).
In your case I recommend to change the base image (config.vm.box
), or add a swap-file to your root file system by integrating for example this script into your Vagrantfile
. This is another link which seems worth trying out.