无管理员权限安装Ruby on Rails及其他gems的方法
Absolutely! You totally can install Rails and all your gems to a custom directory you have full write access to, and set things up so the commands work just like a system-wide install. Since you already have Homebrew, I’ll walk you through two solid approaches—one that tweaks your gem paths directly, and a better long-term solution using a Ruby version manager (which avoids headaches with that ancient system Ruby 2.0.0).
Method 1: Directly Customize Gem Installation Paths
This works if you want to stick with the system Ruby, but keep in mind: Ruby 2.0.0 is extremely outdated and won’t support most modern Rails versions. Use this only if you’re working with legacy projects.
Create your custom gem folders in your home directory (this structure keeps things organized):
mkdir -p ~/.ruby-gems/{bin,gems,specs}Update your shell config to tell
gemand your system where to look for these files. Depending on your shell, edit either~/.bash_profile,~/.bashrc, or~/.zshrc:# Add these lines at the end of the file export GEM_HOME="$HOME/.ruby-gems/gems" export GEM_PATH="$HOME/.ruby-gems/gems" export PATH="$HOME/.ruby-gems/bin:$PATH"Reload your shell to apply the changes right away:
# For Bash users source ~/.bash_profile # For Zsh users source ~/.zshrcInstall Rails—
gemwill now install everything to your custom folder instead of the restricted system directory:gem install railsVerify it works by checking the Rails version:
rails --version
Method 2: Use rbenv for Isolated Ruby Versions (Recommended)
The system Ruby is way too old for modern Rails development. rbenv is a lightweight tool that lets you install recent Ruby versions entirely in your home folder (no admin rights needed) and manage multiple versions for different projects. Since you have Homebrew, this is the easiest and most maintainable option:
Install rbenv via Homebrew:
brew install rbenvInitialize rbenv to integrate it with your shell. Run this command and follow the on-screen instructions to add the required line to your shell config file:
rbenv initReload your shell to activate rbenv:
# For Bash source ~/.bash_profile # For Zsh source ~/.zshrcInstall a modern Ruby version (run
rbenv install --listto see all available stable releases—3.2.2 is a great choice right now):rbenv install 3.2.2Set this Ruby version as your global default:
rbenv global 3.2.2Install Rails—it will be tied to this Ruby version and installed in your home folder:
gem install railsRefresh rbenv's command shims so your system recognizes the new
railscommand:rbenv rehashVerify everything is working:
ruby --version # Should show 3.2.2 (or the version you installed) rails --version # Should show the latest Rails version you installed
Why This Is Better
Using a version manager like rbenv isolates your Ruby environment from the system one, so you’ll never have to fight permission errors again. It also lets you switch between Ruby versions for different projects if you need to—super handy for legacy work!
内容的提问来源于stack exchange,提问作者tposs

