43

I have a stable flutter channel SDK located at c:\flutter. which is set at the system environment variables to be the default path for Flutter.

And I'm using this path c:\flutter when creating new Flutter project in IntelliJ for our customers.

I also downloaded Flutter master channel at c:\flutter_master and I need to use this flutter SDK (master) for another project.

How I can correctly have two working flutter version on the same device for different projects without playing with the system environment variables each time?

Flutter IO Dev
  • 1,759
  • 5
  • 15
  • 20

11 Answers11

48

Firstly you need to download all the flutter SDKs you would want to be able to switch locally and create aliases for it. This allows you to use multiple versions of the SDK through the command line or the terminal, Just like you use any flutter command, And Incase you want to use these different versions of your SDK in your IDE, you need to add the SDK paths to the settings of your IDE. Below you can find the steps to add the path to vscode. The below answer will help you setup the different versions of SDK regardless of whether you are on Windows, Linux, or mac.

Creating alias on Mac/Linux

This is how I have done it on an M1 mac,

I have different versions of flutter SDKs downloaded in a Documents folder located at $HOME/Documents

enter image description here

In order to access the appropriate version of flutter through the terminal, we need to create aliases. Think of aliases as a shortcut to accessing the SDK through the terminal.

  1. To create an alias you need to create .bash_aliases file inside your $HOME directory

you can do this via terminal by running

nano ~/.bash_aliases

Paste these aliases with the appropriate path in the file.

alias flutterd='~/Documents/flutter_dev/bin/flutter'
alias flutterm='~/Documents/flutter_master/bin/flutter'
alias flutterb='~/Documents/flutter_beta/bin/flutter'

Note that you can name the aliases as you like.

I have used the name

  • flutterd to point to flutter_dev
  • flutterm to point to flutter_master
  • flutterb to point to flutter_beta

that means when you type flutterd in the terminal then it will use the SDK located at ~/Documents/flutter_dev/bin/flutter and respectively for rest of the aliases.

(Hit ctrl + x and enter to save and exit)

  1. And lastly, you need to add this in your shell file
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

which is basically the rc file

$HOME/.bashrc if you are using bash

$HOME/.zshrc file if you are using zsh

if you are not sure then typing echo $SHELL in your Terminal tells you which shell you’re using. This is the same file where you have added your flutter sdk's path when you first installed it. And if the file doesn't exist you may create it.

  1. Run source $HOME/.<rc file> to refresh the current terminal window.

Now you can verify by typing your alias names in the terminal flutterm, flutterd etc and it will respond from the respective sdk.

you can verify this by running <alias name> doctor -v

e.g to verify flutterd is pointing to dev run flutterd doctor -v

Here is my output when I run the command

enter image description here

Creating alias on Windows

On windows, I have the flutter SDKs stored in C:/flutter_sdk

enter image description here

and then create an Alias folder and create batch files corresponding to each flutter SDK, where each batch file contains the path to flutter SDK

enter image description here

e.g flutterd.bat contains the path to dev sdk

@echo off
C:\flutter_sdk\dev\bin\flutter %*

Name your batch files wisely, because you will be using them from the command line. e.g I have a batch file named as flutterb.bat to point to the beta channel, so to access the beta SDK I will use flutterb in the command line and not flutter.

and finally, we need to add the alias folder to the environment variable in order to make it accessible throughout windows.

Go to environment variables => user variables => Path => edit=> new

enter image description here

Now you can verify if everything works fine by opening command prompt and enter flutterb doctor and it should show the SDK pointing to beta

enter image description here

Adding multiple SDK versions to VScode

Now to access the appropriate version of the SDK in vscode you need to add these SDK paths in settings.

  • In Vscode settings (Code->Preferences->Settings) search for sdk path

  • Under Flutter SDK paths add all the paths

enter image description here

  • Now when you open a flutter project you can choose your desired version by clicking on the flutter version at the bottom enter image description here

  • And it will prompt you to choose the sdk to use

enter image description here

Note that if you are changing versions from vscode, you should also run flutter pub get from the right top icon in pubspec.yaml, so that the source code updates as per the choosen sdk. You may confirm this by looking at the class definition of the source code.

Mahesh Jamdade
  • 17,235
  • 8
  • 110
  • 131
  • Does it work on building iOS? I have trouble running different versions using xCode. I am not sure if there is way to tell xCode to use different sdk? – bounxye Dec 02 '21 at 15:05
  • Good question! Yes, there is a way you can either `flutter clean` and `flutter pub get` from vscode and flutter will set the currently selected path at `ios/Flutter/generated.xcConfig` or you can manually set the SDK path in that file. – Mahesh Jamdade Dec 02 '21 at 18:05
  • I tried above steps it gives me error `\Documents\flutter_sdk\flutter_windows_2.10.5-stable\flutter' is not recognized as an internal or external command, operable program or batch file.` Did I miss something? – Nicks Jul 05 '22 at 12:34
  • @Nicks I would recommend checking the above answer again, probably you have made some mistake when adding path to batch files and also ensure you have added the alias path in environment variables and try the flutter command in new instance of cmd – Mahesh Jamdade Jul 05 '22 at 14:02
  • Be careful if you install npm globally. If you `source ~/.zshrc `, then don't forget to `source ~/.nvm/nvm.sh` . Or else you can't find npm included in your path – Alphapico Aug 27 '22 at 09:09
  • One more step that was required : After the above steps, In the bottom of VS code, there is a '{}' icon, click on it and restart Dart Language Server to see the above changes. This is for people who have different version of Dart as well being used ( not only Flutter) – Akshay Chopra Jan 10 '23 at 10:44
  • So when I do all these steps I am getting a warning that `The flutter binary is not on your path. Consider adding/Users/lakshay/Developer/flutter/bin to your path`. should I just continue on & ignore the warning or have I missed something? I am on the M1 platform. – L.Goyal Jun 26 '23 at 07:34
  • The problem with this approach is that your bash scripts now have things like "flutterd", so they're not likely to work on other people's machines. I think PATH variables are simpler and don't create the "flutterd" issue. See my answer below. – thebiggestlebowski Aug 30 '23 at 16:02
19

Flutter SDK can be specified per workspace if you use VSCode. You need to:

  1. Clone flutter repo to a new folder:
mkdir ~/flutter_dev
cd ~/flutter_dev
git clone https://github.com/flutter/flutter.git .
  1. Create .vscode/settings.json with the following content:
{
  "dart.flutterSdkPath": "/Users/youruser/flutter_dev"
}
  1. Restart VSCode and you're good to go.

See more info in Dart Code - Quickly Switching Between SDK Versions

Andrey Gordeev
  • 30,606
  • 13
  • 135
  • 162
  • 3
    The linked docs for Dart Code plugin are excellent, especially the note the doc includes on using `git worktree` to have multiple Flutter channels installed using a single local Flutter SDk install (git repo). – Maks May 19 '20 at 23:23
  • 1
    "settings": { "dart.flutterSdkPath": "/Users/youruser/flutter_dev" } where I have to write this line , in which file , please more explanation – Sana'a Al-ahdal Oct 28 '20 at 18:21
  • 1
    @Sana'aAl-ahdal see this: https://code.visualstudio.com/docs/getstarted/settings – Andrey Gordeev Oct 29 '20 at 05:28
  • 1
    Thanks for the reply, I fixed it by doing and creating Alias , like the one described here https://medium.com/@sarbagyastha/using-two-or-more-different-versions-of-flutter-in-single-machine-484293c2a7ff – Sana'a Al-ahdal Oct 29 '20 at 17:31
  • A small note: If you git clone the flutter repo inside a folder called 'flutter_dev', then you need to reference the flutter folder inside the flutter_dev folder. E.g. "/Users/youruser/flutter_dev/flutter" – Vasco Feb 23 '21 at 15:55
  • In the article it said git worktree add ../flutter-master origin/master, so I can quickly switch between flutter/dart sdk versions. But, in the status bar it doesn’t clickable, just show existing version (2.2.3). Any suggest? – bandungeuy Feb 20 '22 at 16:19
  • @bandungeuy the status bar button won't be clickable. It's not a toggle, it's just a text indicating the current Flutter version that VSCode is using. You need to update the version manually in `settings.json` and restart VSCode. – Andrey Gordeev Feb 22 '22 at 09:32
  • @AndreyGordeev that what I’m asking related to above article. Anyway, finally I did it, the status bar now clickable, so if I want to switch flutter version, just click it and select other version. https://stackoverflow.com/questions/71196386/vscode-quickly-switch-between-flutter-versions – bandungeuy Feb 22 '22 at 12:56
  • @bandungeuy thanks a lot, I didn't know it was possible! My mistake. – Andrey Gordeev Feb 23 '22 at 07:10
  • In my vs code, It has double backward slashes: `{"dart.flutterSdkPath": "C:\\src\\3.10.0"}` – Hardik May 30 '23 at 07:07
12

Now you can use “FVM”, and it's Flutter Version Management, A simple CLI to manage Flutter SDK versions per project.

1 you can use it by this command

pub global activate fvm

2 after that you can install any versions you want like

fvm install stable

fvm install 'flutter version'

it will be installed at

 /Users/'username'/fvm/versions/

3 to switch versions:

fvm use 'flutter version'

for more info, visit FVM.app

Mohamed Reda
  • 1,207
  • 13
  • 21
8

According to this issue on Flutter's repo iqbalmineraltown has the answer:

You might want to download multiple version as you need, because each Flutter SDK version is tightly coupled with specific Dart SDK.

You set the Flutter version for each project, and iqbalmineraltown highlights a way if you're using VS Code:

If you're using VSCode, you can download multiple version of flutter SDK into different path and quickly switch between them using Dart&Flutter Plugin You can set default SDK for each project by providing default SDK path for each workspace. So when you open a project, VSCode will use the version you specified for that project.

flarkmarup
  • 5,129
  • 3
  • 24
  • 25
7

If you are using Android Studio, you can set difference version of Flutter for each project from menu File => Setting => Languages & Frameworks => Flutter: enter image description here

And to run the flutter/dart from the command line for multiple flutter version, you can follow this article https://medium.com/@sarbagyastha/using-two-or-more-different-versions-of-flutter-in-single-machine-484293c2a7ff

I Made Mudita
  • 480
  • 5
  • 11
3

You can simply rename the folder without playing with the system environment variables each time.

  1. Suppose you downloaded Flutter 2.0.5 under flutter folder & 2.2.2 under flutter_2.2.2 folder, added flutter to environment path as C:\src\flutter.
  2. Whenever you want 2.2.2 version rename flutter_2.2.2 to flutter & flutter to flutter_2.0.5. enter image description here
manoj reddy
  • 64
  • 1
  • 4
3

I recommend using this tutorial over here, I made some changes for Mac:

For UNIX/Mac based OS users

Edit $HOME/.bashrc (for me it was $HOME/.bash_profile on Mac) file and add this line (if it doesn’t exist).

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
  1. Create and edit $HOME/.bash_aliases and add aliases as you want.
alias flutter2='~/development/sdks/flutter2/bin/flutter'
alias flutter='~/development/sdks/flutter/bin/flutter'

Using same technique I've kept flutter2 setup seprate from flutter1. Because in some projects I need two and in older projects the version 1.

Now open specific project in VSCode and open command palette (on Mac command was Command + Shift + P) and search for flutter sdk. enter image description here

Now choose Auto-Detect, which will create a new file under root of your project .vscode/settings.json. enter image description here

Open this file and place the path of flutter version you want to use like below:

{
  "dart.flutterSdkPath": "/Users/imran/flutter2"
}

Do remember to run flutter2 --doctor, to resolve any issues

While setting up flutter2 I encountered an issue and fix for that is here I am getting error "cmdline-tools component is missing" after installing Flutter and Android Studio... I added the Android SDK. How can I solve them?.

Jaumzera
  • 2,305
  • 1
  • 30
  • 44
Imran Zahoor
  • 2,521
  • 1
  • 28
  • 38
1

You can use FVM to manage versions of flutter https://fvm.app

brianha289
  • 338
  • 1
  • 8
0

Having several versions of SDKs installed and adjusting project specific IDE SDK settings seem reasonable, but I prefer commandline tools and when you call flutter command, it always calls the one on the path. So I can't use this solution.

Alias solution mentioned in this post is preferable for me, but when used it, I often forgot to call with alias (such as flutterb), leading to calling wrong version of flutter. After accidentally doing this, I usually need to do a flutter clean as well.

What I use, inspired by alias solution, is that I added a flutter.bat file in the root folder of the project. It contains the following lines

@echo off
C:\SDK\flutter1\bin\flutter %*

As you may guess, this location belongs to the specific flutter installation for using with this project. It is not the version on the path, but since I call all flutter commands from the root folder of the project, my terminal session sees this flutter first and it uses this version, rather than the global one on the path. You can confirm this by calling flutter doctor for the first time.

- Note that this is for using constant versions for each project, rather than trying several versions on the same project. For that kind of use case, you can combine this with the alias solution.
- Also note that this is a Windows solution.

mselmany
  • 116
  • 4
0

I don't think any of the answers so far are adequate. Operating systems created the PATH variable to solve just this problem. The problem with creating aliases, such as "flutterd" is that flutterd only works on your machine.

Here's a cleaner way to do things.

  1. Create a script called "setup-env.sh" in your project root directory:

    #!/bin/sh
    
    export PATH=/path/to/your/flutter/version:$PATH
    
  2. Anytime you work on this project, just do "source ./setup-env.sh" and you will be pointing to the correct flutter version.

thebiggestlebowski
  • 2,610
  • 1
  • 33
  • 30
0

The best answer these days is Puro. I've stopped using FVM entirely, and switched over to Puro. See the homepage for how it is better in many ways than FVM or by hand.

Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70