Skip to content

Instantly share code, notes, and snippets.

Last active February 12, 2018 18:49
  • Star 14 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Vagrant snippet to set VirtualBox guest CPU count to the number of host cores on Linux or OS X (broken 2018)
gem 'concurrent'
require 'concurrent'
config.vm.provider "virtualbox" do |vb|
v.cpus = Concurrent.physical_processor_count
Copy link

CpuID commented Jul 10, 2013

Works well :)

Copy link

amberj commented Jul 26, 2013

You can use this snippet:
grep "^processor" /proc/cpuinfo | wc -l
instead of:
awk "/^processor/ {++n} END {print n}" /proc/cpuinfo 2> /dev/null || sh -c 'sysctl hw.logicalcpu 2> /dev/null || echo ": 2"' | awk \'{print \$2}\'

Copy link

Great stuff.

Copy link

aygee commented May 9, 2014

You can use this to get the count of physical cores only

grep "^core id" /proc/cpuinfo | sort | uniq | wc -l | tr -d '\n'

Copy link

or just use nproc.to_i :)

Copy link

Trophime commented Dec 6, 2017

cpus = cpus / 2

This becomes problematic when cpus=1. What shall I do to prevent cpus to return 0? Is there some kind of min/max functions somewhere?

Copy link

updated for 2018

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