Centos上安装ruby,rbenv

利用rbenv 在centos上安装ruby
更新于: 2022-03-28 12:34:44

安装必备的包

yum install -y git-core zlib zlib-devel gcc-c++ patch readline readline-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison curl sqlite-devel
cd
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
# 用来编译安装 ruby
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
# 用来管理 gemset, 可选, 因为有 bundler 也没什么必要
git clone https://github.com/jamis/rbenv-gemset.git  ~/.rbenv/plugins/rbenv-gemset
# 通过 rbenv update 命令来更新 rbenv 以及所有插件, 推荐
git clone https://github.com/rkh/rbenv-update.git ~/.rbenv/plugins/rbenv-update
# 使用 Ruby China 的镜像安装 Ruby, 国内用户推荐
git clone https://github.com/AndorChen/rbenv-china-mirror.git ~/.rbenv/plugins/rbenv-china-mirror

添加到 .bashrc

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
export RUBY_BUILD_MIRROR_URL=https://cache.ruby-china.com

从 cache中安装

## prepare package
mkdir -p ~/.rbenv/cache
cd ~/.rbenv/cache
wget https://cache.ruby-china.com/pub/ruby/2.6/ruby-2.6.5.tar.bz2

rbenv常用命令

功能描述
列出所有可用版本
rbenv install -L
安装某个版本
# 正常安装
rbenv install 2.6.5
# 带 verbose 的信息
rbenv install 2.6.5 -v
rehash
rbenv rehash
使刚安装的版本生效
rbenv global 2.6.5

安装报错

openssl_missing.h:75:0: warning: "X509_STORE_get_ex_new_index" redefined 
openssl 报错截图

openssl 相关

# 获得dir相关信息
openssl version -d
# 版本
openssl version

可能的修复方案

export RUBY_CONFIGURE_OPTS="--with-openssl-dir=/usr/local/openssl"
rbenv install 2.6.5
[root@ecs-eqyaP openssl]# export RUBY_CONFIGURE_OPTS="--with-openssl-dir=/usr/local/openssl"
[root@ecs-eqyaP openssl]# rbenv install 2.6.5
Installing ruby-2.6.5...
Installed ruby-2.6.5 to /root/.rbenv/versions/2.6.5

安装gem报错

ERROR:  SSL verification error at depth 1: unable to get local issuer certificate (20)
ERROR:  You must add /OU=GlobalSign Root CA - R3/O=GlobalSign/CN=GlobalSign to your local trusted store
ERROR:  SSL verification error at depth 1: unable to get local issuer certificate (20)
ERROR:  You must add /OU=GlobalSign Root CA - R3/O=GlobalSign/CN=GlobalSign to your local trusted store
ERROR:  SSL verification error at depth 1: unable to get local issuer certificate (20)
ERROR:  You must add /OU=GlobalSign Root CA - R3/O=GlobalSign/CN=GlobalSign to your local trusted store
ERROR:  Could not find a valid gem 'whenever' (>= 0), here is why:
          Unable to download data from https://rubygems.org/ - SSL_connect returned=1 errno=0 state=error: certificate verify failed (unable to get local issuer certificate) (https://rubygems.org/specs.4.8.gz)
ERROR:  SSL verification error at depth 1: unable to get local issuer certificate (20)
ERROR:  You must add /OU=GlobalSign Root CA - R3/O=GlobalSign/CN=GlobalSign to your local trusted store
ERROR:  SSL verification error at depth 1: unable to get local issuer certificate (20)
ERROR:  You must add /OU=GlobalSign Root CA - R3/O=GlobalSign/CN=GlobalSign to your local trusted store
# 到这里 https://rubygems.org/pages/download 下载最新的版本即可
cd /tmp
wget https://rubygems.org/rubygems/rubygems-3.3.10.tgz
tar zxf rubygems-3.3.10.tgz
cd rubygems-3.3.10
ruby setup.rb

参考