如何在Ubuntu 18.04 LTS上安装Ruby

时间:2020-02-23 14:32:50  来源:igfitidea点击:

在本教程中,我们将研究如何在Ubuntu 18.04 LTS上安装Ruby。
Ruby是一种动态,开源,面向对象的脚本语言,以简单和生产力创造。

在Ubuntu 18.04上安装Ruby的最简单方法来自PPA存储库。
为此,我们将使用BrightBox PPA存储库。
BrightBox一直为Ubuntu提供优化的Ruby软件包。

从PPA存储库安装Ruby

我喜欢BrightBox PPA存储库,因为它使我们可以灵活地安装他们维护的任何版本的Ruby。

使用命令将PPA存储库添加到Ubuntu 18.04系统中:

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

添加回收后,更新软件包列表

sudo apt update

然后安装我们想要的Ruby版本。
对我来说,我正在安装Ruby 2.5

$sudo apt install ruby2.5
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-headers-4.15.0-29 linux-headers-4.15.0-29-generic linux-image-4.15.0-29-generic linux-modules-4.15.0-29-generic
  linux-modules-extra-4.15.0-29-generic
Use 'sudo apt autoremove' to remove them.
The following packages will be upgraded:
  ruby2.5
1 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.
Need to get 56.6 kB of archives.
After this operation, 2,048 B of additional disk space will be used.
Get:1 http://ppa.launchpad.net/brightbox/ruby-ng/ubuntu bionic/main amd64 ruby2.5 amd64 2.5.3-1bbox1~bionic1 [56.6 kB]
Fetched 56.6 kB in 1s (66.2 kB/s)  
(Reading database ... 141513 files and directories currently installed.)
Preparing to unpack .../ruby2.5_2.5.3-1bbox1~bionic1_amd64.deb ...
Unpacking ruby2.5 (2.5.3-1bbox1~bionic1) over (2.5.1-1ubuntu1.1) ...
Setting up ruby2.5 (2.5.3-1bbox1~bionic1) ...
update-alternatives: using /usr/bin/gem2.5 to provide /usr/bin/gem (gem) in auto mode
update-alternatives: using /usr/bin/ruby2.5 to provide /usr/bin/ruby (ruby) in auto mode
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...

由BrightBox维护的Ruby包中 - 2.4, 2.3, 2.2, 2.1, 2.0, 1.9.3, and 1.8.7
安装Ruby 2.4的示例,使用:

sudo apt install ruby2.4

安装后,检查版本:

$ruby --version
ruby 2.5.1p57 (2016-03-29 revision 63029) [x86_64-linux-gnu]

使用RVM安装Ruby

我们还可以使用RVM安装Ruby。
RVM是一个命令行工具,它允许我们轻松安装,管理和工作,并使用来自口译员到Gems的多个Ruby环境。

导入RVM GPG密钥:

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

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

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

我们可以检查版本:

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

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

rvm requirements run

安装Ruby如下:

# rvm install 2.5.1
Warning, new version of rvm available '1.29.7', you are using older version '1.29.6'.
You can disable this warning with:    echo rvm_autoupdate_flag=0 >> ~/.rvmrc
You can enable  auto-update  with:    echo rvm_autoupdate_flag=2 >> ~/.rvmrc
Searching for binary rubies, this might take some time.
Found remote file https://rvm_io.global.ssl.fastly.net/binaries/ubuntu/18.04/x86_64/ruby-2.5.1.tar.bz2
Checking requirements for ubuntu.
Requirements installation successful.
ruby-2.5.1 - #configure
ruby-2.5.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     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--    100 17.8M  100 17.8M    0     0  1059k      0  0:00:17  0:00:17 --:--:-- 1303k
ruby-2.5.1 - #validate archive
ruby-2.5.1 - #extract
ruby-2.5.1 - #validate binary
ruby-2.5.1 - #setup
ruby-2.5.1 - #gemset created /usr/local/rvm/gems/Hyman@theitroad
ruby-2.5.1 - #importing gemset /usr/local/rvm/gemsets/global.gems..................................
ruby-2.5.1 - #generating global wrappers.......
ruby-2.5.1 - #gemset created /usr/local/rvm/gems/ruby-2.5.1
ruby-2.5.1 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.5.1 - #generating default wrappers.......

要列出可用的Ruby解释器,请使用:

rvm list known

我们现在在Ubuntu 18.04系统上安装了Ruby。