QEMU, libvirt and virt-manager on an M1 Mac: a troubleshooting deep-dive (with Terraform and Packer)
Tested on macOS (Apple Silicon, arm64) on 12 October 2026 with QEMU 11.1.2, libvirt 12.8.0, virt-manager 5.1.0, Terraform 1.16.5 and Packer 1.16.1, all via Homebrew.
Why bother?
I wanted my Mac to behave like my Linux hypervisors: virt-manager for a GUI, virsh for the command line, and — the real goal — HashiCorp Packer to build VM images and Terraform (with the dmacvicar/libvirt provider) to define virtual machines as code. The usual macOS suggestions are UTM, Lima or Multipass, none of which speak libvirt. If your tooling, playbooks and modules are all built around libvirt, those alternatives mean maintaining two mental models.
So this is the story of getting a proper qemu:///system libvirt connection working on an M1 Mac, every wall I hit on the way, and how Terraform and Packer were wired into it afterwards.
TL;DR — the working recipe
If you just want the answer, here it is; the rest of the post explains why each line matters.
brew install qemu libvirt virt-manager
brew services stop libvirt # the user-level service can never serve qemu:///system
# /opt/homebrew/etc/libvirt/libvirtd.conf
# unix_sock_group = "staff"
# unix_sock_rw_perms = "0770"
# /opt/homebrew/etc/libvirt/qemu.conf
# user = "yourusername"
# group = "staff"
sudo brew services start libvirt # root LaunchDaemon; creates libvirt-sock
Then connect everything to qemu:///system. For Terraform, the provider URI needs an explicit socket parameter:
provider "libvirt" {
uri = "qemu:///system?socket=/opt/homebrew/var/run/libvirt/libvirt-sock"
}
And for Packer, use the QEMU plugin with accelerator = "hvf", qemu_binary = "qemu-system-aarch64", machine_type = "virt" and the EDK2 firmware shipped inside the Homebrew qemu formula.
Now, the long version.
The starting point
Three formulae, one service:
brew install qemu libvirt virt-manager
brew services start libvirt
Fair warning on footprint: virt-manager drags in most of a GTK desktop — around eighty formulae including GTK 3 and 4, gstreamer and ffmpeg — and QEMU itself occupies roughly 730 MB in the Cellar. This is not a lightweight install.
With the service started, pgrep confirmed the daemon was alive:
58326 /opt/homebrew/opt/libvirt/sbin/libvirtd -f /opt/homebrew/etc/libvirt/libvirtd.conf
Everything looked healthy. Then I opened virt-manager and was greeted with:
Could not detect a default hypervisor. Make sure the appropriate QEMU/KVM virtualisation and libvirt packages are installed to manage virtualisation on this host.
Following the dialog’s advice, I tried File → Add Connection, left the hypervisor on the default “QEMU/KVM” entry, and got a rather more revealing traceback:
Unable to connect to libvirt qemu:///system.
Verify that an appropriate libvirt daemon is running.
...
libvirt.libvirtError: Failed to connect socket to
'/opt/homebrew/var/run/libvirt/virtqemud-sock': No such file or directory
Two facts are hiding in that message, and they shape the entire investigation:
- The libvirt 12 client, asked for
qemu:///system, looks for the socket of the modularvirtqemuddaemon — not the monolithiclibvirtdthat Homebrew just started for you. - Nothing on this machine had ever created that socket.
Working out what is actually running
First principles: check the service, the process, and the sockets.
brew services list
# libvirt started davidfield ~/Library/LaunchAgents/sh.brew.libvirt.plist
pgrep -fl libvirtd
# .../sbin/libvirtd -f /opt/homebrew/etc/libvirt/libvirtd.conf
ls -la /opt/homebrew/var/run/libvirt/
# common/ lockd/ network/ qemu/ secrets/ storage/ <- directories, but no sockets
The socket directory contained only empty sub-directories. So where was the daemon actually listening? lsof on the process told the story:
libvirtd 58326 davidfield 8u unix ... /Users/davidfield/.cache/libvirt/libvirt-sock
libvirtd 58326 davidfield 9u unix ... /Users/davidfield/.cache/libvirt/libvirt-admin-sock
There it is. A libvirtd that does not run as root is, by libvirt’s definition, a session daemon. It resolves its runtime directory through the XDG mechanisms — and since macOS has no XDG_RUNTIME_DIR, libvirt falls back to ~/.cache. So the Homebrew service was never a system daemon at all: it was a per-user session daemon wearing a systemd-shaped coat.
A quick virsh comparison confirmed the split:
virsh -c qemu:///system list --all
# error: Failed to connect socket to '/opt/homebrew/var/run/libvirt/virtqemud-sock': No such file or directory
virsh -c qemu:///session list --all
# Id Name State
# --------------------
# (empty, but connected successfully)
qemu:///session works. qemu:///system does not. That asymmetry is the heart of every problem that follows.
The tempting dead end: qemu:///session
At this point there is a perfectly serviceable answer available: use the session URI everywhere. virt-manager has a “QEMU/KVM User Session” entry in its connection dialog, and you can persist the autoconnect:
gsettings set org.virt-manager.virt-manager.connections uris "['qemu:///session']"
gsettings set org.virt-manager.virt-manager.connections autoconnect "['qemu:///session']"
(A gotcha within the gotcha: the schema is org.virt-manager.virt-manager.connections — a schema in its own right. My first attempt passed org.virt-manager.virt-manager plus a two-part key and was rewarded with nothing but a usage message.)
And there was genuinely good news hiding in the session connection. virsh -c qemu:///session domcapabilities revealed that Homebrew’s libvirt build carries macOS patches, and that this stack is far more capable than the error dialog implied:
<domainCapabilities>
<path>/opt/homebrew/bin/qemu-system-aarch64</path>
<domain>hvf</domain>
<machine>virt-11.1</machine>
<arch>aarch64</arch>
<os>
<enum name="firmware"><value>efi</value></enum>
<loader>/opt/homebrew/Cellar/qemu/11.1.2/share/qemu/edk2-aarch64-code.fd</loader>
</os>
...
Three things worth pausing on:
- There is no KVM on macOS. The Apple equivalent is the Hypervisor.framework, which QEMU exposes as the
hvfaccelerator.qemu-system-aarch64 -accel helpon this machine reportshvfandtcg— so arm64 guests run near-natively rather than under emulation. - Homebrew’s libvirt treats
hvfas a first-class domain type, the way Linux libvirt treatskvm. You write<domain type='hvf'>and libvirt does the right thing. - UEFI firmware ships in the bottle.
edk2-aarch64-code.fdis sitting in the qemu formula’s share directory, ready for arm64 guests that need EFI (which is all of the Ubuntu cloud images).
So the session route works, and if you only want virt-manager for the odd VM, you can stop here. But I wanted the standard qemu:///system connection — the one every tutorial, every provider default and every colleague’s muscle memory assumes — and that meant understanding why it could not exist yet.
How libvirt decides what to connect to
Two pieces of libvirt’s architecture control everything that happens next.
Client side. When a libvirt 12 client resolves qemu:///system on a local UNIX transport, the selection logic in src/remote/remote_sockets.c (remoteGetUNIXSocket) runs in “auto” mode:
- If
/…run/libvirt/virtqemud-sockexists, connect to the modular daemon (direct mode). - Otherwise, if
/…run/libvirt/libvirt-sockexists, fall back to the monolithic daemon (legacy mode). - Otherwise, use the build-time default — which for this Homebrew build points at the modular socket that does not exist.
That fallback is the key that unlocks the eventual fix: the client is perfectly happy to talk to a monolithic libvirtd for qemu:///system, as long as virtqemud-sock isn’t lying around misleading it.
Server side. libvirt’s privilege model is non-negotiable: a daemon running as root serves the /system URI; a daemon running as any other user is a session daemon and serves /session only. When an unprivileged daemon receives a /system request it rejects it outright — remember this, it becomes important in a few paragraphs.
Attempt two: the modular daemons
The Homebrew formula ships every modular daemon — virtqemud, virtstoraged, virtsecretd, virtlogd, virtlockd, virtnetworkd, virtproxyd — along with a .conf file for each. My plan was sound enough: run the modular set as my user, with their sockets pointed at /opt/homebrew/var/run/libvirt/, exactly where the client was looking.
Each daemon config has the socket directory commented out around line 56:
#unix_sock_dir = "/opt/homebrew/var/run/libvirt"
I uncommented it in virtqemud.conf, virtstoraged.conf and virtsecretd.conf (virtlogd/virtlockd have no such knob — they use compiled-in paths) and smoke-tested them one at a time.
Failure number one. virtqemud, virtstoraged and virtsecretd all died during state initialisation:
error : virPidFileAcquirePathFull:411 : Failed to acquire pid file
'/Users/davidfield/.cache/libvirt/qemu/run/driver.pid': Resource temporarily unavailable
error : virStateInitialize:676 : Initialisation of QEMU state driver failed
The monolithic libvirtd service was still running, and it held the flock on the per-user state directory. On Linux the monolithic and modular daemons never collide because the system instances run as root with state under /var/lib/libvirt, whilst per-user instances live under the XDG directories. On macOS, everything runs as uid 501, so the modular daemons and the monolithic service were fighting over the same ~/.cache/libvirt state. One of them had to go.
So: brew services stop libvirt, then start the modular set again. This time they came up cleanly, and the sockets landed exactly where the client expected:
srwx------ 1 davidfield admin 12 Oct 19:19 virtqemud-sock
srwx------ 1 davidfield admin 12 Oct 19:19 virtsecretd-sock
srwx------ 1 davidfield admin 12 Oct 19:19 virtstoraged-sock
(Aside: virtstoraged then proved oddly reluctant to die when the experiment was over, shrugging off a SIGTERM and requiring a kill -9. I mention it only so that, if you repeat this, you know you’re not imagining things.)
Failure number two, and the instructive one. With the sockets in place, virsh -c qemu:///system finally got past “No such file or directory” — and straight into a new error:
error: internal error: unexpected qemu URI path '/system', try qemu:///session
This is the privilege wall described above, doing exactly what libvirt’s design says it must: a daemon running as a regular user refuses /system connections, no matter which socket it is listening on. No amount of socket-path plumbing changes it. On macOS, a working qemu:///system requires a root daemon. Full stop.
The fix: run libvirtd as root
Given the client’s fallback logic — prefer virtqemud-sock if it exists, otherwise use libvirt-sock — the simplest architecture is the one Linux distributions have used for a decade: a monolithic libvirtd running as root. Homebrew supports this directly:
sudo brew services start libvirt
That installs a proper LaunchDaemon under /Library/LaunchDaemons, owned by root, which starts at boot. Before starting it, though, three config changes are needed, each of which corresponds to a wall I hit in sequence.
1. Socket group. /opt/homebrew/etc/libvirt/libvirtd.conf, around line 85:
unix_sock_group = "staff"
The default is libvirt, a group that does not exist on macOS. Unless you change this, the daemon has nothing sensible to chown the socket to.
2. Socket permissions. Same file, around line 108:
unix_sock_rw_perms = "0770"
Without PolicyKit (which doesn’t exist on macOS), the compiled default for the read-write socket is 0700. The first start of the root daemon produced srwx------ root:staff, and every non-root connection attempt failed with:
error: Failed to connect socket to '/opt/homebrew/var/run/libvirt/libvirt-sock': Permission denied
With 0770, members of staff — that is, every human user on the machine — can connect with full read-write privileges. Tighten to your taste.
3. QEMU process ownership. /opt/homebrew/etc/libvirt/qemu.conf, around lines 557 and 562:
user = "davidfield" # your username
group = "staff"
macOS has no qemu user, and the Homebrew template’s commented default is root/wheel. Running emulators as root is undesirable anywhere, and hvf is happier in a user context besides. This mirrors what a Linux distro does — root daemon, unprivileged VM processes — just with your own account standing in for the qemu user.
One piece of housekeeping that matters more than it looks: delete any stale modular sockets.
rm -f /opt/homebrew/var/run/libvirt/virtqemud-sock \
/opt/homebrew/var/run/libvirt/virtsecretd-sock \
/opt/homebrew/var/run/libvirt/virtstoraged-sock
Recall the client’s selection order: it prefers virtqemud-sock merely because the file exists. A stale socket from an abandoned experiment sends every fresh connection attempt to a dead endpoint. UNIX sockets do not disappear when their daemon dies.
(For completeness: I also reverted the unix_sock_dir edits in the modular daemon configs, since that approach was abandoned. They’re inert either way — nothing starts those daemons.)
Verifying the fix
With the daemon restarted:
virsh -c qemu:///system version
Compiled against library: libvirt 12.8.0
Using library: libvirt 12.8.0
Using API: QEMU 12.8.0
Running hypervisor: QEMU 11.1.2
The socket listing now shows the shape a Linux admin would expect:
srwxrwx--- 1 root staff libvirt-sock
srwxrwxrwx 1 root staff libvirt-sock-ro
srwx------ 1 root staff libvirt-admin-sock
domcapabilities against the system connection reports the same hvf / virt-11.1 / aarch64 / EFI story as before, so nothing was lost by moving to the root daemon.
For virt-manager, point the persisted settings at the system URI and relaunch:
gsettings set org.virt-manager.virt-manager.connections uris "['qemu:///system']"
gsettings set org.virt-manager.virt-manager.connections autoconnect "['qemu:///system']"
One forensic trick worth knowing for macOS: lsof will not show you the path of a connected UNIX socket, but it will show socket handles. Match the peer handle in the client’s lsof -p output against the accepted-connection handles in sudo lsof -p <libvirtd pid>, and you can prove exactly which daemon a GUI app is talking to. In my case virt-manager’s peer handle matched libvirtd‘s accepted client on libvirt-sock — connected to the system daemon, no error dialog in sight.
Finally, make the shell agree with the GUI:
# ~/.config/fish/config.fish
set -gx LIBVIRT_DEFAULT_URI qemu:///system
# ~/.zshrc
export LIBVIRT_DEFAULT_URI="qemu:///system"
With that, bare virsh list hits the system connection without -c acrobatics.
Caveats and sharp edges
- Brew upgrade wart.
sudo brew services startprints a warning worth keeping in your notes: Homebrew takesroot:adminownership of several libvirt paths (…/Cellar/libvirt/…/sbin,…/opt/libvirt, …) and informs you thatbrew upgrade,reinstalloruninstallof the formula will require manualsudo rmof those paths. Annoying, documented, survivable — but it will surprise you six months from now if you don’t write it down. - NAT networking does not exist.
virsh -c qemu:///system net-list --allshows thedefaultnetwork asinactive/persistent, and it will stay that way: libvirt’s NAT mode leans on dnsmasq and iptables, neither of which exists on macOS. Use user-mode (SLIRP) networking on VM interfaces instead — the guest gets outbound connectivity through QEMU’s built-in network stack. To reach the guest from the host, forward a host port to the guest (SSH on a forwarded port is the usual arrangement). - The session URI still exists, courtesy of libvirt’s on-demand daemon spawning, but everything above standardises on
qemu:///system— one URI, everywhere. - Passwordless sudo. The
sudo brew servicesflow is far smoother ifsudo -nworks. If your machine prompts, expect to type your password at each service operation.
Terraform against local libvirt
With a real qemu:///system connection, the Terraform side becomes straightforward — with one Homebrew-specific trap.
The dmacvicar/libvirt provider (v0.9 and later) is a complete rewrite on the Terraform Plugin Framework. Crucially for us, it is pure Go: it speaks libvirt’s RPC protocol over the UNIX socket directly, with no C library bindings. That means the registry’s darwin_arm64 binary just works on an M1 Mac — no cgo, no DYLD path surgery.
The trap: the pure-Go client’s default socket path for qemu:///system is /var/run/libvirt/libvirt-sock. Homebrew’s prefix is /opt/homebrew/var. Use the URI’s socket query parameter to bridge the two — this is documented provider behaviour, not a hack:
terraform {
required_version = ">= 1.6"
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "~> 0.9"
}
}
}
provider "libvirt" {
uri = "qemu:///system?socket=/opt/homebrew/var/run/libvirt/libvirt-sock"
}
One subtlety about verification: terraform plan with zero resources may not actually configure the provider — Terraform initialises providers lazily. The cleanest connectivity canary is a cheap resource that you actually want anyway. A directory storage pool fits the bill:
resource "libvirt_pool" "vm_lab" {
name = "vm-lab"
type = "dir"
target = {
path = "/Users/davidfield/vm-lab/images"
}
create = {
build = true
start = true
autostart = true
}
}
Then:
terraform init # installs dmacvicar/libvirt v0.9.9
terraform plan # Plan: 1 to add, 0 to change, 0 to destroy.
A plan that reaches “1 to add” has, by construction, opened a working connection to the root libvirtd — the provider cannot plan a pool without one.
A domain definition on this stack looks like this (the v0.9 schema mirrors libvirt’s XML closely — the full disks/interfaces/volumes reference is in the provider docs):
resource "libvirt_domain" "example" {
name = "example"
type = "hvf" # the macOS equivalent of "kvm"
memory = 2048
memory_unit = "MiB"
vcpu = 2
os = {
type = "hvm"
type_arch = "aarch64"
type_machine = "virt"
}
devices = {
interfaces = [
{
type = "user" # SLIRP; libvirt NAT doesn't work on macOS
}
]
}
}
Note type = "hvf" — the same domain type domcapabilities advertised. On Linux you’d write kvm; here, Hypervisor.framework stands in for it.
Packer on QEMU, Apple Silicon edition
Packer’s QEMU plugin builds images by driving qemu-system-* directly — libvirt isn’t involved at all, so none of the daemon archaeology above applies. What applies instead is a small pile of schema realities that packer validate taught me one error at a time.
Install the plugin:
packer init .
# Installed plugin github.com/hashicorp/qemu v1.1.7
# (darwin_arm64 build, fetched automatically)
The gotchas, in the order I met them:
- There is no
archargument.arch = "aarch64"fails with “An argument named ‘arch’ is not expected here.” The emulator is chosen withqemu_binary = "qemu-system-aarch64", and the guest architecture follows from that plusmachine_type = "virt". iso_checksum = "none:"is refused. “unsupported checksum type: none.” Point at the checksum file your image vendor publishes instead — for Ubuntu cloud images:file:https://cloud-images.ubuntu.com/noble/current/SHA256SUMS, which Packer matches against the image filename.cd_filesresolve relative to the directory you run Packer from, not the directory containing the template.packer validate /path/to/cfgfrom your home directory with relative paths fails withstat cloud-init/user-data: no such file or directory. Eithercdinto the template directory religiously or use absolute paths.- Variable references are
var.name— Terraform muscle memory is correct here, but a stray parenthesis (var(name), which HCL reads as a function call) produces a wonderfully misleading “There is no function named ‘var’. Did you mean ‘max’?” Don’t ask me how I know.
The finished, validating template:
packer {
required_plugins {
qemu = {
source = "github.com/hashicorp/qemu"
version = ">= 1.1.0"
}
}
}
variable "image_url" {
type = string
default = "https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-arm64.img"
}
source "qemu" "ubuntu_arm64" {
# Apple Silicon: hvf (Hypervisor.framework) instead of kvm
accelerator = "hvf"
qemu_binary = "qemu-system-aarch64"
machine_type = "virt"
# Base disk: Ubuntu 24.04 LTS arm64 cloud image (preinstalled;
# the cloud-init seed below configures users/credentials on first boot)
disk_image = true
iso_url = var.image_url
iso_checksum = "file:https://cloud-images.ubuntu.com/noble/current/SHA256SUMS"
format = "qcow2"
disk_size = "8G"
# UEFI firmware shipped inside Homebrew's qemu formula
qemuargs = [
["-bios", "/opt/homebrew/opt/qemu/share/qemu/edk2-aarch64-code.fd"],
]
memory = 4096
cpus = 4
headless = true
# cloud-init NoCloud seed CD (CIDATA) — absolute paths, see gotcha 3
cd_files = [
"/Users/davidfield/vm-lab/packer/cloud-init/user-data",
"/Users/davidfield/vm-lab/packer/cloud-init/meta-data",
]
cd_label = "cidata"
# Matches the cloud-init seed — swap for ssh_authorized_keys for real use
ssh_username = "ubuntu"
ssh_password = "packer"
ssh_timeout = "15m"
vm_name = "vm-lab-ubuntu-arm64"
output_directory = "output-ubuntu-arm64"
}
build {
sources = ["source.qemu.ubuntu_arm64"]
# provisioners (shell / ansible) go here
}
The pattern worth internalising is disk_image = true with a cloud image: rather than stepping through an installer, Packer uses the preinstalled cloud image as the base disk, attaches a NoCloud seed CD built from cd_files, boots it under hvf, waits for SSH using the credentials the seed configured, runs your provisioners, shuts down and exports a qcow2. The seed itself is minimal:
#cloud-config
hostname: vm-lab-base
users:
- default
ssh_pwauth: true
chpasswd:
expire: false
list: |
ubuntu:packer
Build it with:
cd ~/vm-lab/packer && packer build ubuntu-arm64.pkr.hcl
The first run downloads roughly 300 MB of cloud image; subsequent runs cache it. Under hvf the boot-and-provision cycle is comfortably quick — this is near-native arm64 execution, not TCG emulation.
The workflow this unlocks
The pieces now compose the way they would on a Linux host:
- Packer builds a golden qcow2 from the Ubuntu cloud image, with whatever baseline provisioning you like.
- Terraform creates a
libvirt_volumeseeded from that image (the v0.9 provider takescreate.content.url, happily accepting afile:///path to local artifacts), then alibvirt_domainoftype = "hvf"with a user-mode interface and a forwarded port for SSH. - Ansible (installed and waiting) configures the running guest over that forwarded port, exactly as it would any remote host.
Build image, define infrastructure, configure guests — each layer replaceable, each layer in version control, all on the laptop.
Final thoughts
Would I recommend this path? With caveats, yes — but go in with open eyes.
libvirt on macOS is very much a second-class citizen. Homebrew’s default service model — a monolithic daemon running as your user — can never serve qemu:///system, which means the out-of-the-box experience for anyone expecting the Linux arrangement is precisely the wall of error dialogs I started with. The fix, a root daemon via sudo brew services, is simple once you understand it, but nothing in the packaging guides you there, and the brew root-ownership wart on future upgrades is a real, if minor, cost.
The reward, though, is real too. Once the daemon is rooted and the sockets are group-writable, everything downstream is boring in the best possible way: virsh behaves, virt-manager autoconnects, the pure-Go Terraform provider plans against the hypervisor, and Packer builds arm64 images at essentially native speed thanks to Hypervisor.framework. No KVM, no NAT networking, no PolicyKit — and yet the workflow is identical to a Linux hypervisor, which was the entire point.
If you just want to run virtual machines on a Mac, use UTM or Lima and save yourself an afternoon. But if your infrastructure is libvirt-shaped — Terraform modules, Packer templates, Ansible playbooks aimed at qemu:///system — this stack holds up, and the troubleshooting above should get you there considerably faster than it got me.
The single most useful mental model, should you take this road: on macOS, everything hinges on which daemon owns which socket, and nothing serving /system will ever run as your user. Get a root libvirtd creating libvirt-sock, keep stale virtqemud-sock files out of its run directory, and the rest falls into place.