Centos7上进行Ruby 2.7.3安装

  1. 创建用户,不以root身份安装。
# 添加用户,执行完以下命令自动创建目录:/home/ruby
adduser ruby
# 修改sudo文件权限
chmod -v u+w /etc/sudoers
# 允许ruby执行sudo
vi /etc/sudoers
# 找到如下内容,在root之后添加刚创建的用户,NOPASSWD:如果去掉,ruby用户在sudo时需要密码
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
ruby    ALL=(ALL)       NOPASSWD:ALL
# 收回sudoers写权限
chmod -v u-w /etc/sudoers
  1. 安装必要的系统依赖。
yum install -y bzip2 openssl-devel
  1. 安装rbenv,用于安装管理不同版本的ruby
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
cd ~/.rbenv && src/configure && make -C src
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
~/.rbenv/bin/rbenv init
source ~/.bash_profile
  • 执行情况如下:
[ruby@xxx ~]$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
Cloning into '/home/ruby/.rbenv'...
remote: Enumerating objects: 2886, done.
remote: Total 2886 (delta 0), reused 0 (delta 0), pack-reused 2886
Receiving objects: 100% (2886/2886), 562.54 KiB | 8.00 KiB/s, done.
Resolving deltas: 100% (1801/1801), done.
[ruby@xxx ~]$ cd ~/.rbenv && src/configure && make -C src
make: Entering directory `/home/ruby/.rbenv/src'
gcc -fPIC     -c -o realpath.o realpath.c
gcc -shared -Wl,-soname,../libexec/rbenv-realpath.dylib  -o ../libexec/rbenv-realpath.dylib realpath.o 
make: Leaving directory `/home/ruby/.rbenv/src'
[ruby@xxx .rbenv]$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
[ruby@xxx .rbenv]$ ~/.rbenv/bin/rbenv init
# Load rbenv automatically by appending
# the following to ~/.bash_profile:

eval "$(rbenv init -)"

[ruby@xxx .rbenv]$ source ~/.bash_profile
[ruby@xxx .rbenv]$
  1. 安装ruby-build插件,用于安装不同版本的ruby
mkdir -p "$(rbenv root)"/plugins
git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
  • 执行情况如下:
[ruby@xxx .rbenv]$ mkdir -p "$(rbenv root)"/plugins
[ruby@xxx .rbenv]$ git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
Cloning into '/home/ruby/.rbenv/plugins/ruby-build'...
remote: Enumerating objects: 11536, done.
remote: Counting objects: 100% (229/229), done.
remote: Compressing objects: 100% (113/113), done.
remote: Total 11536 (delta 174), reused 151 (delta 105), pack-reused 11307
Receiving objects: 100% (11536/11536), 2.43 MiB | 6.00 KiB/s, done.
Resolving deltas: 100% (7641/7641), done.
[ruby@xxx .rbenv]$ 
  1. 下载安装ruby,下载和安装都会比较慢,耐心等待。
rbenv install 2.7.3
  • 执行情况如下:
[ruby@xxx .rbenv]$ rbenv install 2.7.3
Downloading ruby-2.7.3.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.3.tar.bz2
Installing ruby-2.7.3...
Installed ruby-2.7.3 to /home/ruby/.rbenv/versions/2.7.3

[ruby@xxx .rbenv]$ 
  1. 切换/生效相应的ruby版本
rbenv local 2.7.3  #rbenv global 2.7.3  使用global可能切换不成功
rbenv rehash
  1. 安装bundler
# 如果报错:bash: gem: command not found,说明第6步切换未生效
gem install bundler

其他问题及解决:

Error installing bundler:bundler requires Ruby version >= 2.3.0.
若第6步按local的执行应该不会出现上述错误。
如果第6步是按global执行的,建议改为local重新跑第6步的两条命令再看看是否成功。

实在不行再按以下方式操作:
1. sudo cp /usr/bin/ruby /usr/bin/ruby.bak  备份ruby
2. sudo cp versions/2.7.3/bin/ruby /usr/bin/ 覆盖ruby
再执行gem install bundler成功。

You may also like...

发表回复

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

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据