分享最新优惠信息
购买主机更加划算

Ubuntu 20.04/18.04系统安装Ruby两种方法

Ruby是一种动态的、开源的、面向对象的脚本语言,其创建时考虑到了简单性和易用性。在本文中,小编将介绍如何在Ubuntu 20.04/18.04系统中上安装Ruby语言。

在 Ubuntu 20.04/18.04系统中安装Ruby的最简单方法是从PPA存储库进行安装。为此,本文将使用Brightbox PPA存储库,毕竟Brightbox多年来一直为Ubuntu提供优化的Ruby包。此外,还将介绍另外一种方法,即使用 RVM安装Ruby。

Ruby语言

方法 1:从PPA存储库上安装Ruby

个人比较喜欢Brightbox PPA存储库,因为它可以灵活地安装和从包管理器更新的任何Ruby版本,可以使用以下命令将PPA存储库添加到Ubuntu 20.04/18.04 系统:

sudo apt update
sudo apt -y install software-properties-common
sudo apt-add-repository ppa:brightbox/ruby-ng

接下来点击<ENTER>键以便安装PPA库相关组件:

Ubuntu packages for Ruby 2.7, 2.6, 2.5, 2.4, 2.3, 2.2, 2.1, 2.0 1.9.3 and 1.8.

See https://www.brightbox.com/docs/ruby/ubuntu/ for more details.

Discussion list here:

https://groups.google.com/forum/#!forum/brightbox-ruby-ubuntu-packaging

https://www.brightbox.com
 More info: https://launchpad.net/~brightbox/+archive/ubuntu/ruby-ng
Press [ENTER] to continue or Ctrl-c to cancel adding it.

Hit:1 http://security.ubuntu.com/ubuntu focal-security InRelease
Get:2 http://mirrors.DigitalOcean.com/ubuntu focal InRelease [265 kB]
Get:3 http://ppa.launchpad.net/brightbox/ruby-ng/ubuntu focal InRelease [23.8 kB]
Hit:4 http://mirrors.digitalocean.com/ubuntu focal-updates InRelease
Hit:5 http://mirrors.digitalocean.com/ubuntu focal-backports InRelease
Get:6 http://ppa.launchpad.net/brightbox/ruby-ng/ubuntu focal/main amd64 Packages [4508 B]
Get:7 http://ppa.launchpad.net/brightbox/ruby-ng/ubuntu focal/main Translation-en [2568 B]
Fetched 296 kB in 1s (331 kB/s)
Reading package lists... Done

添加repo后,更新包列表:

sudo apt update

然后安装想要Ruby版本,本文以安装Ruby 2.7为例:

$ sudo apt install ruby2.7
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  fonts-lato javascript-common libjs-jquery libruby2.7 rake ruby-minitest ruby-net-telnet ruby-power-assert ruby-test-unit ruby-xmlrpc rubygems-integration
  unzip zip
Suggested packages:
  apache2 | lighttpd | httpd bundler
The following NEW packages will be installed:
  fonts-lato javascript-common libjs-jquery libruby2.7 rake ruby-minitest ruby-net-telnet ruby-power-assert ruby-test-unit ruby-xmlrpc ruby2.7
  rubygems-integration unzip zip
0 upgraded, 14 newly installed, 0 to remove and 47 not upgraded.
Need to get 7853 kB of archives.
After this operation, 34.5 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

在Brightbox维护的Ruby包中- 2.7, 2.6, 2.5, 2.4, 2.3, 2.2, 2.1, 2.0 1.9.3 and 1.8. 例如安装 Ruby 2.6的示例,使用:

sudo apt install ruby2.6

安装后,检查版本:

$ ruby --version
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux-gnu]

方法 2:使用RVM安装 Ruby

此外,还可以使用RVM安装Ruby。RVM 是一个命令行工具,允许用户轻松安装、管理和使用从解释器到gems的多个ruby​​环境。

导入RVM GPG密钥:

sudo gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

然后通过运行以下命令在Ubuntu 20.04/18.04上安装RVM:

curl -L get.rvm.io | sudo bash -s stable
source /etc/profile.d/rvm.sh

最后检查版本:

$ rvm --version
rvm 1.29.12 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]

通过运行以下命令安装依赖项:

# rvm requirements run
Checking requirements for ubuntu.
Installing requirements for ubuntu.
Updating system..
Installing required packages: g++, gcc, autoconf, automake, bison, libc6-dev, libffi-dev, libgdbm-dev, libncurses5-dev, libsqlite3-dev, libtool, libyaml-dev, make, pkg-config, sqlite3, zlib1g-dev, libgmp-dev, libreadline-dev, libssl-dev...............................
Requirements installation successful.

像下面这样安装Ruby,可以检查Ruby 版本以获取版本号。

# rvm install 3.0.1
Searching for binary rubies, this might take some time.
Found remote file https://rubies.travis-ci.org/ubuntu/20.04/x86_64/ruby-3.0.1.tar.bz2
Checking requirements for ubuntu.
Requirements installation successful.
ruby-3.0.1 - #configure
ruby-3.0.1 - #download
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 26.7M  100 26.7M    0     0  18.9M      0  0:00:01  0:00:01 --:--:-- 85.1M
No checksum for downloaded archive, recording checksum in user configuration.
ruby-3.0.1 - #validate archive
ruby-3.0.1 - #extract
ruby-3.0.1 - #validate binary
ruby-3.0.1 - #setup
ruby-3.0.1 - #gemset created /usr/local/rvm/gems/ruby-3.0.1@global
ruby-3.0.1 - #importing gemset /usr/local/rvm/gemsets/global.gems..................................
ruby-3.0.1 - #generating global wrappers........
ruby-3.0.1 - #gemset created /usr/local/rvm/gems/ruby-3.0.1
ruby-3.0.1 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list
ruby-3.0.1 - #generating default wrappers........

要列出可用的Ruby解释器,请使用以下命令:

# rvm list known

确认Ruby版本:

# ruby --version
ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [x86_64-linux]

总结

以上就是在Ubuntu 20.04/18.04 系统上安装了Ruby的两种方法,安装方法仅供参考,仅适用于Ubuntu系统。当然,还有其它安装Ruby的方法,但本文介绍的两种方法相对比较简单。

众所周知,在租用Linux服务器的时候,一般都需要自己安装Ruby环境,如果不清楚怎么操作可以按照上述过程进行安装。

未经允许不得转载:惠主机 » Ubuntu 20.04/18.04系统安装Ruby两种方法

相关文章