9.安装rbenv和ruby-build的步骤(Ubuntu)

1.安装依赖

sudo apt-get update
sudo apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm6 libgdbm-dev libdb-dev git

2.安装rbenv

安装rbenv

git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

升级rbenv

cd ~/.rbenv
git pull

卸载rbenv

从您的shell配置文件PATH环境变量删除rbenv init代码行,然后运行以下命令

sudo rm -rf `rbenv root`

常用命令

#安装ruby
rbenv install --list         # 列出所有网络可下载安装的 ruby 版本
rbenv install 3.0.0          # 安装ruby3.0.0

#列出版本
rbenv versions               # 列出系统已安装的版本
rbenv version                # 列出系统正在使用的版本

#设置版本
rbenv global 3.0.0           # 默认使用 ruby3.0.0
rbenv local 3.0.0            # 当前目录使用 ruby3.0.0, 会生成一个 `.rbenv-version` 文件

#卸载
rbenv uninstall 3.0.0        #卸载ruby3.0.0

3.安装ruby-build

安装ruby-build

git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
echo 'export PATH="$(rbenv root)/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

升级ruby-build

git -C "$(rbenv root)"/plugins/ruby-build pull

使用以下rbenv-doctor脚本验证是否正确设置了 rbenv

curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash

#示例
Checking for `rbenv' in PATH: /usr/local/bin/rbenv
Checking for rbenv shims in PATH: OK
Checking `rbenv install' support: /usr/local/bin/rbenv-install (ruby-build 20170523)
Counting installed Ruby versions: none
  There aren't any Ruby versions installed under `~/.rbenv/versions'.
  You can install Ruby versions like so: rbenv install 2.2.4
Checking RubyGems settings: OK
Auditing installed plugins: OK

如果遇到:curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused的解决方案

4.安装ruby

下载ruby-china镜像中的ruby版本到cache文件夹

wget https://cache.ruby-china.com/pub/ruby/3.0/ruby-3.0.0.tar.gz  -P $(rbenv root)/cache
rbenv install 3.0.0          # 安装ruby3.0.0
rbenv global 3.0.0           # 默认使用 ruby3.0.0
ruby -v
#此处会非常慢,请耐心等待,除非翻q
gem update --system --no-document 
#查看版本号看是否升级成功
gem -v   
#更改ruby-china gem源               
gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/  
#查看源列表,确保只有 gems.ruby-china.com
gem sources -l           
#安装bundler依赖包工具
gem install bundler --no-document

原创文章,作者:huoxiaoqiang,如若转载,请注明出处:https://www.huoxiaoqiang.com/ruby/rvm/3166.html

发表评论

邮箱地址不会被公开。 必填项已用*标注