mac 电脑升级后 出现sass 无法编译,然后我安装sass出现一下错误,网上找了很多方法都不能解决
sudo gem install sass
Building native extensions. This could take a while...
ERROR: Error installing sass:
ERROR: Failed to build gem native extension.
current directory: /Library/Ruby/Gems/2.6.0/gems/ffi-1.15.5/ext/ffi_c
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby -I /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0 -r ./siteconf20230226-19112-3yzmby.rb extconf.rb
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/$(RUBY_BASE_NAME)
--with-ffi_c-dir
--without-ffi_c-dir
--with-ffi_c-include
--without-ffi_c-include=${ffi_c-dir}/include
--with-ffi_c-lib
--without-ffi_c-lib=${ffi_c-dir}/lib
--enable-system-libffi
--disable-system-libffi
--with-libffi-config
--without-libffi-config
--with-pkg-config
--without-pkg-config
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:467:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:546:in `block in try_link0'
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/tmpdir.rb:93:in `mktmpdir'
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:543:in `try_link0'
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:570:in `try_link'
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:672:in `try_ldflags'
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:1832:in `pkg_config'
from extconf.rb:9:in `system_libffi_usable?'
from extconf.rb:42:in `<main>'
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/Library/Ruby/Gems/2.6.0/extensions/universal-darwin-21/2.6.0/ffi-1.15.5/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /Library/Ruby/Gems/2.6.0/gems/ffi-1.15.5 for inspection.
Results logged to /Library/Ruby/Gems/2.6.0/extensions/universal-darwin-21/2.6.0/ffi-1.15.5/gem_make.out
--------------------
分割线
这是我 查看版本报的错
sass -v
-bash: /usr/local/bin/sass: /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby: bad interpreter: No such file or directory
我想试试安装最新的ruby
brew install ruby
==> Downloading https://formulae.brew.sh/api/formula.json
##O#-#
==> Downloading https://formulae.brew.sh/api/cask.json
######################################################################## 100.0%
Warning: ruby 3.2.1 is already installed and up-to-date.
To reinstall 3.2.1, run:
brew reinstall ruby
但是查看版本确实2.6不知道是不是这个问题导致sass无法安装
ruby -v
ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.x86_64-darwin21]
请到老们看看
后来我解决了
虽然ruby安装到最新版本,但ruby-v后版本仍然是老版本
ruby -v
ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.x86_64-darwin21]
这时需把 ruby 最新可执行文件导入全局变量 PATH 中即可,使用如下命令:
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
再ruby-v
ruby 3.2.1 (2023-02-08 revision 31819e82c8) [x86_64-darwin18]
安装sass
gem install sass
提示
Successfully installed sass-3.7.4
但是我sass -v
-bash: /usr/local/bin/sass: /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby: bad interpreter: No such file or directory
在安装完sass后有提示我升级gem
Run `gem update --system 3.4.7` to update your installation
我就升级gem
gem update --system
sass -v 就可以显示版本号了
该回答引用GPTᴼᴾᴱᴺᴬᴵ
这个错误提示是安装 gem 时编译 native extensions 出错导致的,具体原因可能是缺少必要的库或头文件。根据错误提示,你需要先安装开发工具才能编译 native extensions。你可以尝试运行以下命令安装开发工具:
xcode-select --install
如果已经安装了 Xcode 开发工具,但是仍然遇到这个问题,可以尝试更新 Xcode 开发工具,或者运行以下命令更新 Xcode 开发工具的路径:
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
如果你已经更新了 Xcode 开发工具并且仍然遇到问题,可以查看 mkmf.log 文件来获取更详细的错误信息。你可以运行以下命令打开 mkmf.log 文件:
less /Library/Ruby/Gems/2.6.0/extensions/universal-darwin-21/2.6.0/ffi-1.15.5/mkmf.log
在查看 mkmf.log 文件时,可以查找关键词“error”、“warning”、“fatal”等,以确定导致编译错误的原因。如果你还是无法解决问题,可以尝试使用 RVM 或 rbenv 等工具来安装 Ruby,以避免对系统环境造成影响。