centos7使用rvm安装ruby

原文出处:https://dandelioncloud.cn/article/details/1528578221985775618

首先安装rvm安装会使用的包:

yum install gcc-c++ patch readline readline-devel zlib zlib-devel \
   libyaml-devel libffi-devel openssl-devel make \
   bzip2 autoconf automake libtool bison iconv-devel sqlite-devel

之后便是安装rvm:

curl -sSL https://rvm.io/mpapis.asc | gpg --import -          (三选一)
curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -        (三选一)
curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -    (三选一)
curl -L get.rvm.io | bash -s stable

配置rvm的运行环境

source /etc/profile.d/rvm.sh
rvm reload

输入一下命令检查安装情况

rvm requirements run

将显示:

Checking requirements for centos.
Requirements installation successful.

最后便可安装ruby了,当然版本可以任选,这里是2.4.4

rvm install 2.4.4

检查安装情况

rvm list

显示如下信息则 安装完成:

rvm rubies
=* ruby-2.4.4 [ x86_64 ]
# => - current
# =* - current && default
#  * - default

设置默认运行的ruby版本

rvm use 2.4.2 --default

卸载ruby

rvm uninstall 2.4.2
或rvm remove 2.4.2

安装或卸载 rails

gem install rails
gem install rails -v 版本号
gem uninstall -v 版本号

安装rails会出现一个问题

ERROR:  Could not find a valid gem 'rails' (>= 0), here is why:
      Unable to download data from https://rubygems.org/ - no such name (https://rubygems.org/specs.4.8.gz)

解决

#等待时间有点长
gem update --system

换镜像源

gem sources --remove https://rubygems.org/
gem sources -a https://gems.ruby-china.com/  
gem sources -a https://ruby.taobao.org/ # 换成淘宝镜像源
gem sources -l
换源会出现 openssl证书过期问题:解决:yum install openssl openssl-devel,yum update

再次尝试安装rails

gem install rails

成功!

新建rails项目

#进入某个文件夹
rails new demo1
#进入demo1目录,运行
rails s

出现问题

cannot load such file -- rails/html/sanitizer/version (LoadError)

解决

#原因是缺少相关依赖
gem install 'rails-html-sanitizer'

又出现问题

Webpacker configuration file not found /home/ruby_work/rails_test/demo2/config/webpacker.yml. Please run rails webpacker:install Error: No such file or directory @ realpath_rec - /home/ruby_work/rails_test/demo2/config/webpacker.yml (RuntimeError)

解决

#原因是缺少webpacker依赖(这个问题其实可以不解决,不去安装node和yarn,只需要将Gemfile中的webpacker的gen依赖去掉)
rails webpacker:install

#但又会提示
sh: node: 未找到命令
sh: nodejs: 未找到命令
Node.js not installed. Please download and install Node.js https://nodejs.org/en/download/
Exiting!
#原因是缺少Node.js
参考:https://blog.csdn.net/weixin_39709920/article/details/123063035,需要下载较新版本,如v16
wget https://nodejs.org/dist/latest-v16.x/node-v16.17.0-linux-x64.tar.gz
tar -zxvf node-v16.17.0-linux-x64.tar.gz -C /opt/
nodejs环境变量设置,node -v查看安装成功

#下载完Node.js,又发现要安装 yarn;而安装yarn需先安装node
参考:https://blog.csdn.net/ling1998/article/details/126799711
curl -sL https://dl.yarnpkg.com/rpm/yarn.repo -o /etc/yum.repos.d/yarn.repo
yum install -y yarn
yarn -v
#下载完yarn再执行rails webpacker:install就通过了

再执行rails s

又有 #<LoadError: cannot load such file -- rails/dom/testing/assertions>问题
解决:gem install rails-dom-testing

最终运行 rails s -b 0.0.0.0 -p 3000终于成功!

Ubuntu:http://www.taodudu.cc/news/show-1217506.html

posted @ 2022-09-14 10:42  让时间变成力量  阅读(420)  评论(0)    收藏  举报