跳转到内容
Tauri

Windows Installer

Windows 上的 Tauri 应用程序使用 WiX Toolset v3 分发为 Microsoft Installers (.msi 文件) 或使用 NSIS 分发为安装程序可执行文件 (-setup.exe 文件)。

请注意,.msi 安装程序 只能在 Windows 上创建,因为 WiX 只能在 Windows 系统上运行。 NSIS 安装程序的交叉编译如下所示。

本指南提供有关安装程序可用自定义选项的信息。

要构建并将你的应用程序打包到 Windows 安装程序中,你可以使用 Tauri CLI 并在 Windows 计算机中运行 tauri build 命令:

npm run tauri build

在使用 NSIS 时,可以在 Linux 和 macOS 主机上交叉编译 Windows 应用程序,但有一些注意事项。 它不像直接在 Windows 上编译那样简单,而且测试也不多。 因此,如果本地虚拟机或 GitHub Actions 等 CI 解决方案对你不起作用,它应该只作为最后的手段使用。

由于 Tauri 正式只支持 MSVC Windows 目标,因此设置稍微复杂一些。

一些 Linux 发行版在其存储库中有 NSIS,例如在 Ubuntu 上,你可以通过运行此命令安装 NSIS:

Ubuntu
sudo apt install nsis

但在许多其他发行版上,你必须自己编译 NSIS 或手动下载发行版二进制包中未包含的 Stubs 和 Plugins。 例如,Fedora 仅提供二进制文件,不提供 Stubs 和 Plugins:

Fedora
sudo dnf in mingw64-nsis
wget https://github.com/tauri-apps/binary-releases/releases/download/nsis-3/nsis-3.zip
unzip nsis-3.zip
sudo cp nsis-3.08/Stubs/* /usr/share/nsis/Stubs/
sudo cp -r nsis-3.08/Plugins/** /usr/share/nsis/Plugins/

由于默认的 Microsoft 链接器仅在 Windows 上工作,我们还需要安装一个新的链接器。 为了编译用于设置应用程序图标等的 Windows 资源文件,我们还需要 LLVM 项目的一部分 llvm-rc 二进制文件。

Ubuntu
sudo apt install lld llvm

在 Linux 上,如果你的构建脚本中添加了编译 C/C++ 依赖项的依赖项,你还需要安装 clang 包。 默认的 Tauri 应用程序应该不需要这个。

假设你要为 64 位 Windows 系统构建:

Terminal window
rustup target add x86_64-pc-windows-msvc

我们将使用 [cargo-xwin] 作为 Tauri 的 “runner”,而不是手动设置 Windows SDK:

Terminal window
cargo install --locked cargo-xwin

默认情况下,cargo-xwin 将 Windows SDK 下载到项目本地文件夹中。 如果你有多个项目并希望共享这些文件,可以将 XWIN_CACHE_DIR 环境变量设置为首选位置的路径。

现在应该就像将 runner 和 target 添加到 tauri build 命令一样简单:

npm run tauri build -- --runner cargo-xwin --target x86_64-pc-windows-msvc

构建输出将在 target/x86_64-pc-windows-msvc/release/bundle/nsis/ 中。

Tauri CLI 默认使用你机器的架构编译你的可执行文件。 假设你在 64 位机器上开发,CLI 将生成 64 位应用程序。

如果你需要支持 32 位 机器,可以使用 --target 标志使用 不同Rust 目标 编译你的应用程序:

npm run tauri build -- --target i686-pc-windows-msvc

默认情况下,Rust 仅安装你机器目标的工具链, 所以你需要先安装 32 位 Windows 工具链:rustup target add i686-pc-windows-msvc

如果你需要为 ARM64 构建,首先需要安装额外的构建工具。 为此,打开 Visual Studio Installer,单击 “Modify”,并在 “Individual Components” 选项卡中安装 “C++ ARM64 build tools”。 在撰写本文时,VS2022 中的确切名称是 MSVC v143 - VS 2022 C++ ARM64 build tools (Latest)。 现在你可以使用 rustup target add aarch64-pc-windows-msvc 添加 rust 目标,然后使用上述方法编译你的应用程序:

npm run tauri build -- --target aarch64-pc-windows-msvc

请注意,NSIS 安装程序本身仍将是在 ARM 机器上通过仿真运行的 x86 程序。应用程序本身将是原生的 ARM64 二进制文件。

默认情况下,Microsoft Installer (.msi) 在 Windows 7 上不起作用,因为它需要下载 WebView2 引导程序(如果未安装) (如果操作系统中未启用 TLS 1.2,这可能会失败)。Tauri 包含一个嵌入 WebView2 引导程序的选项 (请参阅下面的 嵌入 WebView2 引导程序 部分)。 基于 NSIS 的安装程序 (-setup.exe) 也支持 Windows 7 上的 downloadBootstrapper 模式。

此外,要在 Windows 7 中使用通知 API,你需要启用 windows7-compat Cargo 功能:

Cargo.toml
[dependencies]
tauri-plugin-notification = { version = "2.0.0", features = [ "windows7-compat" ] }

如果你的系统要求 MSI 包符合 FIPS,你可以在运行 tauri build 之前将 TAURI_BUNDLER_WIX_FIPS_COMPLIANT 环境变量设置为 true。 在 PowerShell 中,你可以像这样为当前终端会话设置它:

Terminal window
$env:TAURI_BUNDLER_WIX_FIPS_COMPLIANT="true"

默认情况下,安装程序会下载 WebView2 引导程序,并在未安装运行时的情况下执行它。 或者,你可以嵌入引导程序、嵌入离线安装程序或使用固定的 WebView2 运行时版本。 有关这些方法之间的比较,请参见下表:

安装方法需要互联网连接?额外安装程序大小备注
downloadBootstrapper0MB默认
导致更小的安装程序大小,但不建议通过 .msi 文件部署 Windows 7。
embedBootstrapper~1.8MB更好地支持 Windows 7 的 .msi 安装程序。
offlineInstaller~127MB嵌入 WebView2 安装程序。建议用于离线环境。
fixedVersion~180MB嵌入固定的 WebView2 版本。
skip0MB⚠️ 不推荐
不会作为 Windows 安装程序的一部分安装 WebView2。

在 Windows 10(2018 年 4 月版本或更高版本)和 Windows 11 上,WebView2 运行时作为操作系统的一部分分发。

这是构建 Windows 安装程序的默认设置。它下载引导程序并运行它。 需要互联网连接,但会导致更小的安装程序大小。 如果你打算通过 .msi 安装程序分发到 Windows 7,则不建议这样做。

tauri.conf.json
{
"bundle": {
"windows": {
"webviewInstallMode": {
"type": "downloadBootstrapper"
}
}
}
}

要嵌入 WebView2 引导程序,请将 webviewInstallMode 设置为 embedBootstrapper。 这将使安装程序大小增加约 1.8MB,但会增加与 Windows 7 系统的兼容性。

tauri.conf.json
{
"bundle": {
"windows": {
"webviewInstallMode": {
"type": "embedBootstrapper"
}
}
}
}

要嵌入 WebView2 引导程序,请将 webviewInstallMode 设置为 offlineInstaller。 这将使安装程序大小增加约 127MB,但允许即使在没有互联网连接的情况下也能安装你的应用程序。

tauri.conf.json
{
"bundle": {
"windows": {
"webviewInstallMode": {
"type": "offlineInstaller"
}
}
}
}

使用系统提供的运行时对于安全性非常有利,因为 Webview 漏洞补丁由 Windows 管理。 如果你想控制每个应用程序的 WebView2 分发 (自己管理发布补丁或在可能没有互联网连接的环境中分发应用程序) Tauri 可以为你打包运行时文件。

  1. Microsoft 网站 下载 WebView2 固定版本运行时。 在此示例中,下载的文件名为 Microsoft.WebView2.FixedVersionRuntime.128.0.2739.42.x64.cab
  2. 将文件解压缩到 core 文件夹:
Terminal window
Expand .\Microsoft.WebView2.FixedVersionRuntime.128.0.2739.42.x64.cab -F:* ./src-tauri
  1. tauri.conf.json 中配置 WebView2 运行时路径:
tauri.conf.json
{
"bundle": {
"windows": {
"webviewInstallMode": {
"type": "fixedRuntime",
"path": "./Microsoft.WebView2.FixedVersionRuntime.98.0.1108.50.x64/"
}
}
}
}
  1. 运行 tauri build 以生成带有固定 WebView2 运行时的 Windows 安装程序。

你可以通过将 webviewInstallMode 设置为 skip 来从安装程序中删除 WebView2 运行时下载检查。 如果用户未安装运行时,你的应用程序将无法运行。

如果用户未安装运行时且不尝试安装它,你的应用程序将无法运行。

tauri.conf.json
{
"bundle": {
"windows": {
"webviewInstallMode": {
"type": "skip"
}
}
}
}

有关自定义选项的完整列表,请参阅 WiX 配置

.msi Windows 安装程序包是使用 WiX Toolset v3 构建的。 目前,除了预定义的 配置 外,你还可以通过使用自定义 WiX 源代码 (具有 .wxs 文件扩展名的 XML 文件)或通过 WiX 片段来更改它。

Tauri 定义的 Windows 安装程序 XML 配置为适用于基于简单 webview 的应用程序的常见用例 (你可以 在这里 找到它)。 它使用 handlebars,因此 Tauri CLI 可以根据你的 tauri.conf.json 定义为你的安装程序打上品牌。 如果你需要一个完全不同的安装程序,可以在 tauri.bundle.windows.wix.template 上配置自定义模板文件。

WiX 片段 是一个容器,你可以在其中配置 WiX 提供的几乎所有内容。 在此示例中,我们将定义一个写入两个注册表项的片段:

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
<!-- these registry entries should be installed
to the target user's machine -->
<DirectoryRef Id="TARGETDIR">
<!-- groups together the registry entries to be installed -->
<!-- Note the unique `Id` we provide here -->
<Component Id="MyFragmentRegistryEntries" Guid="*">
<!-- the registry key will be under
HKEY_CURRENT_USER\Software\MyCompany\MyApplicationName -->
<!-- Tauri uses the second portion of the
bundle identifier as the `MyCompany` name
(e.g. `tauri-apps` in `com.tauri-apps.test`) -->
<RegistryKey
Root="HKCU"
Key="Software\MyCompany\MyApplicationName"
Action="createAndRemoveOnUninstall"
>
<!-- values to persist on the registry -->
<RegistryValue
Type="integer"
Name="SomeIntegerValue"
Value="1"
KeyPath="yes"
/>
<RegistryValue Type="string" Value="Default Value" />
</RegistryKey>
</Component>
</DirectoryRef>
</Fragment>
</Wix>

将片段文件以 .wxs 扩展名保存在 src-tauri/windows/fragments 文件夹中,并在 tauri.conf.json 上引用它:

tauri.conf.json
{
"bundle": {
"windows": {
"wix": {
"fragmentPaths": ["./windows/fragments/registry.wxs"],
"componentRefs": ["MyFragmentRegistryEntries"]
}
}
}
}

请注意,ComponentGroupComponentFeatureGroupFeatureMerge 元素 ID 必须在 tauri.conf.jsonwix 对象上的 componentGroupRefscomponentRefsfeatureGroupRefsfeatureRefsmergeRefs 中引用 才能包含在安装程序中。

WiX 安装程序默认使用 en-US 语言构建。 可以使用 tauri.bundle.windows.wix.language 属性配置国际化 (i18n), 定义 Tauri 应针对其构建安装程序的语言。 你可以在 Microsoft 网站 上的 Language-Culture 列中找到要使用的语言名称。

要创建针对特定语言的单一安装程序,请将 language 值设置为字符串:

tauri.conf.json
{
"bundle": {
"windows": {
"wix": {
"language": "fr-FR"
}
}
}
}

要编译针对语言列表的安装程序,请使用数组。 将为每种语言创建一个特定的安装程序,语言键作为后缀:

tauri.conf.json
{
"bundle": {
"windows": {
"wix": {
"language": ["en-US", "pt-BR", "fr-FR"]
}
}
}
}

可以为每种语言定义一个配置对象来配置本地化字符串:

tauri.conf.json
{
"bundle": {
"windows": {
"wix": {
"language": {
"en-US": null,
"pt-BR": {
"localePath": "./wix/locales/pt-BR.wxl"
}
}
}
}
}
}

localePath 属性定义语言文件的路径,这是一个配置语言文化的 XML:

<WixLocalization
Culture="en-US"
xmlns="http://schemas.microsoft.com/wix/2006/localization"
>
<String Id="LaunchApp"> Launch MyApplicationName </String>
<String Id="DowngradeErrorMessage">
A newer version of MyApplicationName is already installed.
</String>
<String Id="PathEnvVarFeature">
Add the install location of the MyApplicationName executable to
the PATH system environment variable. This allows the
MyApplicationName executable to be called from any location.
</String>
<String Id="InstallAppFeature">
Installs MyApplicationName.
</String>
</WixLocalization>

目前,Tauri 引用以下区域设置字符串:LaunchAppDowngradeErrorMessagePathEnvVarFeatureInstallAppFeature。 你可以定义自己的字符串并在自定义模板或片段中使用 "!(loc.TheStringId)" 引用它们。 有关更多信息,请参阅 WiX 本地化文档

有关自定义选项的完整列表,请参阅 NSIS 配置

Tauri 定义的 NSIS 安装程序的 .nsi 脚本配置为适用于基于简单 webview 的应用程序的常见用例 (你可以 在这里 找到它)。 它使用 handlebars,因此 Tauri CLI 可以根据你的 tauri.conf.json 定义为你的安装程序打上品牌。 如果你需要一个完全不同的安装程序,可以在 tauri.bundle.windows.nsis.template 上配置自定义模板文件。

如果你只需要扩展某些安装步骤,你可以使用安装程序钩子,而不是替换整个安装程序模板。

支持的钩子有:

  • NSIS_HOOK_PREINSTALL: 在复制文件、设置注册表项值和创建快捷方式之前运行。
  • NSIS_HOOK_POSTINSTALL: 在安装程序完成复制所有文件、设置注册表项和创建快捷方式之后运行。
  • NSIS_HOOK_PREUNINSTALL: 在删除任何文件、注册表项和快捷方式之前运行。
  • NSIS_HOOK_POSTUNINSTALL: 在删除文件、注册表项和快捷方式之后运行。

例如,在 src-tauri/windows 文件夹中创建一个 hooks.nsh 文件并定义你需要的钩子:

!macro NSIS_HOOK_PREINSTALL
MessageBox MB_OK "PreInstall"
!macroend
!macro NSIS_HOOK_POSTINSTALL
MessageBox MB_OK "PostInstall"
!macroend
!macro NSIS_HOOK_PREUNINSTALL
MessageBox MB_OK "PreUnInstall"
!macroend
!macro NSIS_HOOK_POSTUNINSTALL
MessageBox MB_OK "PostUninstall"
!macroend

然后你必须配置 Tauri 使用该钩子文件:

tauri.conf.json
{
"bundle": {
"windows": {
"nsis": {
"installerHooks": "./windows/hooks.nsh"
}
}
}
}

你可以使用安装程序钩子自动安装你的应用程序所需的系统依赖项。这对运行时依赖项特别有用,如 Visual C++ Redistributables、DirectX、OpenSSL 或其他可能并非在所有 Windows 系统上都存在的系统库。

MSI 安装程序示例 (Visual C++ Redistributable):

!macro NSIS_HOOK_POSTINSTALL
; Check if Visual C++ 2019 Redistributable is installed (via Windows Registry)
ReadRegDWord $0 HKLM "SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\x64" "Installed"
${If} $0 == 1
DetailPrint "Visual C++ Redistributable already installed"
Goto vcredist_done
${EndIf}
; Install from bundled MSI if not installed
${If} ${FileExists} "$INSTDIR\resources\vc_redist.x64.msi"
DetailPrint "Installing Visual C++ Redistributable..."
; Copy to TEMP folder and then execute installer
CopyFiles "$INSTDIR\resources\vc_redist.x64.msi" "$TEMP\vc_redist.x64.msi"
ExecWait 'msiexec /i "$TEMP\vc_redist.x64.msi" /passive /norestart' $0
; Check wether installation process exited successfully (code 0) or not
${If} $0 == 0
DetailPrint "Visual C++ Redistributable installed successfully"
${Else}
MessageBox MB_ICONEXCLAMATION "Visual C++ installation failed. Some features may not work."
${EndIf}
; Clean up setup files from TEMP and your installed app
Delete "$TEMP\vc_redist.x64.msi"
Delete "$INSTDIR\resources\vc_redist.x64.msi"
${EndIf}
vcredist_done:
!macroend

主要注意事项:

  • 一个好的做法是始终使用注册表项或文件存在或通过 Windows where 命令检查依赖项是否已安装。
  • 使用 /passive/quiet/silent 标志来避免中断安装流程。查看 .msi 文件的 msiexec 选项,或特定于应用程序标志的安装手册
  • 包含 /norestart 以防止在重新启动用户设备的设置期间自动重新启动系统
  • 清理临时文件和捆绑的安装程序以避免使应用程序膨胀
  • 考虑到在卸载时可能会与其他应用程序共享依赖项
  • 如果安装失败,提供有意义的错误消息

确保将依赖项安装程序捆绑在 src-tauri/resources 文件夹中并添加到 tauri.conf.json 中,以便它们被捆绑,并且可以在安装期间从 $INSTDIR\resources\ 访问:

tauri.conf.json
{
"bundle": {
"resources": [
"resources/my-dependency.exe",
"resources/another-one.msi"
]
}
}

默认情况下,安装程序仅为当前用户安装你的应用程序。 此选项的优点是安装程序不需要管理员权限即可运行, 但应用程序安装在 %LOCALAPPDATA% 文件夹中,而不是 C:/Program Files

如果你希望你的应用程序安装在系统范围内可用(这需要管理员权限) 你可以将 installMode 设置为 perMachine

tauri.conf.json
{
"bundle": {
"windows": {
"nsis": {
"installMode": "perMachine"
}
}
}
}

或者,你可以通过将 installMode 设置为 both 来让用户选择是仅为当前用户安装还是在系统范围内安装应用程序。 请注意,安装程序将需要管理员权限才能执行。

有关更多信息,请参阅 NSISInstallerMode

NSIS 安装程序是一个多语言安装程序,这意味着你始终拥有一个包含所有选定翻译的单个安装程序。

你可以使用 tauri.bundle.windows.nsis.languages 属性指定要包含的语言。 NSIS GitHub 项目 中提供了 NSIS 支持的语言列表。 需要一些 Tauri 特定的翻译,所以如果你看到未翻译的文本,请随时在 Tauri 的主仓库 中打开功能请求。 你也可以提供 自定义翻译文件

默认情况下,使用操作系统默认语言来确定安装程序语言。 你还可以配置安装程序在渲染安装程序内容之前显示语言选择器:

tauri.conf.json
{
"bundle": {
"windows": {
"nsis": {
"displayLanguageSelector": true
}
}
}
}

如果你的应用程序需要仅在较新 Webview2 版本中可用的功能(例如自定义 URI 方案),你可以指示 Windows 安装程序 验证当前 Webview2 版本,如果不符合目标版本,则运行 Webview2 引导程序。

tauri.conf.json
{
"bundle": {
"windows": {
"nsis": {
"minimumWebview2Version": "110.0.1531.0"
}
}
}
}

© 2025 Tauri Contributors. CC-BY / MIT