在 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
    • git 2.x
    • mkdir
    • rm
    • unzip
    • which
    • xz-utils
  • 公用库: 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

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

获取 Flutter SDK

Get the Flutter SDK

  1. 下载以下安装包来获取最新的 stable 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 releases page.

  2. 将文件解压到目标路径, 比如:

    Extract the file in the desired location, for example:

    $ cd ~/development
    $ tar xf ~/Downloads/flutter_linux_vX.X.X-stable.tar.xz
    
  3. 配置 flutter 的 PATH 环境变量:

    Add the flutter tool to your path:

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

    这个命令配置了 PATH 环境变量,且只会在你 当前 命令行窗口中生效。如果想让它永久生效,请查看 更新 PATH 环境变量

    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

这个命令会检查你当前的配置环境,并在命令行窗口中生成一份报告。安装 Flutter 会附带安装 Dart SDK,所以不需要再对 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.dev/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

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. Determine the directory where you placed the Flutter SDK. You need this in Step 3.
  2. 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. 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. Run source $HOME/.<rc file> to refresh the current window, or open a new terminal window to automatically source the file.
  5. Verify that the flutter/bin directory is now in your PATH by running:

    $ echo $PATH
    

    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. Determine the directory where you placed the Flutter SDK.

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

      $ sudo nano /etc/profile
    
  3. 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. Once you start a new session, verify that the flutter command is available by running:

    $ which flutter
    

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 Platform-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’ll 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_HOME 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 > Tools > Android > AVD Manager,然后选择 Create Virtual Device 选项。(只有在 Android 项目中才会显示 Android 子选项。)

    Launch Android Studio > Tools > Android > AVD Manager and select Create Virtual Device. (The Android submenu is only present when inside an Android project.)

  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

如果要在 Web 平台运行 Flutter 应用,你必须 安装 Chrome 浏览器

To prepare to run and test your Flutter app on the web, you must install Chrome, if you haven’t already.

下一步

Next step

编辑器设置。

Set up your preferred editor.