[MacOS] Install and manage various Flutter versions by FVM

[MacOS] Install and manage various Flutter versions by FVM

2023-08-05 hit count image

Let's see how to use FVM(Flutter Version Management) to install and manage the various Flutter version on one MacOS machine.

Outline

When developing multiple Flutter projects on one machine (PC), there are cases where you need to build a development environment depending on the Flutter version used in each project.

At this time, you cannot delete Flutter every time and install the Flutter version required for the project. In this blog post, I will introduce how to use FVM(Flutter Version Management) to install and manage various Flutter versions on one machine.

Install FVM

Execute the following command to install FVM.

brew tap leoafarias/fvm
brew install fvm

.zshrc file

To use FVM to manage the version of Flutter, you need to modify the .zshrc file. Open the ~/.zshrc file and add the followings.

export PATH="$PATH:$HOME/.pub-cache/bin"
export PATH="$PATH:$HOME/fvm/default/bin"
alias flutter="fvm flutter"

Flutter installed using FVM can be used with the fvm flutter command. To make this easier, add alias flutter="fvm flutter" to use the Flutter command without the fvm keyword.

Flutter version list

Execute the following command to check the Flutter versions that can be installed with FVM.

fvm releases

Then, you can see the list of Flutter versions that can be installed as follows.

...
Jun 8 23   │ 3.12.0
Jun 14 23  │ 3.10.5
Jun 21 23  │ 3.12.0-1.1.pre
Jul 12 23  │ 3.13.0-0.1.pre
--------------------------------------
Jul 12 23  │ 3.10.6            stable
--------------------------------------
--------------------------------------
Jul 19 23  │ 3.13.0-0.2.pre    beta
--------------------------------------

Install multiple Flutter versions

In this blog post, I will install Flutter versions 3.10.6 and 2.10.4. Execute the following command to install Flutter versions 3.10.6 and 2.10.4.

fvm install 3.10.6
fvm install 2.10.4

If you have installed Flutter without using FVM, delete it and install Flutter using the above command.

rm -rf ~/development/flutter

fvm list

After the installation is complete, execute the following command to check the installed Flutter versions.

fvm list

Then, you can see the Flutter versions installed using FVM as follows.

3.10.6
2.10.4

flutter version

Execute the following command to check the current Flutter version.

flutter --version

Then, you can see the current Flutter version as follows.

Flutter 2.10.4 • channel stable • https://github.com/flutter/flutter.git
Framework • revision c860cba910 (1 year, 4 months ago) • 2022-03-25 00:23:12 -0500
Engine • revision 57d3bac3dd
Tools • Dart 2.16.2 • DevTools 2.9.2

Use the installed Flutter version

Now, let’s see how to use the Flutter version installed with FVM. If you run the following command, you can use Flutter 3.10.6 globally.

fvm global 3.10.6

You can use the following command only for the current project.

fvm use 3.10.6

When you run the fvm use command, the .fvm folder is created in the current project (folder) where the command is executed, and the fvm_config.json file and flutter_sdk folder are created.

.fvm folder

When you run the fvm use command, the Flutter SDK of the specified version is installed in the .fvm folder. However, this SDK does not need to be managed by Git, so open the .gitignore file and modify it as follows.

.fvm/flutter_sdk

The version of Flutter specified by fvm use is recorded in the .fvm/fvm_config.json file. When you open the .fvm/fvm_config.json file, you can see the following contents.

{
  "flutterSdkVersion": "3.10.6",
  "flavors": {}
}

If you need to share the Flutter version of the project with other developers, commit this file to Git.

If FVM is installed in another development environment and the .fvm/fvm_config.json file exists in the Flutter project, you can install the required Flutter SDK for the Flutter project by running the following command.

fvm install

Delete Flutter version

If you no longer need the Flutter version installed on the machine, you can remove it with the following command.

fvm remove 2.10.4

After deleting the Flutter version, run the following command to check the Flutter version deleted on the machine.

fvm list

Then, you can see that the 2.10.4 version has been deleted as follows.

Cache Directory:  /Users/deku/fvm/versions

3.10.6 (global)

VSCode

If you are a developer using VSCode, open the .vscode/settings.json file and modify it as follows.

{
  "dart.flutterSdkPath": ".fvm/flutter_sdk",
  "search.exclude": {
    "**/.fvm": true
  },
  "files.watcherExclude": {
    "**/.fvm": true
  }
}

If you modify it as above, you can automatically use the Flutter SDK installed with FVM when you open the Flutter project with VSCode.

Problem solving

When changing the Flutter version with fvm global

When you change the Flutter version with fvm global, the following error message may appear.

Flutter "stable" has been set as global
However your "flutter" path current points to:
.
to use global Flutter SDK through FVM you should change it to:
/Users/{user_name}/fvm/default/bin

This error occurs because the PATH is not set correctly. To resolve this, open the .zshrc file and check that the following is added.

export PATH="$PATH:$HOME/fvm/default/bin"

When running flutter doctor

When you run flutter doctor(fvm flutter doctor), the following warning may appear.

[!] Flutter (Channel stable, 3.10.6, on macOS 13.3.1 22E772610a darwin-x64, locale ja-JP)
    ! Warning: `dart` on your path resolves to /usr/local/Cellar/dart/2.18.0/libexec/bin/dart, which is not inside your current Flutter SDK checkout at /Users/deku/fvm/versions/3.10.6. Consider adding /Users/deku/fvm/versions/3.10.6/bin to the front of your path.

This error is caused by a conflict with the previously installed Dart. To resolve this, run the following command to remove the previously installed Dart.

brew uninstall dart

Complete

Done! we’ve seen how to use FVM to install and manage various Flutter versions on the same machine. If you need a development environment for various Flutter versions, try using FVM to manage the versions.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts