我正在通过不同的书籍学习 rails,这些书籍使用了不同的 ruby 和 rails 版本。目前,我在 Mac OS X Snow Leopard (in /usr/bin
) 上安装了 ruby 1.87,但还需要为不同的 rails 应用程序使用 ruby 1.9。
有人能告诉我如何实现这个功能吗?我对此很陌生,因此如果能提供尽可能多的说明我将不胜感激。
答案1
目前有两种主要的 Ruby 版本管理器可供你选择:
这些允许您在同一系统上保留多个版本的 Ruby。一旦您安装了版本管理器并安装了您自己的 Ruby 版本,您就不会再弄乱系统的 Ruby 及其 Gems,这是最大的好处。再也不会有sudo
权限错误和 Gem 冲突了。
我应该选择哪一个?
两者的作用相同,但遵循不同的理念。选择权在您手中。
我个人推荐rbenv
它,因为它很简单。我已经用了好几年了,效果一直很好。
我该如何安装它们?
如果您选择rbenv
:
- 跟着所有安装和设置说明。
- 安装
ruby-build
- 运行版本
rbenv install x.x.x
在哪里(用于查看哪些可用x.x.x
rbenv install --list
- 运行
rbenv global x.x.x
以更改您的全局 Ruby 版本
如果您选择 RVM:
答案2
答案3
假设你已经安装了 rbenv ruby 版本:运行,
rbenv init
然后;
eval "$(rbenv init - zsh)"
要确认切换的版本,请运行;
which ruby
它应该是这样的;
/Users/MacbookAir/.rbenv/shims/ruby
答案4
这对我有用,我没有 sudo:
#!/usr/bin/env bash
ruby -v
if ! command -v ruby &> /dev/null
then
echo "Going to try to install ruby (ideally 3.1.2)"
# - install rebenv (following ruby-build really is needed eventhough it doesn't look like it)
mkdir -p ~/.rbenv
cd ~/.rbenv
git clone https://github.com/rbenv/rbenv.git .
# if $HOME/.rbenv/bin not in path append it, otherwise don't change it
echo $PATH | tr ':' '\n' | awk '{print " " $0}';
if [[ ":$PATH:" != *":$HOME/.rbenv/bin:"* ]]; then
echo "might want to put $HOME/.rbenv/bin in your path"
export PATH="$HOME/.rbenv/bin:$PATH"
# echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc.lfs
fi
eval "$(rbenv init -)"
rbenv -v
# - install ruby-build, odd, this really is needed for ruby to install despite it not looking like ruby build is need at the bottom
mkdir -p ~/.ruby-build
cd ~/.ruby-build
git clone https://github.com/rbenv/ruby-build.git .
# if $HOME/.ruby-build/bin not in path append it, otherwise don't change it
echo $PATH | tr ':' '\n' | awk '{print " " $0}';
if [[ $PATH != *"$HOME/.ruby-build/bin"* ]]; then
echo "might want to put $HOME/.ruby-build/bin in your path"
export PATH="$HOME/.ruby-build/bin:$PATH"
# echo 'export PATH="$HOME/.ruby-build/bin:$PATH"' >> ~/.bashrc.lfs
fi
ruby-build --version
# - install ruby without sudo -- using rbenv
mkdir -p ~/.local
# ruby-build 3.1.2 ~/.local/
rbenv install 3.1.2
rbenv global 3.1.2
fi
ruby -v
# - wontfix: above worked but what was ruby's official way to do this? doesn't matter but answer might be here some day: https://discuss.rubyonrails.org/t/what-is-the-official-way-to-install-ruby-ideally-3-1-2-on-ubuntu/82226
# -old prover bot ignore doesn't work on SNAP
# Proverbot's way to install ruby
# # First, install Ruby, as that is for some reason required to build the "system" project
# git clone https://github.com/rbenv/ruby-build.git ~/ruby-build
# mkdir -p ~/.local
# PREFIX=~/.local ./ruby-build/install.sh
# ~/.local/ruby-build 3.1.2 ~/.local/
# ref: https://superuser.com/questions/340490/how-to-install-and-use-different-versions-of-ruby/1756372#1756372
请参阅此处以获得更一般的答案:https://askubuntu.com/questions/339/how-can-i-install-a-package-without-root-access