在 Linux 操作系统上安装和配置 Flutter 开发环境

系统配置要求

System requirements

要想安装和运行 Flutter,你的开发环境至少应该满足如下的需求:

To install and run Flutter, your development environment must meet these minimum requirements:

  • 操作系统: Linux (64 位)

    Operating Systems: Linux (64-bit)

  • 磁盘空间: 600MB (不包含安装 IDE 和其他工具的空间)

    Disk Space: 600 MB (does not include disk space for IDE/tools).

  • 命令工具: Flutter 需要你的开发环境中已经配置了以下命令行工具。

    Tools: Flutter depends on these command-line tools being available in your environment.

    • bash
    • curl
    • file
    • git 2.x
    • mkdir
    • rm
    • unzip
    • which
    • xz-utils
    • zip
  • 公用库: Flutter 的 test 命令需要你的系统安装或存在如下的公用库。

    Shared libraries: Flutter test command depends on this library being available in your environment.

    • libGLU.so.1 - 由 mesa 套件 (packages) 提供,比如 Ubuntu/Debian 系统下的 libglu1-mesa,以及 Fedora 系统下的 mesa-libGLU

      libGLU.so.1 - provided by mesa packages such as libglu1-mesa on Ubuntu/Debian and mesa-libGLU on Fedora.

获取 Flutter SDK{#get-sdk}

Get the Flutter SDK

在 Linux 上你有两种方式安装 Flutter。

On Linux, you have two ways you can install Flutter.

使用 snapd 安装 Flutter

Install Flutter using snapd

最简单的方式是使用 snapd 在 Linux 上安装 Flutter。更多详情请查看:安装 snapd

The easiest way to install Flutter on Linux is by using snapd. For more information, see Installing snapd.

如果你已经有 snapd 了,那么你可以通过 Snap 商店来安装 Flutter,或者通过以下命令安装:

Once you have snapd, you can install Flutter using the Snap Store, or at the command line:

$ sudo snap install flutter --classic

手动安装 Flutter

Install Flutter manually

如果你没有 snapd,或者你无法使用它,那么你可以通过以下步骤安装 Flutter。

If you don’t have snapd, or can’t use it, you can install Flutter using the following steps.

  1. 通过下载下面的安装包以获得最新 stable release 版本的 Flutter SDK:

    Download the following installation bundle to get the latest stable release of the Flutter SDK:

    (loading…)

    对于其他发布频道以及更久的构建版本,请查看 SDK 存档页面。

    For other release channels, and older builds, see the SDK archive page.

  2. 将文件解压到合适的地方,例如:

    Extract the file in the desired location, for example:

    $ cd ~/development
    $ tar xf ~/Downloads/flutter_linux_vX.X.X-stable.tar.xz
    

    如果你不想安装安装包的补丁,你可以跳过步骤 1 或步骤 2,直接获取 Github 上 Flutter 仓库 的源码并执行以下命令:

    If you don’t want to install a fixed version of the installation bundle, you can skip steps 1 and 2. Instead, get the source code from the Flutter repo on GitHub with the following command:

    $ git clone https://github.com/flutter/flutter.git
    

    你也可以按你的需要切换分支或者tag。例如,你可以使用 stable 版本的分支:

    You can also change branches or tags as needed. For example, to get just the stable version:

    $ git clone https://github.com/flutter/flutter.git -b stable --depth 1
    
  3. flutter 工具添加到环境变量中:

    Add the flutter tool to your path:

    $ export PATH="$PATH:`pwd`/flutter/bin"
    

    用这个命令添加 PATH 仅在当前的命令行视窗生效。要将 Flutter 永久添加到环境变量中,请参阅 更新您的路径

    This command sets your PATH variable for the current terminal window only. To permanently add Flutter to your path, see Update your path.

  4. 可选步骤,提前下载二进制开发文件:

    Optionally, pre-download development binaries:

    flutter 工具将下载所需的平台特殊开发二进制文件。对于预下载这些工件更好的做法是(例如,在系统构建环境中,网络可能出现不通畅的问题),通过运行下面命令提前下载 iOS 和 Android 的二进制文件:

    The flutter tool downloads platform-specific development binaries as needed. For scenarios where pre-downloading these artifacts is preferable (for example, in hermetic build environments, or with intermittent network availability), iOS and Android binaries can be downloaded ahead of time by running:

    $ flutter precache
    

    对于这些可选的下载项,请参考 flutter help precache

    For additional download options, see flutter help precache.

你现在可以运行 Flutter 命令了!

You are now ready to run Flutter commands!

运行 flutter doctor

Run flutter doctor

运行以下命令以查看是否还有缺失的依赖需要安装,你需要安装这些依赖以完成设置(要看到详细输出,请添加 -v 标识):

Run the following command to see if there are any dependencies you need to install to complete the setup (for verbose output, add the -v flag):

$ flutter doctor

该命令将检查你的环境情况并显示汇报在命令行窗口中。 Dart SDK 已经绑在 Flutter 中了;你无需单独再安装 Dart。仔细检查你是否还有需要安装的东西,或者要执行的任务(在该文字中提示的)。

This command checks your environment and displays a report to the terminal window. The Dart SDK is bundled with Flutter; it is not necessary to install Dart separately. Check the output carefully for other software you might need to install or further tasks to perform (shown in bold text).

例如:

For example:

[-] Android toolchain - develop for Android devices
    • Android SDK at /Users/obiwan/Library/Android/sdk
    ✗ Android SDK is missing command line tools; download from https://goo.gl/XxQghQ
    • Try re-installing or updating your Android SDK,
      visit https://flutter.cn/setup/#android-setup for detailed instructions.

上面的部分描述了如何执行这些任务,并完成设置流程。

The following sections describe how to perform these tasks and finish the setup process.

当你已经安装了全部缺失的依赖之后,请再次运行 flutter doctor 命令,以验证你是否是真的全部正确设置完毕了。

Once you have installed any missing dependencies, run the flutter doctor command again to verify that you’ve set everything up correctly.

更新你的环境变量

Update your path

获取 Flutter SDk 中说的一样,你可以通过命令行更新当前窗口的环境变量。但你也许会想要让它一直生效,在任何终端中都可以运行 flutter 命令。

You can update your PATH variable for the current session at the command line, as shown in Get the Flutter SDK. You’ll probably want to update this variable permanently, so you can run flutter commands in any terminal session.

下面是更改环境变量的详细步骤,这将会对所有终端生效,且仅在该机器上生效。通常,每当你打开一个新的窗口,都会将一行添加到执行的文件。例如:

The steps for modifying this variable permanently for all terminal sessions are machine-specific. Typically you add a line to a file that is executed whenever you open a new window. For example:

  1. 检测你存放 Flutter SDK 的文件夹,你需要在第三步用到它。

    Determine the directory where you placed the Flutter SDK. You need this in Step 3.

  2. 在你的 shell 中打开(或者创建)rc 文件。例如,Linux 默认使用 Bash shell,所以编辑 $HOME/.bashrc 文件。如果你使用不同的 shell,那么在你电脑上的文件路径以及文件名必须不同。

    Open (or create) the rc file for your shell. For example, Linux uses the Bash shell by default, so edit $HOME/.bashrc. If you are using a different shell, the file path and filename will be different on your machine.

  3. 添加下面内容,并将 [PATH_TO_FLUTTER_GIT_DIRECTORY] 改到你 clone Flutter 的 git 仓库目录下。

    Add the following line and change [PATH_TO_FLUTTER_GIT_DIRECTORY] to be the path where you cloned Flutter’s git repo:

    $ export PATH="$PATH:[PATH_TO_FLUTTER_GIT_DIRECTORY]/flutter/bin"
    
  4. 运行 source $HOME/.<rc file> 刷新当前窗口或者打开一个新的终端窗口就会自动更新这个文件。

    Run source $HOME/.<rc file> to refresh the current window, or open a new terminal window to automatically source the file.

  5. 通过运行以下命令验证 flutter/bin 确实加到环境变量中了:

    Verify that the flutter/bin directory is now in your PATH by running:

    $ echo $PATH
    

    通过运行以下命令验证 flutter 命令:

    Verify that the flutter command is available by running:

    $ which flutter
    

直接更新环境变量

Update path directly

在某些情况下,你的分发可能无法长期获取使用上面提到的环境变量。发生这种情况时,你可以直接更改环境变量文件。但这需要管理员权限:

In some cases, your distribution may not permanently acquire the path when using the above directions. When this occurs, you can change the environment variables file directly. These instructions require administrator privileges:

  1. 找到你存放 Flutter SDK 的目录。

    Determine the directory where you placed the Flutter SDK.

  2. 定位系统根目录下的 etc 文件夹,然后用 root 权限打开 profile 文件。

    Locate the etc directory at the root of the system, and open the profile file with root privileges.

      $ sudo nano /etc/profile
    
  3. 更新 Flutter SDK 文件夹所在路径的字符串。

    Update the PATH string with the location of your Flutter SDK directory.

    if [ "`id -u`" -eq 0 ]; then
       PATH="..."
    else
       PATH="/usr/local/bin:...:[PATH_TO_FLUTTER_GIT_DIRECTORY]/flutter/bin"
    fi
    export PATH
    
  4. 结束当前会话并重启系统。

    End the current session or reboot your system.

  5. 当你重新启动了新会话,请确认 flutter 命令已经可以执行:

    Once you start a new session, verify that the flutter command is available by running:

    $ which flutter
    

更多关于如何在 Bash 中配置环境变量的信息请查看这条 StackExchange 提问。更多关于如何在 Z shell 中配置环境变量的信息请查看 这条 StackExchange 提问

For more details on setting the path in Bash, see this StackExchange question. For information on setting the path in Z shell, see this StackOverflow question.

设置 Android 开发环境

Android setup

安装 Android Studio

Install Android Studio

  1. 下载并安装 Android Studio

    Download and install Android Studio.

  2. 运行 Android Studio,并进入 ‘Android Studio Setup Wizard’,这会安装最新的 Android SDK, Android SDK Platform-Tools 以及 Android SDK Build-Tools,这些都是在开发 Android Flutter 应用时所需要的。

    Start Android Studio, and go through the ‘Android Studio Setup Wizard’. This installs the latest Android SDK, Android SDK Command-line Tools, and Android SDK Build-Tools, which are required by Flutter when developing for Android.

配置 Android 设备

Set up your Android device

在 Android 设备上运行或测试 Flutter 应用之前,你需要一个运行 Android 4.1(API 版本 16)或者更高的设备。

To prepare to run and test your Flutter app on an Android device, you need an Android device running Android 4.1 (API level 16) or higher.

  1. 在设备上打开 Developer optionsUSB debugging 选项,你可以在 Android documentation 上查看更详细的方法介绍。

    Enable Developer options and USB debugging on your device. Detailed instructions are available in the Android documentation.

  2. 如果是在 Windows 平台上使用,需要安装 Google USB Driver

    Windows-only: Install the Google USB Driver.

  3. 通过 USB 接口连接手机和电脑,如果在设备上弹出需要授权弹窗,允许授权以便让电脑能够访问你的开发设备。

    Using a USB cable, plug your phone into your computer. If prompted on your device, authorize your computer to access your device.

  4. 在命令行中,使用 flutter devices 命令来确保 Flutter 能够识别出你所连接的 Android 设备。

    In the terminal, run the flutter devices command to verify that Flutter recognizes your connected Android device. By default, Flutter uses the version of the Android SDK where your adb tool is based. If you want Flutter to use a different installation of the Android SDK, you must set the ANDROID_SDK_ROOT environment variable to that installation directory.

默认情况下,Flutter 会使用当前版本 adb 工具所依赖的 Android SDK 版本,如果你想让 Flutter 使用别的 Android SDK,你可以通过设置 ANDROID_HOME 环境变量来达到这个目的。

By default, Flutter uses the version of the Android SDK where your adb tool is based. If you want Flutter to use a different installation of the Android SDK, you must set the ANDROID_HOME environment variable to that installation directory.

配置 Android 模拟器

Set up the Android emulator

根据以下步骤来将 Flutter 应用运行或测试于你的 Android 模拟器上:

To prepare to run and test your Flutter app on the Android emulator, follow these steps:

  1. 激活机器上的 VM acceleration 选项。

    Enable VM acceleration on your machine.

  2. 打开 Android Studio,点击 AVD Manager 按钮,选择 Create Virtual Device…

    Launch Android Studio, click the AVD Manager icon, and select Create Virtual Device…

    • 在一些旧的 Android Studio 版本里,需要通过 Android Studio > Tools > Android > AVD Manager,然后选择 Create Virtual Device 选项。(只有在 Android 项目中才会显示 Android 子选项。)

      In older versions of Android Studio, you should instead launch Android Studio > Tools > Android > AVD Manager and select Create Virtual Device. (The Android submenu is only present when inside an Android project.)

    • 如果你以及还没打开某个项目,你可以选择 Configure > AVD Manager 然后选择 Create Virtual Device 选项

      If you do not have a project open, you can choose Configure > AVD Manager and select Create Virtual Device…

  3. 选择相应的设备并选择 Next 选项。

    Choose a device definition and select Next.

  4. 选择一个或多个你想要模拟的 Android 版本的系统镜像,然后选择 Next 选项。推荐选择 x86 或者 x86_64 镜像。

    Select one or more system images for the Android versions you want to emulate, and select Next. An x86 or x86_64 image is recommended.

  5. 在 Emulated Performance 下选择 Hardware - GLES 2.0 选项来开启 硬件加速

    Under Emulated Performance, select Hardware - GLES 2.0 to enable hardware acceleration.

  6. 确保 AVD 选项配置正确,并选择 Finish 选项。

    Verify the AVD configuration is correct, and select Finish.

    想要查看上述步骤的更多详细信息,请查看 Managing AVDs 页面。

    For details on the above steps, see Managing AVDs.

  7. 在 Android Virtual Device Manager 中,点击工具栏中的 Run 选项,模拟器会启动并为你所选择的系统版本和设备显示出相应的界面。

    In Android Virtual Device Manager, click Run in the toolbar. The emulator starts up and displays the default canvas for your selected OS version and device.

Web 平台设置

Web setup

现在你已经可以通过使用 beta 渠道的 Flutter SDK 构建 Web 应用了。要开始,请通过阅读 这些步骤 完成设置。

Flutter has early support for building web applications using the beta channel of Flutter. To add support for web development, follow these instructions when you’ve completed the setup above.

下一步

Next step

编辑器设置。

Set up your preferred editor.