> ## Documentation Index
> Fetch the complete documentation index at: https://herd.laravel.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Manage Rust

# Managing Rust

Herd serves Laravel Rust applications next to your PHP sites. Herd builds and runs them with your Rust toolchain. On Windows, Rust also needs Microsoft's C++ build tools to link applications. Herd installs Rust for you and keeps it up to date. You install the build tools yourself from Microsoft, as described [below](#installing-the-build-tools).

Rust is optional. If you only work on PHP applications, you don't need either of them.

## Installing Rust

Open the settings, go to "Runtimes" and select "Rust". Click "Install" and Herd downloads `rustup-init.exe`, the official Rust installer, checks it against the checksum that rust-lang.org publishes and installs the latest stable version of Rust. The installation takes a few minutes and needs about 540 MB of disk space. It doesn't need administrator rights, and you can keep using Herd in the meantime.

Herd also offers to install Rust during the onboarding and in the What's New window of Herd 2.4, so you may already have it.

Herd installs Rust into `%USERPROFILE%\.cargo` and `%USERPROFILE%\.rustup`, where `rustup` always installs it. Besides the compiler and Cargo, Herd installs `rustfmt`, `clippy` and `rust-analyzer`, which editors and coding agents use. `rustup` adds `%USERPROFILE%\.cargo\bin` to your `PATH`, so `cargo` is available in your terminal. Your editor, your coding agents and Herd all use the same toolchain, so none of them has to rebuild what another one already built. Terminals that were already open before the installation don't see the new `PATH` yet. Open a new one.

## Installing the build tools

Rust's default toolchain on Windows, `x86_64-pc-windows-msvc` or `aarch64-pc-windows-msvc` on ARM64, uses Microsoft's linker `link.exe` and the libraries of the Windows SDK to build applications. Without them, every build fails. Rust needs:

* The **MSVC C++ build tools** for your machine's architecture, from Visual Studio Build Tools or any edition of Visual Studio. They are part of the "Desktop development with C++" workload.
* A **Windows 10 or Windows 11 SDK**.

Herd doesn't install these tools for you, because Microsoft's license terms for Visual Studio and its Build Tools don't cover every company. Check the [license terms](https://visualstudio.microsoft.com/license-terms/) or ask your organization which edition you may use.

As long as something is missing, the Rust settings show a "Build tools" row that tells you what Herd couldn't find, and the "Learn more" button leads to this page. Rust is greyed out with "Needs build tools" in the new application window, and the Rust card of the onboarding shows a "Get Build Tools" button. After you installed the tools, click "Check Again" on the Rust settings page. Herd also checks again whenever you switch back to the settings window.

### With the Visual Studio Installer

1. Download "Build Tools for Visual Studio" from the [Visual Studio downloads page](https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio). If you already have Visual Studio or the Build Tools, open the "Visual Studio Installer" from the Start menu and click "Modify" instead.
2. Select the "Desktop development with C++" workload. It includes the latest MSVC build tools for x64/x86 and a Windows 11 SDK by default.
3. On a Windows on ARM machine, also select these components in the details on the right, or in the "Individual components" tab:
   * "MSVC Build Tools for ARM64/ARM64EC (Latest)"
   * "C++ Clang tools for Windows"
4. Click "Install" and wait until the installer finishes. You don't have to restart Herd.

### With winget

You can also install the Build Tools from your terminal with [winget](https://learn.microsoft.com/en-us/windows/package-manager/winget/). The Visual Studio Installer asks for administrator rights while it runs.

```powershell theme={null}
# x64
winget install --id Microsoft.VisualStudio.BuildTools --source winget --override "--passive --wait --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended"

# ARM64
winget install --id Microsoft.VisualStudio.BuildTools --source winget --override "--passive --wait --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended --add Microsoft.VisualStudio.Component.VC.Tools.ARM64 --add Microsoft.VisualStudio.Component.VC.Llvm.Clang"
```

These commands install the Build Tools edition. If you already have Visual Studio or the Build Tools, winget doesn't change the components of the existing installation. Add the missing components with the Visual Studio Installer instead.

Herd finds the tools in every Visual Studio product that has them, whether that's the Build Tools, Community, Professional or Enterprise. In the Visual Studio Installer's terms, Herd looks for these components:

| Machine | Required components |
| - | - |
| x64 | `Microsoft.VisualStudio.Component.VC.Tools.x86.x64` and a Windows 10 or 11 SDK |
| ARM64 | `Microsoft.VisualStudio.Component.VC.Tools.ARM64`, `Microsoft.VisualStudio.Component.VC.Llvm.Clang` and a Windows 10 or 11 SDK |

### Windows on ARM

Laravel Rust applications need two more things on ARM64 machines:

* **Clang.** The `ring` crate, which handles TLS and cryptography, compiles its C code with Clang on ARM64. Visual Studio ships Clang as the "C++ Clang tools for Windows" component. Herd adds it to the `PATH` of the applications it builds, so you don't have to.
* **MSVC 14.41 or newer.** The memory allocator `mimalloc` needs the `__popcnt64` function, which Microsoft's ARM64 headers only contain from MSVC 14.41, which shipped with Visual Studio 2022 17.11, onwards. What counts is Visual Studio's default toolset, which is the latest one installed. Make sure that the "(Latest)" ARM64 component is installed, not only an older version that you selected explicitly. If your toolset is too old, Herd shows the version that it found on the Rust settings page. Update Visual Studio in the Visual Studio Installer to get a newer one.

Rust itself should be the ARM64 toolchain, `aarch64-pc-windows-msvc`, which Herd installs on ARM64 machines. An x86\_64 toolchain runs emulated on ARM64, and it isn't the toolchain that Herd's update offers. If you installed Rust yourself before, make the ARM64 toolchain the default:

```powershell theme={null}
rustup default stable-aarch64-pc-windows-msvc
```

### The GNU toolchain

`rustup` can also install a GNU toolchain, `x86_64-pc-windows-gnu`, which builds with MinGW instead of Microsoft's tools. Without MinGW's `dlltool.exe` on your `PATH`, every Laravel Rust build fails with:

```
error calling dlltool 'dlltool.exe': program not found
```

If your default toolchain is the GNU one and `dlltool.exe` is missing, Herd shows a "Toolchain host" row on the Rust settings page, and Rust is greyed out with "Needs MSVC" in the new application window. Click "Switch to MSVC" and Herd makes the MSVC toolchain for your machine the default of `rustup`, which applies to your terminal and your editor as well. You can do the same in your terminal:

```powershell theme={null}
# x64
rustup default stable-x86_64-pc-windows-msvc

# ARM64
rustup default stable-aarch64-pc-windows-msvc
```

The MSVC toolchain needs the [C++ build tools](#installing-the-build-tools) as well. If you use the GNU toolchain on purpose with MSYS2 and have `dlltool.exe` on your `PATH`, Herd builds with it and doesn't ask you to switch.

## Using an existing Rust installation

If Rust is already installed on your machine, Herd uses it and doesn't install a second copy. Herd uses the first `cargo.exe` that it finds in the following places, in this order:

* The directories in your `PATH`, like your terminal does
* `%CARGO_HOME%\bin`, or `%USERPROFILE%\.cargo\bin` if `CARGO_HOME` isn't set

Herd can update a toolchain that `rustup` manages. If you installed Rust another way, for example with Rust's standalone MSI installer or with Scoop, Herd uses it as it is, and you update it with the tool that installed it.

## Updating Rust

When you open the Rust settings, Herd checks for a new stable release of Rust and shows an "Update to" button with the new version if there is one. Updating Rust rebuilds every Laravel Rust application from scratch on its next start, so the first visit after an update takes a bit longer.

You can also update Rust from your terminal:

```powershell theme={null}
rustup update
```

## Laravel Rust applications

Herd serves a site as a Laravel Rust application when its `Cargo.toml` names the `laravel-rust` crate as a dependency. Every Laravel Rust application in a parked path is available via its `.test` domain. Herd ships with the Laravel installer, which creates them with the `--rust` flag:

```powershell theme={null}
Set-Location -Path "$env:USERPROFILE\Herd"
laravel new my-rust-app --rust
cd my-rust-app
herd open
```

You can also create Laravel Rust applications in the new application window. Once Rust and the build tools are installed, the window lets you choose between PHP and Rust.

When you open the site in your browser, Herd starts the application on a free port and passes all requests for the `.test` domain to it. If the application has a `dev` script in its `package.json` and its `node_modules` are installed, Herd runs `laravel dev`, which starts the server, a queue worker and Vite and rebuilds the application when you change it. Otherwise, Herd runs `cargo run`.

The first start compiles the application and all of its dependencies, which can take a few minutes. After that, the application starts in a few seconds. Herd stops the application when it didn't receive a request for five minutes, and starts it again with the next request.

The [Applications window](https://herd.laravel.com/docs/windows/sites/managing-sites.md) shows a "Dev server" row for Laravel Rust applications, where you can start, stop and restart the application and look at its output. In your terminal, run these commands in the application's directory:

```powershell theme={null}
# Show the application, its command and its state
herd processes

# Show the output of the application and its build
herd processes:output app

# Stop and start the application
herd processes:stop app
herd processes:start app
```

Laravel Rust applications don't use `php artisan` or Composer. Run your console commands with the `laravel` CLI from the application's directory, for example `laravel migrate`.

## Uninstalling Rust

Herd doesn't uninstall Rust, because other tools on your machine may use it as well. If you want to remove Rust, run the uninstaller of `rustup`:

```powershell theme={null}
rustup self uninstall
```

You can remove the C++ build tools in the Visual Studio Installer or in the "Installed apps" of the Windows settings.
