flutter-setting-up-on-linux

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Setting Up a Linux Environment for Flutter Development

为Flutter开发搭建Linux环境

Contents

目录

System Dependencies

系统依赖

To build and run Flutter applications on a Linux desktop, install the required C/C++ toolchain and system libraries. Flutter relies on
dart:ffi
to interface with Linux system calls and the GTK framework for UI rendering.
Required packages for Debian/Ubuntu-based distributions:
  • Core Utilities:
    curl
    ,
    git
    ,
    unzip
    ,
    xz-utils
    ,
    zip
  • Build Tools:
    clang
    ,
    cmake
    ,
    ninja-build
    ,
    pkg-config
  • Libraries:
    libglu1-mesa
    ,
    libgtk-3-dev
    ,
    libstdc++-12-dev
要在Linux桌面端构建和运行Flutter应用程序,需要安装必要的C/C++工具链和系统库。Flutter依赖
dart:ffi
与Linux系统调用交互,并依赖GTK框架进行UI渲染。
基于Debian/Ubuntu的发行版所需软件包:
  • 核心工具:
    curl
    ,
    git
    ,
    unzip
    ,
    xz-utils
    ,
    zip
  • 构建工具:
    clang
    ,
    cmake
    ,
    ninja-build
    ,
    pkg-config
  • 库文件:
    libglu1-mesa
    ,
    libgtk-3-dev
    ,
    libstdc++-12-dev

Workflow: Configuring the Linux Toolchain

工作流:配置Linux工具链

Follow this sequential workflow to prepare the Linux host for Flutter desktop development.
Task Progress:
  • 1. Update package lists and upgrade existing packages.
  • 2. Install core utilities and build dependencies.
  • 3. Configure IDE/Editor with Flutter support.
  • 4. (Conditional) Enable Linux support on ChromeOS.
1. Update and Install Dependencies Execute the following command to install all required packages on Debian/Ubuntu systems:
bash
sudo apt-get update -y && sudo apt-get upgrade -y
sudo apt-get install -y curl git unzip xz-utils zip libglu1-mesa clang cmake ninja-build pkg-config libgtk-3-dev libstdc++-12-dev
2. Conditional: ChromeOS Setup
  • If developing on a Chromebook: Turn on Linux support in the ChromeOS settings. Ensure the Linux container is fully updated using the
    apt-get
    commands above before proceeding.
3. IDE Configuration Install Visual Studio Code, Android Studio, or an IntelliJ-based IDE. Install the official Dart and Flutter extensions/plugins to enable language server features and debugging capabilities.
按照以下顺序工作流准备Linux主机以进行Flutter桌面开发。
任务进度:
  • 1. 更新软件包列表并升级现有软件包。
  • 2. 安装核心工具和构建依赖。
  • 3. 为IDE/编辑器配置Flutter支持。
  • 4. (可选)在ChromeOS上启用Linux支持。
1. 更新并安装依赖 在Debian/Ubuntu系统上执行以下命令安装所有必要的软件包:
bash
sudo apt-get update -y && sudo apt-get upgrade -y
sudo apt-get install -y curl git unzip xz-utils zip libglu1-mesa clang cmake ninja-build pkg-config libgtk-3-dev libstdc++-12-dev
2. 可选:ChromeOS配置
  • 如果在Chromebook上开发: 在ChromeOS设置中开启Linux支持。在继续之前,确保使用上述
    apt-get
    命令将Linux容器完全更新。
3. IDE配置 安装Visual Studio Code、Android Studio或基于IntelliJ的IDE。安装官方的Dart和Flutter扩展/插件,以启用语言服务器功能和调试能力。

Workflow: Validating the Environment

工作流:验证环境

Run this feedback loop to ensure the toolchain is correctly recognized by the Flutter SDK.
Task Progress:
  • 1. Run environment validator.
  • 2. Verify connected Linux devices.
  • 3. Resolve toolchain errors.
1. Run Validator Execute the Flutter diagnostic tool with verbose output:
bash
flutter doctor -v
2. Review and Fix (Feedback Loop)
  • If errors exist under the "Linux toolchain" section: Review the missing dependencies, install the flagged packages, and re-run
    flutter doctor -v
    . Repeat until the Linux toolchain section passes.
3. Verify Device Availability Ensure the Linux desktop is recognized as a valid deployment target:
bash
flutter devices
Expected Output: At least one entry must display with the platform marked as linux.
运行以下反馈循环以确保Flutter SDK能正确识别工具链。
任务进度:
  • 1. 运行环境验证工具。
  • 2. 验证已连接的Linux设备。
  • 3. 解决工具链错误。
1. 运行验证工具 执行带详细输出的Flutter诊断工具:
bash
flutter doctor -v
2. 检查并修复(反馈循环)
  • 如果“Linux toolchain”部分存在错误: 检查缺失的依赖,安装标记的软件包,然后重新运行
    flutter doctor -v
    。重复此步骤直到Linux toolchain部分通过验证。
3. 验证设备可用性 确保Linux桌面被识别为有效的部署目标:
bash
flutter devices
预期输出: 至少有一个条目显示平台为linux

Workflow: Preparing for Distribution (Snapcraft)

工作流:为发布做准备(Snapcraft)

When preparing a release build for the Snap Store, configure the Snapcraft build environment.
Task Progress:
  • 1. Install Snapcraft and LXD.
  • 2. Configure LXD.
  • 3. Build the Snap package.
1. Install Build Tools
bash
sudo snap install snapcraft --classic
sudo snap install lxd
2. Configure LXD Initialize LXD and add the current user to the
lxd
group:
bash
sudo lxd init
sudo usermod -a -G lxd <your_username>
Note: Log out and log back in to apply the group changes.
3. Build the Snap Navigate to the project root containing the
snap/snapcraft.yaml
file and execute the build:
bash
snapcraft --use-lxd
当准备为Snap Store构建发布版本时,配置Snapcraft构建环境。
任务进度:
  • 1. 安装Snapcraft和LXD。
  • 2. 配置LXD。
  • 3. 构建Snap包。
1. 安装构建工具
bash
sudo snap install snapcraft --classic
sudo snap install lxd
2. 配置LXD 初始化LXD并将当前用户添加到
lxd
组:
bash
sudo lxd init
sudo usermod -a -G lxd <your_username>
注意:注销并重新登录以应用组更改。
3. 构建Snap包 导航到包含
snap/snapcraft.yaml
文件的项目根目录并执行构建命令:
bash
snapcraft --use-lxd

Examples

示例

Baseline
snapcraft.yaml
Configuration

基础
snapcraft.yaml
配置

Use this template for the
<project_root>/snap/snapcraft.yaml
file when packaging a Flutter Linux app for the Snap Store.
yaml
name: super-cool-app
version: 0.1.0
summary: Super Cool App
description: Super Cool App that does everything!

confinement: strict
base: core22
grade: stable

slots:
  dbus-super-cool-app:
    interface: dbus
    bus: session
    name: org.bar.super_cool_app

apps:
  super-cool-app:
    command: super_cool_app
    extensions: [gnome]
    plugs:
    - network
    slots:
      - dbus-super-cool-app

parts:
  super-cool-app:
    source: .
    plugin: flutter
    flutter-target: lib/main.dart
当为Snap Store打包Flutter Linux应用时,可使用此模板作为
<project_root>/snap/snapcraft.yaml
文件。
yaml
name: super-cool-app
version: 0.1.0
summary: Super Cool App
description: Super Cool App that does everything!

confinement: strict
base: core22
grade: stable

slots:
  dbus-super-cool-app:
    interface: dbus
    bus: session
    name: org.bar.super_cool_app

apps:
  super-cool-app:
    command: super_cool_app
    extensions: [gnome]
    plugs:
    - network
    slots:
      - dbus-super-cool-app

parts:
  super-cool-app:
    source: .
    plugin: flutter
    flutter-target: lib/main.dart