Skip to content

Instantly share code, notes, and snippets.

@ozbillwang
Created November 20, 2014 01:27
Show Gist options
  • Save ozbillwang/7834632bb41c5642912e to your computer and use it in GitHub Desktop.
Save ozbillwang/7834632bb41c5642912e to your computer and use it in GitHub Desktop.
Vagrant setting - Use all CPU cores and 1/4 system memory
# http://www.stefanwrobel.com/how-to-make-vagrant-performance-not-suck
config.vm.provider "virtualbox" do |v|
host = RbConfig::CONFIG['host_os']
# Give VM 1/4 system memory & access to all cpu cores on the host
if host =~ /darwin/
cpus = `sysctl -n hw.ncpu`.to_i
# sysctl returns Bytes and we need to convert to MB
mem = `sysctl -n hw.memsize`.to_i / 1024 / 1024 / 4
elsif host =~ /linux/
cpus = `nproc`.to_i
# meminfo shows KB and we need to convert to MB
mem = `grep 'MemTotal' /proc/meminfo | sed -e 's/MemTotal://' -e 's/ kB//'`.to_i / 1024 / 4
else # sorry Windows folks, I can't help you
cpus = 2
mem = 1024
end
v.customize ["modifyvm", :id, "--memory", mem]
v.customize ["modifyvm", :id, "--cpus", cpus]
end
@krishnadasm
Copy link

krishnadasm commented Apr 2, 2022

#Following snippet helped me give all cores to the guest os from a windows machine.

config.vm.provider "virtualbox" do |vb|
    host = RbConfig::CONFIG['host_os']

    if host =~ /darwin/
      cpus = `sysctl -n hw.ncpu`.to_i
    elsif host =~ /linux/
      cpus = `nproc`.to_i
    #Works in windows like a charm
    elsif host =~ /cygwin|mswin|mingw|bccwin|wince|emx/
      cpus = `wmic cpu get NumberOfCores /VALUE`.split("=")[1].to_i
    else
      cpus = 1
    end

    # Customize the amount of memory on the VM:
    vb.memory = "1024"
    vb.cpus    = cpus
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment