打包目标与安装包格式
本教程共 48 篇 · 第 44 篇 · 更新于 2026-08-09 · 约 12 分钟阅读
本节目标:读完你能分清 Tauri 2 在 Windows、macOS、Linux 上分别能打哪些格式的安装包,各有什么特点,以及怎么在
tauri.conf.json里配置。
写完代码、调试通过,下一步就是把应用打包成用户能安装的文件。Tauri 的 tauri build 命令会编译 Rust 代码、打包前端资源、生成各平台的安装包。但每个平台能打的格式不同,配置项也不同。本章按平台逐一拆解。
构建命令与版本管理
基本命令很简单:
tauri build
这会自动编译 release 版本并生成当前平台的所有配置格式。如果只想要特定格式:
tauri build --bundles msi
tauri build --bundles nsis
tauri build --bundles dmg,app
应用版本在 tauri.conf.json 的 version 字段管理。如果不设,Tauri 会读 src-tauri/Cargo.toml 里的 package.version。
Note不同平台对版本号格式有特殊限制,具体看各格式文档。但一般来说,遵循 SemVer(如
1.0.0)就不会出问题。
Windows:MSI 和 NSIS
Windows 上 Tauri 支持两种安装包格式:
| 格式 | 工具 | 特点 |
|---|---|---|
| MSI(.msi) | WiX Toolset v3 | Windows 官方安装包格式,只能在 Windows 上构建 |
| NSIS(-setup.exe) | NSIS | 轻量级安装程序,支持跨平台编译 |
MSI 安装包
MSI 是 Windows 传统的安装包格式,用 WiX Toolset 生成。构建命令:
tauri build --bundles msi
Warning构建 MSI 需要系统启用 VBSCRIPT 可选功能。大多数 Windows 默认开启,但如果遇到
failed to run light.exe错误,去「设置 → 应用 → 可选功能 → 更多 Windows 功能」手动启用。
MSI 支持国际化(多语言)。默认是 en-US,可以配多个语言生成多个安装包:
{
"bundle": {
"windows": {
"wix": {
"language": ["en-US", "zh-CN", "fr-FR"]
}
}
}
}
还可以用自定义 WiX 模板(.wxs 文件)完全替换安装逻辑,或用 WiX Fragments 扩展安装步骤(比如写注册表)。进阶配置参考官方文档的 WiX 配置部分。
NSIS 安装包
NSIS 是另一种 Windows 安装程序工具,生成 -setup.exe 文件。比 MSI 更轻量,而且支持跨平台编译——你可以在 Linux 或 macOS 上为 Windows 编译 NSIS 安装包。
tauri build --bundles nsis
NSIS 的安装模式(installMode)有三种:
{
"bundle": {
"windows": {
"nsis": {
"installMode": "currentUser"
}
}
}
}
currentUser(默认):只给当前用户安装,不需要管理员权限,装在%LOCALAPPDATA%perMachine:全机器安装,需要管理员权限,装在C:\Program Filesboth:让用户选择,需要管理员权限
NSIS 也支持多语言安装界面:
{
"bundle": {
"windows": {
"nsis": {
"languages": ["English", "SimplifiedChinese"],
"displayLanguageSelector": true
}
}
}
}
displayLanguageSelector 设为 true 时,安装时先弹语言选择框。
WebView2 安装策略
Windows 上的 Tauri 应用依赖 WebView2 运行时。Tauri 提供了五种安装策略,通过 tauri.conf.json 配置:
{
"bundle": {
"windows": {
"webviewInstallMode": {
"type": "downloadBootstrapper"
}
}
}
}
| 类型 | 需要联网 | 安装包增大 | 说明 |
|---|---|---|---|
downloadBootstrapper(默认) | 是 | 0MB | 运行时下载引导程序,体积最小 |
embedBootstrapper | 是 | ~1.8MB | 内嵌引导程序,Windows 7 兼容更好 |
offlineInstaller | 否 | ~127MB | 内嵌离线安装器,适合无网环境 |
fixedRuntime | 否 | ~180MB | 内嵌固定版本 WebView2,完全自控 |
skip | 否 | 0MB | 不安装,用户须自行装好 WebView2 |
TipWindows 10(2018 年 4 月更新及以后)和 Windows 11 系统自带 WebView2 运行时,大多数情况用默认的
downloadBootstrapper就行。只有面向 Windows 7 或离线环境才需要考虑其他策略。
跨平台编译 Windows 安装包
在 Linux 或 macOS 上编译 NSIS 安装包需要额外工具:
- 安装 NSIS(Linux:
sudo apt install nsis,macOS:brew install nsis) - 安装 LLVM 和 LLD 链接器
- 添加 Windows Rust target:
rustup target add x86_64-pc-windows-msvc - 安装 cargo-xwin:
cargo install --locked cargo-xwin - 构建命令加上 runner 和 target 参数
Note跨平台编译 Windows 安装包不如直接在 Windows 上构建稳定,建议仅在无法使用 Windows 机器时作为备选方案。CI/CD 里推荐用 GitHub Actions 的
windows-latestrunner。
32 位和 ARM64 支持
默认编译当前机器架构。需要支持 32 位:
rustup target add i686-pc-windows-msvc
tauri build --target i686-pc-windows-msvc
ARM64 需要先在 Visual Studio Installer 里装「C++ ARM64 build tools」,然后:
rustup target add aarch64-pc-windows-msvc
tauri build --target aarch64-pc-windows-msvc
macOS:DMG 和 APP
macOS 上 Tauri 主要打两种产物:App Bundle(.app)和 DMG(磁盘映像)。
DMG 安装包
DMG 是 macOS 最常见的非 App Store 分发格式。用户打开后看到一个安装窗口,把应用图标拖到 Applications 文件夹即可。
tauri build --bundles dmg
可以自定义 DMG 安装窗口的外观:
{
"bundle": {
"macOS": {
"dmg": {
"background": "./images/dmg-bg.png",
"windowSize": {
"width": 800,
"height": 600
},
"appPosition": {
"x": 180,
"y": 220
},
"applicationFolderPosition": {
"x": 480,
"y": 220
}
}
}
}
}
background 可以放一张带箭头的背景图,引导用户拖拽安装。窗口默认大小 660×400。
Warning在 CI/CD 平台上构建 DMG 时,图标位置和大小可能不生效,这是一个已知问题。详见 tauri#1731。
Universal Binary
支持同时打 Apple Silicon 和 Intel 架构:
tauri build --target universal-apple-darwin
这样生成的包两种 Mac 都能跑。如果只面向 Apple Silicon,可以设最低系统版本为 12.0:
{
"bundle": {
"macOS": {
"minimumSystemVersion": "12.0"
}
}
}
App Store 分发
如果要上架 Mac App Store,需要打 .app 包再用 productbuild 生成 .pkg:
tauri build --bundles app --target universal-apple-darwin
xcrun productbuild --sign "证书名" --component "target/universal-apple-darwin/release/bundle/macos/$APPNAME.app" /Applications "$APPNAME.pkg"
App Store 还要求配置 Provisioning Profile、Entitlements(沙盒)和 Info.plist(加密合规声明)。详见第 45 章代码签名部分。
Linux:多种格式并存
Linux 发行版太多,包管理器各不相同。Tauri 支持以下格式:
| 格式 | 适用场景 | 特点 |
|---|---|---|
| AppImage | 通用 | 单文件免安装,体积大 |
| deb | Debian/Ubuntu 系 | apt 包管理器安装 |
| rpm | Fedora/RHEL 系 | yum/dnf 包管理器安装 |
| Flatpak | 沙盒应用 | 跨发行版,需 Flathub |
| Snap | 沙盒应用 | Ubuntu 主推,需 Snap Store |
AppImage
AppImage 把应用和所有依赖打包成一个文件,用户 chmod +x 后直接运行,不需要安装:
tauri build --bundles appimage
好处是跨发行版、免安装;坏处是体积大(70MB+),因为把依赖都打进去了。
NoteAppImage 的兼容性受 glibc 版本限制。建议在尽可能老的系统上构建(Ubuntu 22.04 或 Debian 12),这样生成的包在新系统上也能跑。反过来就不行——新系统上构建的包可能跑在老系统上会报
GLIBC_2.33 not found。
如果应用需要播放音视频,开启 bundleMediaFramework 会把 GStreamer 相关文件也打包进去:
{
"bundle": {
"linux": {
"appimage": {
"bundleMediaFramework": true
}
}
}
}
Debian 包(deb)
deb 是 Debian/Ubuntu 系的安装包格式。Tauri 生成的 deb 包会自动配置好图标、Desktop 文件和依赖声明:
tauri build --bundles deb
默认依赖 libwebkit2gtk-4.1-0 和 libgtk-3-0,如果用了系统托盘还会加 libappindicator3-1。你还可以声明额外依赖:
{
"bundle": {
"linux": {
"deb": {
"depends": ["libssl-dev"]
}
}
}
}
可以往 deb 包里加自定义文件:
{
"bundle": {
"linux": {
"deb": {
"files": {
"/usr/share/README.md": "../README.md",
"/usr/share/assets": "../assets/"
}
}
}
}
}
RPM 包
rpm 面向 Fedora/RHEL/openSUSE 等发行版:
tauri build --bundles rpm
RPM 支持预安装/后安装脚本:
{
"bundle": {
"linux": {
"rpm": {
"preInstallScript": "./scripts/preinstall.sh",
"postInstallScript": "./scripts/postinstall.sh",
"preRemoveScript": "./scripts/preremove.sh",
"postRemoveScript": "./scripts/postremove.sh"
}
}
}
}
还能配 depends(依赖)、conflicts(冲突)、provides(提供)、obsoletes(废弃)等 RPM 元数据。
Flatpak
Flatpak 是一种沙盒化的跨发行版包格式,主要通过 Flathub 分发。配置相对复杂,需要:
- 用
flatpak-builder-tools生成 Node.js 和 Cargo 的依赖源文件 - 编写 AppStream MetaInfo(XML 格式的应用元数据)
- 编写 Flatpak Manifest(YAML 格式的构建配置)
- 提交到 Flathub 审核
Tip较新版本的 GNOME 运行时已包含大部分 Tauri 应用所需的依赖,但具体版本兼容性建议查阅 Flatpak 官方文档。如果用了系统托盘,需要在 manifest 里加
--talk-name=org.kde.StatusNotifierWatcher权限。
Snap
Snap 是 Canonical 主推的沙盒包格式,在 Ubuntu 上预装。需要创建 snapcraft.yaml 配置文件:
name: appname
base: core22
version: '0.1.0'
summary: 你的应用简介
description: |
你的应用描述
grade: stable
confinement: strict
apps:
appname:
command: usr/bin/appname
desktop: usr/share/applications/appname.desktop
extensions: [gnome]
parts:
build-app:
plugin: dump
build-snaps: [node/20/stable, rustup/latest/stable]
build-packages:
- libwebkit2gtk-4.1-dev
- build-essential
- curl
- wget
- file
- libxdo-dev
- libssl-dev
- libayatana-appindicator3-dev
- librsvg2-dev
stage-packages:
- libwebkit2gtk-4.1-0
- libayatana-appindicator3-1
source: .
override-build: |
set -eu
npm install
npm run tauri build -- --bundles deb
dpkg -x src-tauri/target/release/bundle/deb/*.deb $SNAPCRAFT_PART_INSTALL/
构建和发布:
sudo snapcraft # 构建
snapcraft login # 登录
snapcraft upload --release=stable mysnap_latest_amd64.snap # 发布
应用商店分发
Microsoft Store
Tauri 目前生成 EXE 和 MSI 安装包,Microsoft Store 要求安装包支持静默安装。NSIS 的静默参数是 /S,MSI 的是 /quiet。
上架 Microsoft Store 的安装包必须使用 offlineInstaller 模式的 WebView2(因为商店环境可能无网)。可以用单独的配置文件:
{
"bundle": {
"windows": {
"webviewInstallMode": {
"type": "offlineInstaller"
}
}
}
}
构建时合并配置:tauri build --config src-tauri/tauri.microsoftstore.conf.json。
Apple App Store
上架 App Store 需要开发者账号、代码签名、Provisioning Profile 和 Entitlements。构建 Universal Binary 后用 productbuild 打包成 .pkg,再用 xcrun altool 上传。详见第 45 章和官方文档。
小结
Tauri 2 在 Windows 上支持 MSI(WiX)和 NSIS 两种安装包,macOS 上打 DMG 和 App Bundle,Linux 上可选 AppImage、deb、rpm、Flatpak、Snap 五种格式。各格式的配置都在 tauri.conf.json 的 bundle 字段下。Windows 的 WebView2 安装策略(downloadBootstrapper/embedBootstrapper/offlineInstaller/fixedRuntime/skip)按部署环境选择。跨平台编译 NSIS 可行但不如原生构建稳定。Linux 各格式要注意 glibc 版本兼容,建议在老系统上构建。应用商店分发有额外要求(静默安装、离线 WebView2、代码签名等),需要单独配置。