更新ruby与cocoapods

更新ruby

升级ruby环境

sudo gem update --system

遇到了下面的错误:

Updating rubygems-update
Fetching rubygems-update-3.6.8.gem
ERROR:  Error installing rubygems-update:
    invalid gem: package is corrupt, exception while verifying: wrong number of arguments (given 2, expected 1) (ArgumentError) in /Users/xxx/.rvm/rubies/ruby-3.0.0/lib/ruby/gems/3.0.0/cache/rubygems-update-3.6.8.gem
ERROR:  While executing gem ... (NoMethodError)
    undefined method `version' for nil:NilClass

解决办法:

# 通过homebrew安装ruby
brew install ruby --verbose

查看本地ruby的安装目录

~ which -a ruby
/opt/homebrew/opt/ruby/bin/ruby
/usr/bin/ruby

查看当前使用的ruby版本

ruby -v

系统自带ruby,如果发现当前使用的是系统自带的ruby,可以通过配置.zshrc环境变量来修改成使用安装的ruby版本

~ echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc
~ source ~/.zshrc

这里的/opt/homebrew/opt/ruby/bin就是~ which -a ruby命令列出来的ruby的安装地址
也可以使用vim编辑器去编辑.zshrc

~ echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc

~ export LDFLAGS="-L/usr/local/opt/ruby/lib"

~ export CPPFLAGS="-I/usr/local/opt/ruby/include"

~ export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"

~ source ~/.zshrc

重新执行命令sudo gem update --system即可

Updating rubygems-update
Fetching rubygems-update-3.6.8.gem
Successfully installed rubygems-update-3.6.8
Parsing documentation for rubygems-update-3.6.8
Installing ri documentation for rubygems-update-3.6.8
Done installing documentation for rubygems-update after 1 seconds
Parsing documentation for rubygems-update-3.6.8
Done installing documentation for rubygems-update after 0 seconds
Installing RubyGems 3.6.8
  Successfully built RubyGem
  Name: bundler
  Version: 2.6.8
  File: bundler-2.6.8.gem
Bundler 2.6.8 installed
RubyGems 3.6.8 installed
Regenerating binstubs
Regenerating plugins
Parsing documentation for rubygems-3.6.8
Installing ri documentation for rubygems-3.6.8

...

Ruby Interactive (ri) documentation was installed. ri is kind of like man 
pages for Ruby libraries. You may access it like this:
  ri Classname
  ri Classname.class_method
  ri Classname#instance_method
If you do not wish to install this documentation in the future, use the
--no-document flag, or set it as the default in your ~/.gemrc file. See
'gem help env' for details.

RubyGems system software updated

查看当前安装的ruby

gem source -l

移出ruby

gem sources --remove https://xxxx.xxxxxx.xxx/

添加新的ruby

gem sources -a https://xxxx.xxxxxx.xxx/

查看RubyGems的环境配置

~ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 3.5.7
  - RUBY VERSION: 3.2.2 (2023-03-30 patchlevel 53) [x86_64-darwin21]
  - INSTALLATION DIRECTORY: /Users/sfh/.rvm/gems/ruby-3.2.2
  - USER INSTALLATION DIRECTORY: /Users/sfh/.gem/ruby/3.2.0
  - RUBY EXECUTABLE: /Users/sfh/.rvm/rubies/ruby-3.2.2/bin/ruby
  - GIT EXECUTABLE: /usr/bin/git
  - EXECUTABLE DIRECTORY: /Users/sfh/.rvm/gems/ruby-3.2.2/bin
  - SPEC CACHE DIRECTORY: /Users/sfh/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /Users/sfh/.rvm/rubies/ruby-3.2.2/etc
  - RUBYGEMS PLATFORMS:
     - ruby
     - x86_64-darwin-21
  - GEM PATHS:
     - /Users/sfh/.rvm/gems/ruby-3.2.2
     - /Users/sfh/.rvm/rubies/ruby-3.2.2/lib/ruby/gems/3.2.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
     - :sources => ["https://gems.ruby-china.com/"]
     - :concurrent_downloads => 8
  - REMOTE SOURCES:
     - https://gems.ruby-china.com/
  - SHELL PATH:
     ...
    

安装或更新cocoapods

查看cocoapods版本

pod --version

安装cocoapods:

sudo gem install -n /usr/local/bin cocoapods

更新cocoapods:

sudo gem update cocoapods

如果安装了多个Xcode使用下面的命令选择(一般选择较新的版本)

sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer

安装本地repo仓库

# 官方命令,已失效
pod setup

# 使用克隆命令安装,需科学上网
git clone https://github.com/CocoaPods/Specs.git ~/.cocoapods/repos/trunk

# 使用国内镜像
git clone https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git  ~/.cocoapods/repos/trunk

更新本地repo仓库:

pod repo update
或者
pod install --repo-update

安装或更新本地repo仓库可能会需要比较长的时间。本地仓库更新完成后,一般即可搜索到指定的第三方库,如果仍然搜索不到,可删除cocoapods的搜索缓存后重试

rm ~/Library/Caches/CocoaPods/search_index.json 

cocoapods卸载当前版本:

sudo gem uninstall cocoapods

cocoapods下载指定版本:

sudo gem install cocoapods -v 1.16.1
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。