# macOS Virtual Machines

> Run macOS 27 virtual machines on Apple Silicon with barn mac — create, connect, share files and the clipboard, and clean up.

---

LLMS index: [llms.txt](/v1.5/llms.txt)

---

Use `barn mac` when you need a clean macOS machine to build, test, or reproduce a macOS-specific bug, and want to throw it away afterwards. Each VM gets an administrator account with passwordless `sudo`, SSH with pinned keys, a desktop window, shared host folders, and clipboard sharing. VMs run under your own user account through Apple's Virtualization framework, so nothing needs `sudo`.

Mac VMs are separate from the Linux lab: they don't read `barn.yml`, their state lives in `$BARN_HOME/mac` (default `~/.barn/mac`), and Linux `barn destroy` or `barn purge` never touches them.

## Requirements {#requirements}

- An Apple Silicon Mac running **macOS 27 or later**, with a logged-in desktop session. Guests also run macOS 27.
- About **65 GiB** of free disk space for the first VM: the IPSW download (23.8 GiB for macOS 27.2.1) plus 40 GiB that Barn requires for the restore. Each VM is an APFS clone of the base image and uses more space as the guest writes to disk. The first base defaults to 100 GiB of capacity.
- **`Barn.app`**, the native app bundle that runs the VMs, from the same release as the CLI. Homebrew and the Apple Silicon release archive include it.

Apple's Virtualization framework runs **at most two macOS VMs at a time** on one Mac, counting VMs from every virtualization tool and an in-progress base-image install. You can keep more stopped VMs on disk and start any two of them, subject to the Apple software license.

## Install {#install}

```bash
brew install pgsty/infra/barn
barn version
barn mac doctor
```

Homebrew installs the signed, notarized `Barn.app` together with the matching CLI and helper; you don't need Go or Xcode. The [installer and manual archive](../installation/) include the app on Apple Silicon too. Stop running Mac VMs before upgrading or replacing the app.

To develop Barn itself, see [Build the Mac component](../source-build/#mac-component).

## Create your first machine {#create-your-first-machine}

```bash
barn mac up
```

On a Mac with no prepared base, Barn 1.4.0 selects **macOS 27.2.1 (`26B131`)** and asks before downloading it:

```text
info: macOS 27.2.1 (26B131) is not prepared on this Mac yet.
download:  23.8 GiB from Apple (updates.cdn-apple.com)
then:      install macOS once into a reusable base (about 20 minutes)
free:      551.2 GiB
Download macOS from Apple now? [Y/n]
```

After you confirm, Barn:

1. **Downloads the restore image from Apple** (`updates.cdn-apple.com`), resuming any partial transfer and checking Apple's published SHA-256.
2. **Installs macOS once into an unbooted base image.** The install uses one of the two VM slots while it runs.
3. **Clones `mac1` from the base** with APFS copy-on-write, boots it, creates your account, and waits until SSH and passwordless `sudo` work.
{.steps}

```text
NAME  ADDRESS      STATE    MESSAGE
mac1  10.10.20.10  running  created and ready; macOS 27.2.1 (26B131), user alice
ready: barn mac ssh mac1
desktop: barn mac open mac1
```

Later VMs clone the same base in seconds. If an earlier release prepared this Mac, run `barn mac image update` to prepare 27.2.1 for new VMs; existing VMs keep their current base.

If you already have Apple's restore image, pass it with `--ipsw` to skip the download. On the same APFS volume Barn clones the file instead of copying it:

```bash
barn mac up --ipsw ~/Downloads/UniversalMac_27.2.1_26B131_Restore.ipsw
```

In scripts, pass `--yes` to allow the download, or run `barn mac setup` beforehand to prepare the base without creating a VM. A new VM started with `--no-wait` returns as soon as its process runs; run `barn mac up NAME` without `--no-wait` to finish SSH and `sudo` setup before using `ssh` or `exec`.

## Work in the machine {#work-in-the-machine}

### Shell and commands {#shell-and-commands}

```bash
barn mac ssh                                  # interactive shell
barn mac exec -- sw_vers                      # run one program
barn mac ssh -- 'id; sudo -n true && echo sudo works'
```

```text
ProductName:		macOS
ProductVersion:		27.2.1
BuildVersion:		26B131
```

The guest administrator account has your host username unless you set `--user` at creation. Put `--` before the remote command. `ssh` passes a command string to the guest shell; `exec` runs the program with its arguments directly, so wrap pipelines and redirection in `sh -c` (see the [SSH/exec contract](../../reference/mac/#ssh-and-exec)). Both return the remote exit status, and `--json` captures `stdout`, `stderr`, and `exit_code`:

```bash
barn --json mac exec -- sh -c 'echo out; exit 3'
```

```json
{
  "command": "exec",
  "node": "mac1",
  "host": "10.10.20.10",
  "arguments": ["sh", "-c", "echo out; exit 3"],
  "success": false,
  "exit_code": 3,
  "stdout": "out\n"
}
```

The full schema is in the [Mac reference](../../reference/mac/#json-output).

### Desktop {#desktop}

```bash
barn mac open
```

`barn mac open` shows the guest desktop in a native window; the guest display follows the window size (**View → Enter Full Screen** for full screen). Closing the window leaves the VM running. Run `barn mac open` again to reopen it, or to boot a stopped VM and show its display.

While the guest window has focus, keyboard shortcuts go to the guest, so use the menu bar for host actions:

| Menu item | Action |
|---|---|
| **Machine → Share Clipboard** | Toggle plain-text clipboard sync for this session |
| **Machine → Restart…** | Restart macOS inside the guest |
| **Machine → Shut Down…** | Shut the guest down cleanly (same as `barn mac stop`) |
| **Window → Keep Running in Background** | Hide the window and keep the VM running |
| **Barn → Quit Barn…** | Choose whether to keep the VM running in the background or shut it down |

When you log out of or shut down the host, Barn asks the guest to shut down and powers it off if it misses the timeout.

Each VM has a random password for the lock screen and GUI administrator prompts. Copy it to the host clipboard without printing it:

```bash
barn mac password --copy
```

### Clipboard {#clipboard}

Clipboard sharing moves plain text over the VM's SSH channel, so the guest needs no extra agent. Focusing the guest window brings in text copied on the host; switching back to a host app brings out text copied in the guest. Password-manager items marked concealed, images, and files are never transferred.

To turn clipboard sharing off for a VM from its next start, run `barn mac configure mac1 --clipboard off`.

### Shared folders {#shared-folders}

Pass `--share` when creating a VM to mount host directories in the guest under `/Volumes/My Shared Files/<name>`:

```bash
barn mac up dev --cpu 8 --memory 16G --share ~/src --share docs=~/Documents:ro
barn mac exec dev -- ls "/Volumes/My Shared Files"
```

The share name defaults to the directory's basename; append `:ro` for read-only. Each path must resolve to an existing directory (symlinks are fine), and Barn never creates or deletes the host directory. To change shares on an existing VM, stop it and use `configure`:

```bash
barn mac stop dev
barn mac configure dev --share data=/Volumes/Work/data --unshare docs
barn mac start dev
```

VirtioFS in macOS guests can briefly serve cached contents after a host-side edit. When a build needs the exact bytes immediately, copy files onto the guest disk with `scp` or `rsync`.

### SSH from other tools {#ssh-from-other-tools}

When the first Mac VM becomes ready, Barn adds a marked `Include` line to `~/.ssh/config`, so `ssh mac1`, `scp`, `rsync`, and IDE Remote-SSH work with each VM's own key and pinned host key:

```bash
ssh mac1 'uptime'
rsync -a ./project/ mac1:project/
barn mac ssh-config              # print the generated entries
barn mac ssh-config --remove     # remove Barn's Include and stop re-adding it
barn mac ssh-config --install    # add it back
```

If `~/.ssh/config` is a symlink managed by a dotfile tool, Barn leaves it alone and prints the `Include` line for you to add. If your own config already has a `Host` alias with a VM's name, Barn keeps yours and writes its entry by IP address only.

## Multiple machines {#multiple-machines}

VM names are lowercase letters and digits, start with a letter, and may contain inner hyphens. Creation flags (`--cpu`, `--memory`, `--disk`, `--user`, `--share`) apply when creating a VM. CPU and memory default to 4 vCPUs and 8 GiB. Without `--disk`, the VM inherits the default base's capacity, or uses 100 GiB for the first base.

```bash
barn mac up dev --cpu 8 --memory 16G
barn mac ls
```

```text
NAME  ADDRESS      STATE    SSH    USER   OS            CPU  MEMORY  DISK                 SHARED
dev   10.10.21.10  running  ready  alice  macOS 27.2.1  8    16 GiB  504.0 MiB / 100 GiB  src,data
mac1  10.10.20.10  running  ready  alice  macOS 27.2.1  4    8 GiB   4.7 GiB / 100 GiB
limit: 2 of 2 macOS VMs are running; stop one before starting another
```

Without a name, commands act on the only VM, or on `mac1`; when several exist, Barn asks you to name one. A `--disk` size different from the prepared base's capacity makes Barn install a new base at that size first.

Each VM gets its own private `/24`, starting at `10.10.20.0/24` with the guest at `.10`. Barn skips subnets that overlap your LAN, VPNs, the Linux lab, or other Mac VMs.

## Everyday lifecycle {#everyday-lifecycle}

```bash
barn mac stop dev               # clean shutdown; powers off after 2 minutes
barn mac start dev              # boot and wait for SSH
barn mac restart dev            # stop and start, applying pending configure changes
barn mac stop --all             # stop every running Mac VM
```

`barn mac stop --force` powers off at once; `barn mac start --recovery` boots macOS Recovery with the desktop window open.

`barn mac up` never changes an existing VM. Passing a different creation flag points you to `configure`:

```text
error: dev already exists, so --cpu would not apply; its configuration and data were preserved
next: barn mac configure dev --cpu 4
```

Stop the VM before changing CPUs, memory, shared folders, or subnet (clipboard sharing can change while it runs):

```bash
barn mac stop dev
barn mac configure dev --cpu 6 --memory 12G --subnet auto
barn mac start dev
```

`barn mac recreate` replaces the disk with a fresh clone of the base and keeps the name, account, hardware settings, shares, and IP. `barn mac destroy` deletes the VM. Both ask for typed confirmation on a terminal; pass `--yes` in scripts.

```bash
barn mac recreate dev
barn mac destroy dev
```

| Operation | Guest disk and installed apps | Hardware settings, IP, and account |
|---|---|---|
| `stop` / `start`, `restart`, repeated `up` | Kept | Kept |
| `configure` | Kept | Updated as requested |
| `recreate` | Replaced with a fresh macOS clone | Kept; new login password and SSH keys |
| `destroy` | Deleted | Deleted; the base image stays |
{.wide .expand}

## macOS versions and disk space {#macos-versions-and-disk-space}

```bash
barn mac image ls
```

```text
KIND           OS            BUILD   STATE  ON DISK   CAPACITY  USED BY
base           macOS 27.0    26A428  ready  26.7 GiB  100 GiB   mac1
base           macOS 27.2.1  26B131  ready  25.6 GiB  100 GiB   default
restore image  macOS 27.0    26A428  ready  24.8 GiB  -
restore image  macOS 27.2.1  26B131  ready  23.8 GiB  -
```

APFS clones share blocks with their base, so `ON DISK` and the per-VM disk figures overlap and don't add up.

`barn mac image update` prepares the newest macOS 27 build Barn supports and makes it the default for new VMs. Existing VMs keep their disks and bases. To move a VM to the new base, back up its files and run `barn mac recreate NAME --update`. For a VM with a custom disk size such as `200G`, first prepare a base of that size with `barn mac setup --disk 200G`.

`barn mac image prune` lists base images that no VM uses and aren't the default; `--installers` adds downloaded `.ipsw` files, and `--yes` deletes them:

```bash
barn mac image prune --installers         # preview
barn mac image prune --installers --yes   # delete
```

## Clean up {#clean-up}

```bash
barn mac ls                                 # list Mac VMs
barn mac destroy mac1 dev                   # delete selected VMs
barn mac image prune --installers --yes     # delete cached IPSWs and unused bases
```

Destroying the last Mac VM removes its `~/.ssh/config` entries and keeps the default base image. To remove all Mac VM state:

1. Destroy every Mac VM (`barn mac destroy ...`) and run `barn mac ssh-config --remove`.
2. Delete `$BARN_HOME/mac` (default `~/.barn/mac`).
3. Optionally delete `~/Library/Preferences/com.pgsty.barn.plist` (`io.pgsty.barn.mac-runner.plist` in `0.9.0`), which stores the desktop window geometry.
{.steps}

## Troubleshooting {#troubleshooting}

Start with `barn mac doctor`. It checks the host, `Barn.app`, base images, free space, and every VM, and exits `3` if any check fails. `barn mac logs [name]` shows startup, network, and Virtualization framework logs.

| Symptom | Fix |
|---|---|
| `dev and mac1 are running; macOS allows 2 macOS virtual machines at a time` | Run the suggested `barn mac stop NAME`, or quit another tool's macOS VM. See [Requirements](#requirements). |
| `preparing macOS needs … GiB free on this volume` | Free space, or run `barn mac image prune --installers --yes`. |
| `… has a 200 GiB disk, but macOS … is prepared only at 100 GiB` | Run the printed `barn mac setup --disk 200G`, then `barn mac recreate NAME --update`. |
| `network … overlaps route …` on start | A VPN or new route now covers the VM's subnet. Run `barn mac configure NAME --subnet auto`. |
| `ssh mac1` from another SSH client fails with `No route to host` | Allow your terminal or IDE in **System Settings → Privacy & Security → Local Network**, or use `/usr/bin/ssh` (`barn mac ssh` and `exec` use it). |
| `mac_runner_missing` or `mac_runner_protocol` | The CLI and `Barn.app` come from different builds. Reinstall Barn so they match; for a source build, use the `bin/mac/barn` from one `make mac-build`. |
| Starting a VM fails from a remote SSH session | Run `barn mac` in a terminal on the Mac's logged-in desktop session, with the login keychain unlocked. |
{.wide .expand}

Every error reason and exit code is listed in the [Mac reference](../../reference/mac/#failures).

Apple Account sign-in is unreliable in macOS guests under Apple's Virtualization framework. USB passthrough, snapshots, and suspend/resume aren't supported.
