Last Updated: February 25, 2016
·
53.68K
· 8xx8ru

Vagrantfile. Set memory and CPU

To set virtual machine memory size use:

config.vm.customize ["modifyvm", :id, "--memory", 2048]

To set virtual machine CPU's count use:

config.vm.customize ["modifyvm", :id, "--cpus", 2]

or to set half of available CPU's count:

config.vm.customize ["modifyvm", :id, "--cpus", `awk "/^processor/ {++n} END {print n}" /proc/cpuinfo 2> /dev/null || sh -c 'sysctl hw.logicalcpu 2> /dev/null || echo ": 2"' | awk \'{print \$2}\' `.chomp ]

7 Responses
Add your response

In Linux to find number of CPU cores you can call nproc command.

over 1 year ago ·

This works on Mac & Linux:

config.vm.customize ["modifyvm", :id, "--cpus", `#{RbConfig::CONFIG['host_os'] =~ /darwin/ ? 'sysctl -n hw.ncpu' : 'nproc'}`.chomp]
over 1 year ago ·

@swrobel As far as I know, sysctl -n hw.ncpu shows the virtual CPU count, but vbox can only go as high as the physical cores, which is sysctl -n hw.physicalcpu_max :)

over 1 year ago ·

@patcon where are you reading that virtualbox only supports physical cores? I'm able to use all 4 logical cores on my Macbook...

over 1 year ago ·

what's wrong with the setting from vagrant documentation?

v.customize ["modifyvm", :id, "--cpuexecutioncap", "50"]
over 1 year ago ·

now you can use these shortcuts:
v.cpus = 4 v.memory = 2048

over 1 year ago ·

Like benjamine said, but a more complete example. This is valid for version 2 of Vagrantfile configuration:


config.vm.provider "virtualbox" do |vb|
    vb.memory = "2048"
    vb.cpus = "2"
end
over 1 year ago ·