flutter搭建mac开发环境, 前期准备,至少20G+的硬盘空间,否则xcode都安装不了, 以前也搭建过mac环境,不过cocoapads版本比较老 跳转
- clone flutter最新版本
- 安装xcode
- 升级ruby到最新版本, 再安装cocoapods
- flutter doctor验证安装完成
下载代码, 进入安装目录,执行一下./bin/flutter主文件
$ git clone https://github.com/flutter/flutter.git
$ cd flutter
$ ./bin/flutter
安装xcode
从appstore下载会提示磁盘空间不足,这里改从下面链接下载
mac版本与xcode版本对应,下载对应xcode版本
由于mac系统12.7.4的版本,安装对应xcode也并非最新版本
一步一步安装xcode,完成后,显示占用空间23G, 后
增加flutter到系统环境变量
$ echo 'export PATH=$PATH:/Users/apple/Flutter/bin' >> ~/.bash_profile
$ source ~/.bash_profile
$ flutter --version
Framework • revision 638e47146d (13 hours ago) • 2024-03-23 15:16:54 -0400
Engine • revision 7aa8521e64
Tools • Dart 3.4.0 (build 3.4.0-266.0.dev) • DevTools 2.34.0-dev.12
$ flutter doctor
...略
[!] Xcode - develop for iOS and macOS (Xcode 14.2)✗ CocoaPods not installed.CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side.Without CocoaPods, plugins will not work on iOS or macOS.For more info, see https://flutter.dev/platform-pluginsTo install see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.
[✓] Chrome - develop for the web
...略
这里提示CocoaPods not installed,开始安装cocoapods, 使用gem安装
$ gem which cocoapods
ERROR: Can't find Ruby library file or shared library cocoapods
以下未添加sudo
$ gem install cocoapods
ERROR: While executing gem ... (Gem::FilePermissionError)You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.
以下未安装最新版本ruby
ERROR: Error installing cocoapods:ERROR: Failed to build gem native extension.current directory: /Library/Ruby/Gems/2.6.0/gems/ffi-1.13.1/ext/ffi_c
安装最新版本raby (注意: 此处使用brew前面不要添加sudo)
$ brew reinstall ruby
...略
==> Reinstalling ruby
==> Pouring ruby--3.1.1.monterey.bottle.tar.gz
==> Caveats
By default, binaries installed by gem will be placed into:/usr/local/lib/ruby/gems/3.1.0/binYou may want to add this to your PATH.ruby is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.If you need to have ruby first in your PATH, run:echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrcFor compilers to find ruby you may need to set:export LDFLAGS="-L/usr/local/opt/ruby/lib"export CPPFLAGS="-I/usr/local/opt/ruby/include"For pkg-config to find ruby you may need to set:export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"
设置ruby的系统环境变量
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.bash_profile
echo 'export LDFLAGS="-L/usr/local/opt/ruby/lib"' >> ~/.bash_profile
echo 'export CPPFLAGS="-I/usr/local/opt/ruby/include"' >> ~/.bash_profile
再使用gem安装cocoapods
$ gem install -n /usr/local/bin cocoapods
...略
Done installing documentation for rexml, nanaimo, colored2, claide, CFPropertyList, atomos, xcodeproj, ruby-macho, nap, molinillo, gh_inspector, fourflusher, escape, cocoapods-try, netrc, cocoapods-trunk, cocoapods-search, cocoapods-plugins, cocoapods-downloader, cocoapods-deintegrate, ffi, ethon, typhoeus, public_suffix, fuzzy_match, concurrent-ruby, httpclient, algoliasearch, addressable, tzinfo, i18n, connection_pool, activesupport, cocoapods-core, cocoapods after 33 seconds
35 gems installed
// 安装完成
$ pod --version
1.15.2
最后验证结果,
flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[!] Flutter (Channel master, 3.21.0-13.0.pre.25, on macOS 12.7.4 21H1123 darwin-x64, locale zh-Hans-CN)
...略
[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
[✓] Chrome - develop for the web
[!] Android Studio (not installed)
[✓] VS Code (version 1.85.2)
[✓] Connected device (2 available)
[!] Network resources
...略
最后,到此就flutter的mac环境搭建就完成