当前位置: 首页 > 工具软件 > Ruby > 使用案例 >

Ubuntu 20.04下使用rvm安裝Ruby

刁越
2023-12-01

前言

本篇基於How to Install Redmine on Ubuntu 16.04,並記錄在實際操作過程中碰到的問題及解決方式。

安裝rvm

使用如下指令安裝rvm:

sudo apt-add-repository -y ppa:rael-gc/rvm
sudo apt update -y
sudo apt -y install rvm

設定開機自啟動:

#echo "source /usr/share/rvm/scripts/rvm" | sudo tee -a /etc/profile
echo "source /usr/share/rvm/scripts/rvm" | sudo tee -a ~/.bashrc
source /usr/share/rvm/scripts/rvm

教學中原來是將echo "source /usr/share/rvm/scripts/rvm"寫入/etc/profile。但是實際試了之後,發現重開機後/etc/profile並不會自動被執行(?),所以這裡改為寫入~/.bashrc

安裝ruby

首先啟用rvm

source /usr/share/rvm/scripts/rvm

查看可用rvm安裝的ruby版本:

rvm list known

輸出如下:

Warning, new version of rvm available '1.29.12', you are using older version '1.29.10'.
You can disable this warning with:   echo rvm_autoupdate_flag=0 >> ~/.rvmrc
You can enable auto-update with:     echo rvm_autoupdate_flag=2 >> ~/.rvmrc
You can update manually with:        rvm get VERSION                         (e.g. 'rvm get stable')

# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.10]
[ruby-]2.3[.8]
[ruby-]2.4[.9]
[ruby-]2.5[.7]
[ruby-]2.6[.5]
[ruby-]2.7[.0]
ruby-head

...

選好想要的ruby版本後,開始安裝:

rvm install ruby-2.6

註:因為Redmine支援的ruby版本小於2.7.0,所以此處安裝的是ruby-2.6

設定默認使用的ruby版本:

rvm --default use ruby-2.6

安裝完成後檢查ruby版本:

ruby -v
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]

目前rvm list不會出現3.1版的ruby,但是參考What’s the right string to use when installing Ruby 3.1 through RVM on Mac OS Big Sur?,仍然可以透過以下指令安裝:

rvm install ruby-3.1.0

安裝bundler

安裝bundler

gem install bundler

檢查bundler版本有以下兩種方式:

gem list | grep "bundler"

輸出:

bundler (default: 2.2.16)
bundler-unload (1.0.2)
rubygems-bundler (1.4.5)

或使用:

bundle -v

輸出:

Your Gemfile lists the gem redmine_crm (>= 0) more than once.
You should probably keep only one of them.
Remove any duplicate entries and specify the gem only once.
While it's not a problem now, it could cause errors if you change the version of one of them later.
Bundler version 2.2.16

如果想要卸載並重新安裝bundler

gem uninstall bundler
gem install bundler -v '1.17.3' --default # revise 1.17.3 to any version you want

Troubleshooting

ruby 2.7 w/ Redmine

以下為安裝ruby 2.7.2.版後進入redmine目錄後所出現的警告:

Your Ruby version is 2.7.2, but your Gemfile specified >= 2.3.0, < 2.7.0

參考How to fix “Your Ruby version is 2.3.0, but your Gemfile specified 2.2.5” while server starting,解決辦法是改為安裝舊版的ruby:

rvm install ruby-2.6
rvm use ruby-2.6

環境中有多個使用者

如果環境中有多個使用者,在rvm install ruby-xxx時可能會碰到權限問題,參考Rvm ruby Permission denied,使用如下指令解決:

rvm fix-permissions system # and then input your password
rvm fix-permissions user

source /usr/share/rvm/scripts/rvm

這一句重開機後要再執行一遍:

source /usr/share/rvm/scripts/rvm

如果沒執行,系統就會用到非rvm安裝的ruby:

which ruby # before
#/usr/bin/ruby
which ruby # after
#/usr/share/rvm/rubies/ruby-2.6.6/bin/ruby

RVM is not a function, selecting rubies with ‘rvm use …’ will not work.

如果在使用rvm use xxx時出現以下錯誤:

RVM is not a function, selecting rubies with 'rvm use ...' will not work.

You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for an example.

參考RVM is not a function, selecting rubies with ‘rvm use …’ will not work.,使用以下指令解決:

source /usr/share/rvm/scripts/rvm

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

在下bundle install時如果出現以下錯誤:

Installing nokogiri 1.10.10 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /home/redmine/redmine/vendor/bundle/ruby/2.7.0/gems/nokogiri-1.10.10/ext/nokogiri
/usr/bin/ruby2.7 -I /usr/lib/ruby/2.7.0 -r ./siteconf20201231-1909-1sf5ayj.rb extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/include/ruby.h

You might have to install separate package for the ruby development
environment, ruby-dev or ruby-devel for example.

extconf failed, exit code 1

Gem files will remain installed in /home/redmine/redmine/vendor/bundle/ruby/2.7.0/gems/nokogiri-1.10.10 for
inspection.
Results logged to
/home/redmine/redmine/vendor/bundle/ruby/2.7.0/extensions/x86_64-linux/2.7.0/nokogiri-1.10.10/gem_make.out

An error occurred while installing nokogiri (1.10.10), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.10.10' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
  rails was resolved to 5.2.4.2, which depends on
    actioncable was resolved to 5.2.4.2, which depends on
      actionpack was resolved to 5.2.4.2, which depends on
        actionview was resolved to 5.2.4.2, which depends on
          rails-dom-testing was resolved to 2.0.3, which depends on
            nokogiri

可使用以下指令解決:

source /usr/share/rvm/scripts/rvm
 类似资料: