Reputation: 2187
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 the path C:\flutter when creating new Flutter project in IntelliJ for our customers.
I also downloaded the Flutter master channel at C:\flutter_master and I need to use this Flutter SDK (master) for another project.
How can I correctly have two working Flutter versions on the same device for different projects without playing with the system environment variables each time?
Upvotes: 60
Views: 51163
Reputation: 101
Put all the versions folders in the Documents folder. (as shown below:)
alias flutter_sn='~/Documents/flutter_3.10.5/flutter/bin/flutter'
alias flutter_rg='~/Documents/flutter_3.13.7/flutter/bin/flutter'
export PATH=”$PATH:/Users/yourUserName/Documents/flutter_3.13.9/flutter/bin”
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
“/Users/yourUserName/Documents/flutter_3.10.5”
Upvotes: 0
Reputation: 2797
I recommend using this tutorial over here. I made some changes for Mac:
For Unix/Mac-based OS users
Edit file $HOME/.bashrc (for me it was file $HOME/.bash_profile on Mac) file and add this line (if it doesn’t exist).
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
alias flutter2='~/development/sdks/flutter2/bin/flutter'
alias flutter='~/development/sdks/flutter/bin/flutter'
Using the same technique, I've kept the flutter2
setup separate from flutter1
. Because in some projects I need two and in older projects the version 1.
Now open the specific project in Visual Studio Code and open command palette (on Mac, the command was Command + Shift + P) and search for flutter sdk
.
Now choose Auto-Detect, which will create a new file under root of your project's .vscode/settings.json file.
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?.
Upvotes: 4
Reputation: 29
Use Visual Studio Code to do that:
Download multiple Flutter versions from this Flutter official site flutter_archives.
Then set up those ZIP files like this:
Open Visual Studio Code and go to settings. Change settings like this (use double slashes "\")
Now you can easily switch between Flutter SDKs by pressing Ctrl + Shift + P and searching for Flutter SDKs.
Upvotes: 0
Reputation: 444
Having several versions of SDKs installed and adjusting project-specific IDE SDK settings seem reasonable, but I prefer commandline tools and when you call the flutter
command, it always calls the one on the path. So I can't use this solution.
The 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 the 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.
Upvotes: 0
Reputation: 32559
The Flutter SDK can be specified per workspace if you use Visual Studio Code. You need to:
Clone the Flutter repository to a new folder:
mkdir ~/flutter_dev
cd ~/flutter_dev
git clone https://github.com/flutter/flutter.git .
Create .vscode/settings.json
with the following content:
{
"dart.flutterSdkPath": "/Users/youruser/flutter_dev"
}
Restart Visual Studio Code and you're good to go.
See more information in Dart Code - Quickly Switching Between SDK Versions.
Upvotes: 21
Reputation: 20379
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 in case 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 Visual Studio Code. The below answer will help you setup the different versions of SDK regardless of whether you are on Windows, Linux, or Mac.
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
.
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.
To create an alias you need to create .bash_aliases
file inside your $HOME directory
You can do this via a 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_devflutterm
to point to flutter_masterflutterb
to point to flutter_betaThat 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.)
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 Z shell (executable 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.
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
.
For example, to verify flutterd is pointing to dev, run flutterd doctor -v
.
Here is my output when I run the command:
On Windows, I have the Flutter SDKs stored in C:/flutter_sdk
.
And then create an Alias folder and create batch files corresponding to each flutter SDK, where each batch file contains the path to a Flutter SDK.
For example, flutterd.bat
contains the path to the 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 useflutterb
in the command line and notflutter
.
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.
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
Now to access the appropriate version of the SDK in Visual Studio Code you need to add these SDK paths in settings.
In Visual Studio Code settings (Code → Preferences → Settings), search for SDK path
Under Flutter SDK paths, add all the paths
Now when you open a Flutter project, you can choose your desired version by clicking on the Flutter version at the bottom
And it will prompt you to choose the SDK to use.
Note that if you are changing versions from Visual Studio Code, you should also run flutter pub get
from the right top icon in pubspec.yaml, so that the source code updates as per the chosen SDK. You may confirm this by looking at the class definition of the source code.
Upvotes: 64
Reputation: 570
If you are using Android Studio, you can set difference version of Flutter for each project from menu File → Setting → Languages & Frameworks → Flutter:
And to run Flutter/Dart from the command line for multiple Flutter versions, you can follow the article Using two or more different versions of Flutter on a single machine.
Upvotes: 10
Reputation: 5439
According to this issue in Flutter's repository, 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 Visual Studio Code:
If you're using VSCode [sic], 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 [sic] will use the version you specified for that project.
Upvotes: 8
Reputation: 538
First the download the required flutter SDK.
If you are using Android Studio, you can change the Flutter version by navigating to Android Studio -> Settings -> Languages and Frameworks -> Flutter. After changing the version, apply the settings and check the bottom section to confirm the current version.
For double confirmation, you can also go to Tools -> Flutter -> Flutter Doctor, where you can see the Flutter version used by the project.
Upvotes: 0
Reputation: 44220
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.
Upvotes: 1
Reputation: 2799
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.
Create a script called "setup-env.sh" in your project root directory:
#!/bin/sh
export PATH=/path/to/your/flutter/version:$PATH
Anytime you work on this project, just do "source ./setup-env.sh" and you will be pointing to the correct flutter version.
Upvotes: 1
Reputation: 1617
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
Upvotes: 18
Reputation: 127
You can simply rename the folder without playing with the system environment variables each time.
Upvotes: 3