Manage VirtualBox with PHP

Jan 4, 2020 by Thibault | 545 views


phpVirtualBox is a well known web interface for managing virtual machines. There is however another less known library that allows to manage virtual machines from your own PHP applications : php-vbox-api. The API allows you to do stuff like:

$vbox = new VBox($username, $password);

// a single .ova may contain multiple machines
$vms = $vbox->import("/path/to/image.ova");
$vm = $vms[0];

$vm->setMemorySize(512); // MB

$adapter = $vm->getNetworkAdapter(0);



The library itself is best installed using composer:

composer require cylab/php-vbox-api

VirtualBox Web Service

You will of course have to download and install VirtualBox.

In VirtualBox, each virtual machine is a separate user process. So it is usually best to create a dedicated user for running your VM's :

sudo adduser vbox

Then add this new user to the vbox group so it will be allowed to run virtual machines :

sudo adduser vbox vbox

Finally, you have to activate the VirtualBox Web Service.

Therefore, you have to create the file /etc/default/virtualbox and indicate which user should be used to run your machines:


You can now start the VirtualBox Web Service:

sudo service vboxweb-service restart

By default the web service will be listening on port 18083 :


Connecting to VirtualBox

$vbox = new VBox("vbox", "passord-of-vbox-user");

List all machines

$vms = $vbox->allVMs();
foreach ($vms as $vm) {
  echo $vm->getName() . "\n";
  echo $vm->getUUID() . "\n";

Modify a VM

$vm = $vbox->findVM("name or UUID");

// do a clean shutdown

// do a hard shutdown

// destroy the VM

You can find more examples on

You might also like...