Flutter (3): Install Flutter

Time: Column:Mobile & Frontend views:224

Since Flutter builds release packages for both Android and iOS platforms, it requires both Android SDK and iOS SDK. Therefore, when installing Flutter, you also need to install the necessary platform build tools and SDKs. Below we will introduce how to set up the environment on Windows and macOS.

Note: The installation methods described in this section may change as Flutter is upgraded. If the information below is outdated by the time you're installing Flutter, please visit the Flutter official website and follow the latest installation tutorial.

1. Using Mirrors

Due to restrictions on accessing Flutter resources in China, Flutter has set up temporary mirrors for Chinese developers. You can add the following environment variables to your user environment variables:

export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

Note: These mirrors are temporary and cannot be guaranteed to be available indefinitely. You can check Flutter's China community page for updates on mirror servers.

2. Setting Up Flutter Development Environment on Windows

1) System Requirements

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

  • Operating System: Windows 7 or later (64-bit)

  • Disk Space: 400 MB (excluding disk space for Android Studio)

  • Tools: Flutter requires the following command-line tools:

2) Getting the Flutter SDK

Download the latest available Flutter SDK from the Flutter official website.

Note: The Flutter release channels may change over time. Always refer to the official website for the latest information. If you are in mainland China and have trouble accessing the list or downloading the packages, you may need to use a VPN. Alternatively, you can download the SDK from the Flutter GitHub repository.

Unzip the installation package to the directory where you want to install Flutter SDK (e.g., C:\src\flutter). Note: Do not install Flutter in a directory that requires elevated permissions, such as C:\Program Files.

Find flutter_console.bat in the Flutter installation directory, double-click it to start the Flutter command line, and you will be able to run Flutter commands from there.

Updating Environment Variables

If you want to run Flutter commands from the built-in Windows command prompt, you need to add the following to the user PATH environment variable:

  1. Search "env" in the Start Menu, and select Edit System Environment Variables.

  2. Under "User Variables," check if there is an entry named Path:

    • If it exists, append the full path to flutter\bin, using ; as the separator.

    • If it doesn't exist, create a new user variable named Path, and set its value to the full path to flutter\bin.

  3. Restart Windows to apply the changes.

Running flutter doctor

Run the following command in the Flutter command line to check if any dependencies are missing and install them if necessary:

flutter doctor

This command checks your environment and displays a report in the command-line window. The Dart SDK is bundled with the Flutter SDK, so there's no need to install Dart separately. Carefully review the output to see if there are additional software requirements or tasks that need to be performed.

For example:

[-] Android toolchain - develop for Android devices
    • Android SDK at D:\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.

When running the Flutter command (such as flutter doctor) for the first time, Flutter will download its dependencies and compile them. Subsequent runs will be faster. Make sure to install any missing dependencies and run flutter doctor again to verify the setup.

3) Android Setup

Flutter depends on a full installation of Android Studio. Not only does Android Studio manage Android platform dependencies and SDK versions, but it is also one of the recommended IDEs for Flutter development (though you can use other editors or IDEs, which will be discussed later).

Installing Android Studio

Download and install Android Studio. After launching Android Studio, complete the "Android Studio Setup Wizard." This installs the latest Android SDK, Android SDK Platform Tools, and Android SDK Build Tools, which are required for Android development with Flutter.

4) Installation Issues?

If you encounter issues during installation, check the official Flutter website to see if the installation methods have changed, or search online for solutions.

3. Setting Up Flutter Development Environment on macOS

On macOS, you can develop and test for both Android and iOS devices.

1) System Requirements

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

  • Operating System: macOS (64-bit)

  • Disk Space: 700 MB (excluding Xcode or Android Studio space)

  • Tools: Flutter depends on the following command-line tools:

    • bash, mkdir, rm, git, curl, unzip, which

2) Getting the Flutter SDK

Download the latest available Flutter SDK from the Flutter official website.

Note: The Flutter release channels may change over time. Always refer to the official website for the latest information. If you're in mainland China, you may need to use a VPN to access the SDK list or download the packages. Alternatively, you can download the SDK from the Flutter GitHub repository.

Unzip the package to the desired directory, for example:

cd ~/development
unzip ~/Downloads/flutter_macos_v0.5.1-beta.zip

Add Flutter tools to your PATH:

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

This code temporarily sets the PATH environment variable for the current command-line window. To permanently add Flutter to PATH, refer to the Updating Environment Variables section below.

Running flutter doctor

This step is the same as on Windows, so it won't be repeated here.

Updating Environment Variables

To add Flutter to PATH, so you can run Flutter commands from any terminal session, follow these steps:

To permanently modify the environment variable, you'll need to update files specific to your operating system. Typically, you'll add a command to set environment variables to a file executed when opening a new terminal window. For example:

  1. Locate the Flutter SDK installation directory, referred to as FLUTTER_INSTALL_PATH, which you'll use in the next steps.

  2. Open (or create) $HOME/.bash_profile. The file path and name might differ on your machine.

  3. Add the following path:

    export PATH=[FLUTTER_INSTALL_PATH]/flutter/bin:$PATH

    For example, if the Flutter installation directory is ~/code/flutter_dir, the command would be:

    export PATH=~/code/flutter_dir/flutter/bin:$PATH
  4. Run source $HOME/.bash_profile to refresh the current terminal window.

Note: If you are using the zsh terminal, the ~/.bash_profile will not be loaded when the terminal starts. To resolve this, modify ~/.zshrc and add: source ~/.bash_profile.

Verify that flutter/bin has been added to PATH:

echo $PATH

3) Installing Xcode

To develop Flutter apps for iOS, you need the latest version of Xcode:

  1. Install the latest version of Xcode.

  2. Configure Xcode command-line tools to use the newly installed version of Xcode by running:

    sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer

    This is the correct path for most users when using the latest version of Xcode. If you need to use a different version, specify the corresponding path.

  3. Ensure that the Xcode license agreement is accepted by opening Xcode once or by running:

    sudo xcodebuild -license

With Xcode, you can run Flutter apps on iOS devices or simulators.

4) Installing Android Studio

As with Windows, you need to install Android Studio to build and run Flutter programs on Android devices. Download and install Android Studio by following the instructions provided earlier.

4. Upgrading Flutter

1) Flutter SDK Channels

Flutter SDK has several release channels, such as beta, dev, master, and stable. The stable branch is the most reliable. After installing Flutter, you can run flutter channel to view all branches. For example, running the command on my local machine gives:

Flutter channels:
  beta
  dev
* master

The branch marked with * is the one your local Flutter SDK is currently tracking. To switch branches, use flutter channel beta or flutter channel master. It is recommended to follow the stable branch, but you can switch to the master branch to access the latest changes, though stability may be reduced.

2) Upgrading Flutter SDK and Dependencies

To upgrade the Flutter SDK, simply run the following command:

flutter upgrade

This command updates both the Flutter SDK and your project's dependencies. If you only want to update the project's dependencies (without upgrading Flutter SDK), you can use the following commands:

flutter packages get  # Retrieves all the project's dependencies.
flutter packages upgrade  # Retrieves the latest versions of all dependencies.