[Ruby on Rails (RoR)] Use RubyGems Mirror to speed up downloading gems

RubyGems Mirrors

Please use a newer RubyGems version as much as possible, 2.6.x or above is recommended.

1
2
3
4
$ gem update --system

$ gem -v
2.6.3

Replace https://rubygems.org/ with https://gems.ruby-china.com/ or other mirrors.

1
2
3
4
$ gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/

$ gem sources -l
https://gems.ruby-china.com

Or mirror https://rubygems.org/ with https://gems.ruby-china.com/.

1
2
3
$ bundle config mirror.https://rubygems.org https://gems.ruby-china.com

# $ bundle config mirror.https://rubygems.org https://mirrors.tuna.tsinghua.edu.cn/rubygems

It will append the BUNDLE_SET and value into ~/.bundle/config.

1
2
3
4
5
# ~/.bundle/config

---
BUNDLE_SET: "mirror.https://rubygems.org https://gems.ruby-china.com"
# BUNDLE_SET: "mirror.https://rubygems.org https://mirrors.tuna.tsinghua.edu.cn/rubygems"

Or, edit ~/.gemrc,append https://mirrors.tuna.tsinghua.edu.cn/rubygems/ to sources fields。

1
2
3
4
5
--- 
:sources:
- https://rubygems.org
- https://gems.ruby-china.com
- https://mirrors.tuna.tsinghua.edu.cn/rubygems

See Project specific .gemrc files using the GEMRC environment variable | Jordan Elver | Ruby on Rails Developer, Bristol, UK - https://jordanelver.co.uk/blog/2020/12/06/project-specific-gemrc-files-using-the-gemrc-environment-variable/ to learn more.

So you don’t need to change the source of your Gemfile.

1
2
3
source 'https://rubygems.org/'

gem 'rails', '6.1'

References

[1] RubyGems 镜像 - Ruby China - https://gems.ruby-china.com/

[2] RubyGems.org | your community gem host - https://rubygems.org/

[3] Run your own gem server - RubyGems Guides - https://guides.rubygems.org/run-your-own-gem-server/

[4] Index of /rubygems/ | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror - https://mirrors.tuna.tsinghua.edu.cn/rubygems/

[5] GEM ENVIRONMENT | Command Reference - RubyGems Guides - https://guides.rubygems.org/command-reference/#gem-environment

[6] Project specific .gemrc files using the GEMRC environment variable | Jordan Elver | Ruby on Rails Developer, Bristol, UK - https://jordanelver.co.uk/blog/2020/12/06/project-specific-gemrc-files-using-the-gemrc-environment-variable/

[7] gemrc example - https://gist.github.com/jch/1331576

[8] [class Gem::ConfigFile - Documentation for Ruby 3.0.0 - https://docs.ruby-lang.org/en/3.0.0/Gem/ConfigFile.html(https://docs.ruby-lang.org/en/3.0.0/Gem/ConfigFile.html)