Windows 安装程序
适用于 Windows 的 Tauri 应用程序可以使用 WiX Toolset v3 以 Microsoft 安装程序(.msi
文件)的形式分发,也可以使用 NSIS 以安装可执行文件(-setup.exe
文件)的形式分发。
请注意,.msi
安装程序**只能在 Windows 上创建**,因为交叉编译不起作用。NSIS 安装程序的交叉编译目前是实验性的。
本指南提供有关安装程序可用自定义选项的信息。
构建
要在 Windows 计算机上构建应用并将其捆绑到 Windows 安装程序中,您可以使用 Tauri CLI 并运行 tauri build
命令
npm run tauri build
yarn tauri build
pnpm tauri build
deno task tauri build
bun tauri build
cargo tauri build
实验性功能:在 Linux 和 macOS 上构建 Windows 应用
当使用 NSIS 时,在 Linux 和 macOS 主机上交叉编译 Windows 应用程序是可能的。请注意,这目前被认为是高度实验性的,可能并非在每个系统上或每个项目上都有效。因此,如果本地 VM 或 GitHub Actions 等 CI 解决方案对您不起作用,则应仅将其用作最后的手段。
由于 Tauri 官方仅支持 MSVC Windows 目标,因此设置会稍微复杂一些。
安装 NSIS
某些 Linux 发行版的存储库中提供了 NSIS,例如在 Ubuntu 上,您可以通过运行以下命令来安装 NSIS
sudo apt install nsis
但在许多其他发行版上,您必须自己编译 NSIS 或手动下载发行版的二进制包中未包含的 Stubs 和 Plugins。例如,Fedora 仅提供二进制文件,但不提供 Stubs 和 Plugins
sudo dnf in mingw64-nsiswget https://github.com/tauri-apps/binary-releases/releases/download/nsis-3/nsis-3.zipunzip nsis-3.zipsudo cp nsis-3.08/Stubs/* /usr/share/nsis/Stubs/sudo cp -r nsis-3.08/Plugins/** /usr/share/nsis/Plugins/
在 macOS 上,您将需要 [Homebrew] 来安装 NSIS
brew install nsis
安装 LLVM 和 LLD 链接器
由于默认的 Microsoft 链接器仅在 Windows 上有效,因此我们还需要安装新的链接器。为了编译用于设置应用程序图标等的 Windows 资源文件,我们还需要 llvm-rc
二进制文件,它是 LLVM 项目的一部分。
sudo apt install lld llvm
如果您添加了在构建脚本中编译 C/C++ 依赖项的依赖项,则在 Linux 上还需要安装 clang
包。默认的 Tauri 应用程序不应需要此项。
brew install llvm
在 macOS 上,您还必须将 /opt/homebrew/opt/llvm/bin
添加到您的 $PATH
中,如安装输出中建议的那样。
安装 Windows Rust 目标
假设您正在为 64 位 Windows 系统构建
rustup target add x86_64-pc-windows-msvc
安装 cargo-xwin
我们将使用 [cargo-xwin
] 作为 Tauri 的“运行器”,而不是手动设置 Windows SDK。
cargo install --locked cargo-xwin
默认情况下,cargo-xwin
会将 Windows SDK 下载到项目本地文件夹中。如果您有多个项目并希望共享这些文件,则可以使用首选位置的路径设置 XWIN_CACHE_DIR
环境变量。
构建应用程序
现在,只需将运行器和目标添加到 tauri build
命令即可,这应该很简单
npm run tauri build -- --runner cargo-xwin --target x86_64-pc-windows-msvc
yarn tauri build --runner cargo-xwin --target x86_64-pc-windows-msvc
pnpm tauri build --runner cargo-xwin --target x86_64-pc-windows-msvc
deno task tauri build --runner cargo-xwin --target x86_64-pc-windows-msvc
bun tauri build --runner cargo-xwin --target x86_64-pc-windows-msvc
cargo tauri build --runner cargo-xwin --target x86_64-pc-windows-msvc
然后,构建输出将位于 target/x86_64-pc-windows-msvc/release/bundle/nsis/
中。
为 32 位或 ARM 构建
Tauri CLI 默认使用您机器的架构编译您的可执行文件。假设您正在 64 位机器上开发,CLI 将生成 64 位应用程序。
如果您需要支持 32 位 机器,您可以使用 --target
标志,使用**不同的** Rust 目标编译您的应用程序
npm run tauri build -- --target i686-pc-windows-msvc
yarn tauri build --target i686-pc-windows-msvc
pnpm tauri build --target i686-pc-windows-msvc
deno task tauri build --target i686-pc-windows-msvc
bun tauri build --target i686-pc-windows-msvc
cargo tauri build --target i686-pc-windows-msvc
默认情况下,Rust 仅为您的机器目标安装工具链,因此您需要首先安装 32 位 Windows 工具链:rustup target add i686-pc-windows-msvc
。
如果您需要为 ARM64 构建,则首先需要安装其他构建工具。为此,请打开 Visual Studio Installer
,单击“修改”,然后在“单个组件”选项卡中安装“C++ ARM64 构建工具”。在撰写本文时,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
yarn tauri build --target aarch64-pc-windows-msvc
pnpm tauri build --target aarch64-pc-windows-msvc
deno task tauri build --target aarch64-pc-windows-msvc
bun tauri build --target aarch64-pc-windows-msvc
cargo tauri build --target aarch64-pc-windows-msvc
请注意,NSIS 安装程序本身仍然是 x86,通过模拟在 ARM 机器上运行。应用程序本身将是本机 ARM64 二进制文件。
支持 Windows 7
默认情况下,Microsoft 安装程序(.msi
)在 Windows 7 上不起作用,因为它需要下载 WebView2 引导程序(如果未安装)(如果操作系统中未启用 TLS 1.2,则可能会失败)。Tauri 包含嵌入 WebView2 引导程序的选项(请参阅下面的嵌入 WebView2 引导程序部分)。基于 NSIS 的安装程序(-setup.exe
)在 Windows 7 上也支持 downloadBootstrapper
模式。
此外,要在 Windows 7 中使用 Notification API,您需要启用 windows7-compat
Cargo 功能
[dependencies]tauri-plugin-notification = { version = "2.0.0", features = [ "windows7-compat" ] }
FIPS 合规性
如果您的系统要求 MSI 捆绑包符合 FIPS,您可以在运行 tauri build
之前将 TAURI_FIPS_COMPLIANT
环境变量设置为 true
。在 PowerShell 中,您可以像这样为当前终端会话设置它
$env:TAURI_FIPS_COMPLIANT="true"
WebView2 安装选项
默认情况下,安装程序会下载 WebView2 引导程序并在运行时未安装时执行它。或者,您可以嵌入引导程序、嵌入离线安装程序或使用固定 WebView2 运行时版本。请参阅下表,了解这些方法之间的比较
安装方法 | 是否需要 Internet 连接? | 额外的安装程序大小 | 注释 |
---|---|---|---|
downloadBootstrapper | 是 | 0MB | 默认 导致安装程序尺寸更小,但不建议通过 .msi 文件部署到 Windows 7。 |
embedBootstrapper | 是 | ~1.8MB | 更好地支持 Windows 7 上的 .msi 安装程序。 |
offlineInstaller | 否 | ~127MB | 嵌入 WebView2 安装程序。推荐用于离线环境。 |
fixedVersion | 否 | ~180MB | 嵌入固定 WebView2 版本。 |
skip | 否 | 0MB | ⚠️ 不推荐 不将 WebView2 作为 Windows 安装程序的一部分安装。 |
在 Windows 10(2018 年 4 月版或更高版本)和 Windows 11 上,WebView2 运行时作为操作系统的一部分分发。
下载引导程序
这是构建 Windows 安装程序的默认设置。它下载引导程序并运行它。需要 Internet 连接,但会导致安装程序尺寸更小。如果您要通过 .msi
安装程序分发到 Windows 7,则不建议这样做。
{ "bundle": { "windows": { "webviewInstallMode": { "type": "downloadBootstrapper" } } }}
嵌入式引导程序
要嵌入 WebView2 引导程序,请将 webviewInstallMode 设置为 embedBootstrapper
。这会将安装程序大小增加约 1.8MB,但会提高与 Windows 7 系统的兼容性。
{ "bundle": { "windows": { "webviewInstallMode": { "type": "embedBootstrapper" } } }}
离线安装程序
要嵌入 WebView2 引导程序,请将 webviewInstallMode 设置为 offlineInstaller
。这会将安装程序大小增加约 127MB,但允许您的应用程序即使在没有 Internet 连接的情况下也能安装。
{ "bundle": { "windows": { "webviewInstallMode": { "type": "offlineInstaller" } } }}
固定版本
使用系统提供的运行时对于安全性非常有利,因为 webview 漏洞补丁由 Windows 管理。如果您想控制每个应用程序上的 WebView2 分发(无论是自己管理发布补丁还是在可能没有 Internet 连接的环境中分发应用程序),Tauri 都可以为您捆绑运行时文件。
- 从 Microsoft 网站下载 WebView2 固定版本运行时。在本示例中,下载的文件名是
Microsoft.WebView2.FixedVersionRuntime.128.0.2739.42.x64.cab
- 将文件解压到核心文件夹
Expand .\Microsoft.WebView2.FixedVersionRuntime.128.0.2739.42.x64.cab -F:* ./src-tauri
- 在
tauri.conf.json
中配置 WebView2 运行时路径
{ "bundle": { "windows": { "webviewInstallMode": { "type": "fixedRuntime", "path": "./Microsoft.WebView2.FixedVersionRuntime.98.0.1108.50.x64/" } } }}
- 运行
tauri build
以生成带有固定 WebView2 运行时的 Windows 安装程序。
跳过安装
您可以通过将 webviewInstallMode 设置为 skip
,从安装程序中删除 WebView2 运行时下载检查。如果用户未安装运行时,您的应用程序将无法工作。
如果用户未安装运行时,您的应用程序将无法工作,也不会尝试安装它。
{ "bundle": { "windows": { "webviewInstallMode": { "type": "skip" } } }}
自定义 WiX 安装程序
有关完整的自定义选项列表,请参阅WiX 配置。
安装程序模板
.msi
Windows 安装程序包是使用 WiX Toolset v3 构建的。目前,除了预定义的配置之外,您还可以通过使用自定义 WiX 源代码(扩展名为 .wxs
的 XML 文件)或通过 WiX 片段来更改它。
使用自定义 WiX 文件替换安装程序代码
Tauri 定义的 Windows 安装程序 XML 旨在用于基于简单 webview 的应用程序的常见用例(您可以在此处找到它)。它使用 handlebars,因此 Tauri CLI 可以根据您的 tauri.conf.json
定义来品牌化您的安装程序。如果您需要完全不同的安装程序,可以在 tauri.bundle.windows.wix.template
上配置自定义模板文件。
使用 WiX 片段扩展安装程序
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
上引用它
{ "bundle": { "windows": { "wix": { "fragmentPaths": ["./windows/fragments/registry.wxs"], "componentRefs": ["MyFragmentRegistryEntries"] } } }}
请注意,ComponentGroup
、Component
、FeatureGroup
、Feature
和 Merge
元素 ID 必须在 tauri.conf.json
的 wix
对象上分别引用 componentGroupRefs
、componentRefs
、featureGroupRefs
、featureRefs
和 mergeRefs
,才能包含在安装程序中。
国际化
WiX 安装程序默认使用 en-US
语言构建。可以使用 tauri.bundle.windows.wix.language
属性配置国际化 (i18n),定义 Tauri 应针对其构建安装程序的语言。您可以在 Microsoft 网站上的“语言-文化”列中找到要使用的语言名称。
为单个语言编译 WiX 安装程序
要创建针对特定语言的单个安装程序,请将 language
值设置为字符串
{ "bundle": { "windows": { "wix": { "language": "fr-FR" } } }}
为列表中的每种语言编译 WiX 安装程序
要编译针对语言列表的安装程序,请使用数组。将为每种语言创建一个特定的安装程序,并将语言键作为后缀
{ "bundle": { "windows": { "wix": { "language": ["en-US", "pt-BR", "fr-FR"] } } }}
为每种语言配置 WiX 安装程序字符串
可以为每种语言定义配置对象以配置本地化字符串
{ "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 引用以下区域设置字符串:LaunchApp
、DowngradeErrorMessage
、PathEnvVarFeature
和 InstallAppFeature
。您可以定义自己的字符串,并在自定义模板或片段中使用 "!(loc.TheStringId)"
引用它们。有关更多信息,请参阅 WiX 本地化文档。
自定义 NSIS 安装程序
有关完整的自定义选项列表,请参阅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.nsi
文件,并定义您需要的钩子
!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 以使用该钩子文件
{ "bundle": { "windows": { "nsis": { "installerHooks": "./windows/hooks.nsi" } } }}
安装模式
默认情况下,安装程序将仅为当前用户安装您的应用程序。此选项的优点是安装程序不需要管理员权限即可运行,但应用程序安装在 %LOCALAPPDATA%
文件夹中,而不是 C:/Program Files
中。
如果您希望您的应用程序安装在系统范围内可用(这需要管理员权限),您可以将 installMode 设置为 perMachine
{ "bundle": { "windows": { "nsis": { "installMode": "perMachine" } } }}
或者,您可以通过将 installMode 设置为 both
,让用户选择应用程序是仅为当前用户安装还是在系统范围内安装。请注意,安装程序将需要管理员权限才能执行。
有关更多信息,请参阅NSISInstallerMode。
国际化
NSIS 安装程序是一个多语言安装程序,这意味着您始终拥有一个包含所有选定翻译的单个安装程序。
您可以使用 tauri.bundle.windows.nsis.languages
属性指定要包含的语言。NSIS GitHub 项目中提供了 NSIS 支持的语言列表。有一些Tauri 特定的翻译是必需的,因此如果您看到未翻译的文本,请随时在 Tauri 的主仓库中打开功能请求。您还可以提供自定义翻译文件。
默认情况下,操作系统默认语言用于确定安装程序语言。您还可以配置安装程序以在呈现安装程序内容之前显示语言选择器
{ "bundle": { "windows": { "nsis": { "displayLanguageSelector": true } } }}
最低 Webview2 版本
如果您的应用程序需要仅在较新 Webview2 版本中提供的功能(例如自定义 URI 方案),您可以指示 Windows 安装程序验证当前 Webview2 版本,并在与目标版本不匹配时运行 Webview2 引导程序。
{ "bundle": { "windows": { "nsis": { "minimumWebview2Version": "110.0.1531.0" } } }}
© 2025 Tauri Contributors. CC-BY / MIT