Having performance issues with your Vagrant machine? It is probably because of insufficient Memory or CPU. Simply add more RAM and CPU core to Vagrant machine to improve its performance. This brief guide explains how to increase Memory and CPU on Vagrant machine in Linux from commandline.
The other day I noticed that the dnf package manager keeps getting terminated immediately when I run any package management operations, such as update, install, remove etc., in my Fedora vagrant virtual machine I can't even update the repository lists.
Initially, I thought this issue is related to OS. After couple investigations, I found that the Memory size of the Fedora VM is very low (512 MB to be precise).
So I decided to add more memory and the CPU count to my Fedora Vagrant machine. It worked just fine as expected.
Now let us go ahead and set sufficient Memory size and CPU counts to a VM from commandline.
Increase Memory and CPU on Vagrant machine from commandline
When you initialize a new vargant environment, a configuration file named Vagrantfile is created. This file contains the details of operating system and software requirements.
The actual purpose of a vagrantfile is to describe the type of the virtual machine and how to configure and provision the VMs.
To add memory and CPU to a Vagrant virtual machine, edit Vagrantfile:
$ vi Vagrantfile
Define the RAM size and CPU count like below.
config.vm.provider "virtualbox" do |v| v.memory = 2048 v.cpus = 2 end
As per the above configuration, I have defined 2 GB RAM and 2 CPU core to my Vagrant machine. Make sure you have added these lines before the last
If you're using libvirt provider, you need to update the lines like below:
config.vm.provider "libvirt" do |v| v.memory = 2048 v.cpus = 2 end
ESC key, and type
:wq to save the file and exit.
Now start the Vagrant machine.
$ vagrant up
If the Vagrant machine is already running, just reload it to take effect the changes.
$ vagrant reload
Check the RAM size with command:
$ free -h
total used free shared buff/cache available Mem: 1.9Gi 100Mi 1.7Gi 5.0Mi 153Mi 1.7Gi Swap: 0B 0B 0B
Check the total number of CPU cores using command:
$ nproc 2
As you see in the above output, the RAM size is 2 GB and the CPU count is 2.
There are many ways to find the number of CPU cores. The following guide lists more ways to find the number of CPU cores:
This is how we can set Memory size and CPU count for a Vagrant machine in Linux. We can do more customization through Vagrantfile. We will publish more Vagrant tips in the days to come. Stay tuned!