在Centos和Ubuntu的Rails上安装Ruby

时间:2020-03-21 11:45:46  来源:igfitidea点击:

Ruby在Rails上是一个免费的开源Web应用程序框架,允许我们更容易且快速地构建Web应用程序。
有数百万个流行的应用程序,如Basecamp,Github,Shopify,Airbnb,抽搐,SoundCloud,Hulu,Zendesk,Square,Highris,以及许多人在Rails上使用Ruby建造。
在本教程中,我们将看到如何在CentOS 7和Ubuntu 18.04 LTS服务器上安装在Rails上安装Ruby。
此外,下面给出的安装步骤应在其他基于DEB和RPM的分布上工作。

在Rails上安装Ruby

准备工作

使用命令更新和升级Ubuntu系统:

$sudo apt-get update
$sudo apt-get upgrade

在CentOS上,运行:

$sudo yum update

然后安装Ruby环境管理器(不久RVM)。
它是一个命令行工具,用于轻松安装,管理和处理多个Ruby环境。

我们需要下载并添加RVM公钥。
为此,请先安装curl包。

在CentOS上:

$sudo yum install curl

在Ubuntu上:

$sudo apt-get install curl

现在,逐个运行以下命令以下载并添加RVM公钥:

$curl -#LO https://rvm.io/mpapis.asc
$gpg --import mpapis.asc

使用命令下载RVM Installer脚本:

$curl -sSL https://get.rvm.io | bash -s stable

示例输出:

Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc
gpg: Signature made Monday 11 September 2016 02:29:21 AM IST
gpg: using RSA key E206C29FBF04FF17
gpg: Good signature from "Michal Papis (RVM signing) <Hyman@theitroad>" [unknown]
gpg: aka "Michal Papis <Hyman@theitroad>" [unknown]
gpg: aka "[jpeg image of size 5015]" [unknown]
gpg: WARNING: This key is not certified with a trusted signature!
gpg: There is no indication that the signature belongs to the owner.
Primary key fingerprint: 409B 6B17 96C2 7546 2A17 0311 3804 BB82 D39D C0E3
 Subkey fingerprint: 62C9 E5F4 DA30 0D94 AC36 166B E206 C29F BF04 FF17
GPG verified '/home/sk/.rvm/archives/rvm-1.29.3.tgz'
Installing RVM to /home/sk/.rvm/
 Adding rvm PATH line to /home/sk/.profile /home/sk/.mkshrc /home/sk/.bashrc /home/sk/.zshrc.
 Adding rvm loading line to /home/sk/.profile /home/sk/.bash_profile /home/sk/.zlogin.
Installation of RVM in /home/sk/.rvm/is almost complete:
* To start using RVM you need to run `source /home/sk/.rvm/scripts/rvm`
 in all your open shell windows, in rare cases you need to reopen all shell windows.

正如我们在上面的输出中注意到的那样,有一条线说"开始使用RVM我们需要运行"源/home/sk/.rvm/scripts/rvm`":

因此,让我们运行该命令来设置RVM环境路径:

$source ~/.rvm/scripts/rvm

在基于RPM的系统上,这条线可能是不同的。
检查输出并相应运行该命令。
在我的CentOS 7操作系统中,我必须开始使用RVM来运行source/etc/profile.d/rvm.sh

所以,我ran以下命令:

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

添加RVM路径后,请运行以下命令以安装RVM依赖项。

$rvm requirements

示例输出:

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

我们已经完成了Ruby在Rails上的准备工作。

让我们继续安装Ruby和Rails。

安装Ruby.

现在,让我们找到使用命令的可用Ruby版本:

$rvm list known

示例输出:

# 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[.7]
[ruby-]2.3[.4]
[ruby-]2.4[.1]
ruby-head
# for forks use: rvm install ruby-head-<name> --url https://github.com/github/ruby.git --branch 2.2
# JRuby
jruby-1.6[.8]
jruby-1.7[.27]
jruby[-9.1.13.0]
jruby-head
# Rubinius
rbx-1[.4.3]
rbx-2.3[.0]
rbx-2.4[.1]
rbx-2[.5.8]
rbx-3[.84]
rbx-head
# Opal
opal
# Minimalistic ruby implementation - ISO 30170:2012
mruby-1.0.0
mruby-1.1.0
mruby-1.2.0
mruby-1[.3.0]
mruby[-head]
# Ruby Enterprise Edition
ree-1.8.6
ree[-1.8.7][-2012.02]
# Topaz
topaz
# MagLev
maglev[-head]
maglev-1.0.0
# Mac OS X Snow Leopard Or Newer
macruby-0.10
macruby-0.11
macruby[-0.12]
macruby-nightly
macruby-head
# IronRuby
ironruby[-1.1.3]
ironruby-head

要安装任何Ruby版本,例如2.4.1,从终端运行以下命令:

$rvm install 2.4.1

示例输出:

Searching for binary rubies, this might take some time.
No binary rubies available for: ubuntu/18.04/x86_64/ruby-2.4.1.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for ubuntu.
Installing requirements for ubuntu.
Updating system...
Installing required packages: libreadline6-dev...
Requirements installation successful.
Installing Ruby from source to: /home/sk/.rvm/rubies/ruby-2.4.1, this Jan take a while depending on your cpu(s)...
ruby-2.4.1 - #downloading ruby-2.4.1, this Jan take a while depending on your connection...
 % Total % Received % Xferd Average Speed Time Time Time Current
 Dload Upload Total Spent Left Speed
100 11.9M 100 11.9M 0 0 74477 0 0:02:48 0:02:48 --:--:-- 88459
ruby-2.4.1 - #extracting ruby-2.4.1 to /home/sk/.rvm/src/ruby-2.4.1....
ruby-2.4.1 - #applying patch /home/sk/.rvm/patches/ruby/2.4.1/random_c_using_NR_prefix.patch.
ruby-2.4.1 - #configuring..................................................................
ruby-2.4.1 - #post-configuration..
ruby-2.4.1 - #compiling.......................................................................
ruby-2.4.1 - #installing...........
ruby-2.4.1 - #making binaries executable..
ruby-2.4.1 - #downloading rubygems-2.6.14
 % Total % Received % Xferd Average Speed Time Time Time Current
 Dload Upload Total Spent Left Speed
100 751k 100 751k 0 0 46591 0 0:00:16 0:00:16 --:--:-- 44278
No checksum for downloaded archive, recording checksum in user configuration.
ruby-2.4.1 - #extracting rubygems-2.6.14....
ruby-2.4.1 - #removing old rubygems.........
ruby-2.4.1 - #installing rubygems-2.6.14...........................
ruby-2.4.1 - #gemset created /home/sk/.rvm/gems/Hyman@theitroad
ruby-2.4.1 - #importing gemset /home/sk/.rvm/gemsets/global.gems..............................|
ruby-2.4.1 - #generating global wrappers........
ruby-2.4.1 - #gemset created /home/sk/.rvm/gems/ruby-2.4.1
ruby-2.4.1 - #importing gemsetfile /home/sk/.rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.4.1 - #generating default wrappers........
ruby-2.4.1 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-2.4.1 - #complete 
Ruby was built without documentation, to build it run: rvm docs generate-ri

最后,使用命令设置Ruby默认版本:

$rvm use 2.4.1 --default

要查找已安装的Ruby版本,请运行:

$ruby -v

示例输出:

ruby 2.4.1p111 (2016-03-22 revision 58053) [x86_64-linux]

接下来,我们需要安装Rails。

安装Rails

可以使用命令安装rails:

$gem install rails

上面的命令将安装最新的Rails版本。
我们还可以安装特定版本,例如4.2.6,如下面。

$gem install rails -v 4.2.6

要查看已安装的Rails版本,请运行:

$rails -v

示例输出:

Rails 5.2.0