Home Vagrant How To Increase Memory And CPU On Vagrant Machine In Linux

How To Increase Memory And CPU On Vagrant Machine In Linux

By sk
Published: Updated: 52.5K views

Having performance issues with your Vagrant machine? It is probably occurs due to 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.

Introduction

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.

dnf command killed
dnf command killed

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
Define RAM size and CPU count in Vagrantfile
Define RAM size and CPU count in Vagrantfile

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 "end" line.

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

Press 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

Samle output:

               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
Check RAM size and CPU core count in Linux
Check RAM size and CPU core count in Linux

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!

You May Also Like

2 comments

Rafael July 27, 2022 - 6:20 pm

Hi!
It’s helpme. thaks!
But how to increase size of disk with libvirt?

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More