Ruby on Rails is a popular application stack for developers looking to create sites and web apps. The Ruby programming language, combined with the Rails development framework, makes app development quick and efficient.
对于希望创建站点和Web应用程序的开发人员而言, Ruby on Rails是一种流行的应用程序堆栈。 Ruby编程语言与Rails开发框架相结合,使应用程序开发快速高效。
One way to install Ruby and Rails is with the command-line tool rbenv. Using rbenv will provide you with a well-controlled and robust environment for developing your Ruby on Rails applications, allowing you to easily switch the version of Ruby for your entire team when needed.
安装Ruby和Rails的一种方法是使用命令行工具rbenv 。 使用rbenv将为您提供一个良好控制的健壮环境,用于开发Ruby on Rails应用程序,使您可以在需要时轻松地为整个团队切换Ruby版本。
rbenv provides support for specifying application-specific versions of Ruby, lets you change the global Ruby for each user, and allows you to use an environment variable to override the Ruby version.
rbenv支持指定特定于应用程序的Ruby版本,允许您为每个用户更改全局Ruby,并允许您使用环境变量来覆盖Ruby版本。
In this tutorial, you will use rbenv to install and set up Ruby on Rails on your local macOS machine.
在本教程中,您将使用rbenv在本地macOS机器上安装和设置Ruby on Rails。
To follow this tutorial, you will need:
要遵循本教程,您将需要:
One computer or virtual machine with macOS installed, with administrative access to that machine and an internet connection. This tutorial has been tested on macOS 10.14 Mojave.
一台装有macOS的计算机或虚拟机,可以对该计算机进行管理访问并具有Internet连接。 本教程已在macOS 10.14 Mojave上进行了测试。
Node.js installed on your macOS machine, as explained in How to Install Node.js and Create a Local Development Environment on macOS. A few Rails features, such as the Asset Pipeline, depend on a JavaScript Runtime. Node.js provides this functionality.
如如何在macOS上安装Node.js和创建本地开发环境中所述,在MacOS机器上安装了Node.js。 一些Rails功能(例如Asset Pipeline )取决于JavaScript运行时。 Node.js提供了此功能。
In this step, you will install rbenv and make sure that it starts automatically at boot. To do this on macOS, this tutorial will use the package manager Homebrew.
在此步骤中,您将安装rbenv并确保其在启动时自动启动。 要在macOS上执行此操作,本教程将使用软件包管理器Homebrew 。
To download the rbenv
package with Homebrew, run the following command:
要使用Homebrew下载rbenv
软件包,请运行以下命令:
This will install rbenv and the ruby-build plugin. This plugin adds therbenv install
command, which streamlines the installation process for new versions of Ruby.
这将安装rbenv和ruby-build插件。 该插件添加了rbenv install
命令,该命令简化了Ruby新版本的安装过程。
Next, you’ll add the command eval "$(rbenv init -)"
to your ~/.bash_profile
file to make rbenv load automatically when you open up the Terminal. To do this, open your .bash_profile
in your favorite text editor:
接下来,将命令eval "$(rbenv init -)"
到~/.bash_profile
文件中,以在打开终端时自动加载rbenv。 为此,请在您喜欢的文本编辑器中打开.bash_profile
:
Add the following line to the file:
将以下行添加到文件中:
eval "$(rbenv init -)"
Save and quit the file.
保存并退出文件。
Next, apply the changes you made to your ~/.bash_profile
file to your current shell session:
接下来,将对~/.bash_profile
文件所做的更改应用于当前的shell会话:
To verify that rbenv is set up properly, use the type
command, which will display more information about the rbenv
command:
要验证rbenv的设置是否正确,请使用type
命令,该命令将显示有关rbenv
命令的更多信息:
Your terminal window will display the following:
您的终端窗口将显示以下内容:
Output
rbenv is a function
rbenv ()
{
local command;
command="${1:-}";
if [ "$#" -gt 0 ]; then
shift;
fi;
case "$command" in
rehash | shell)
eval "$(rbenv "sh-$command" "$@")"
;;
*)
command rbenv "$command" "$@"
;;
esac
}
At this point, you have both rbenv and ruby-build installed on your machine. This will allow you to install Ruby from the command line in the next step.
此时,您的计算机上同时安装了rbenv和ruby-build。 这将允许您在下一步中从命令行安装Ruby。
With the ruby-build plugin now installed, you can install any version of Ruby you may need through a single command. In this step, you will choose a version of Ruby, install it on your machine, and then verify the installation.
现在安装了ruby-build插件,您可以通过一个命令安装所需的任何版本的Ruby。 在此步骤中,您将选择Ruby版本,将其安装在计算机上,然后验证安装。
First, use the -l
flag to list all the available versions of Ruby:
首先,使用-l
标志列出所有可用的Ruby版本:
The output of that command will be a long list of versions that you can choose to install.
该命令的输出将是一长串可供选择安装的版本。
For this tutorial, install Ruby 2.6.3:
对于本教程,请安装Ruby 2.6.3 :
rbenv install 2.6.3
rbenv安装2.6.3
Installing Ruby can be a lengthy process, so be prepared for the installation to take some time to complete.
安装Ruby可能是一个漫长的过程,因此请做好准备,以完成安装过程。
Once it’s done installing, set it as your default version of Ruby with the global
sub-command:
安装完成后,使用global
子命令将其设置为Ruby的默认版本:
rbenv global 2.6.3
rbenv全局2.6.3
Verify that Ruby was properly installed by checking its version number:
通过检查其版本号来验证Ruby是否已正确安装:
Your output will look something like this:
您的输出将如下所示:
Output
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]
To install and use a different version of Ruby, run the rbenv
commands with a different version number, such as rbenv install 2.3.0
and rbenv global 2.3.0
.
要安装和使用其他版本的Ruby,请运行具有不同版本号的rbenv
命令,例如rbenv install 2.3.0
和rbenv global 2.3.0
。
You now have one version of Ruby installed and have set your default Ruby version. Next, you will set yourself up to work with Ruby packages and libraries, or gems, which will then allow you to install Rails.
现在,您已经安装了一个Ruby版本,并设置了默认的Ruby版本。 接下来,您将进行设置以使用Ruby包和库或gems ,然后可以使用它们安装Rails。
Gems are packages of Ruby libraries and programs that can be distributed throughout the Ruby ecosystem. You use the gem
command to manage these gems. In this step, you will configure the gem
command to prepare for the Rails installation.
Gems是可在整个Ruby生态系统中分发的Ruby库和程序的软件包。 您可以使用gem
命令来管理这些gem。 在此步骤中,您将配置gem
命令以准备安装Rails。
When you install a gem, the installation process generates local documentation. This can add a significant amount of time to each gem’s installation process, so turn off local documentation generation by creating a file called ~/.gemrc
which contains a configuration setting to turn off this feature:
当您安装gem时,安装过程将生成本地文档。 这会为每个gem的安装过程增加大量时间,因此,通过创建一个名为~/.gemrc
的文件来关闭本地文档生成,该文件包含用于禁用此功能的配置设置:
With that done, use the gem
command to install Bundler, a tool that manages gem dependencies for projects. This is needed for Rails to work correctly:
完成此操作后,使用gem
命令安装Bundler ,该工具可管理项目的gem依赖关系。 这是Rails正常工作所必需的:
You’ll see output like this:
您将看到如下输出:
Output
Fetching: bundler-2.0.2.gem
Successfully installed bundler-2.0.2
1 gem installed
You can use the gem env
command to learn more about the environment and configuration of gems. To see the location of installed gems, use the home
argument, like this:
您可以使用gem env
命令来了解有关gem env
的环境和配置的更多信息。 要查看已安装的gem的位置,请使用home
参数,如下所示:
You’ll see output similar to this:
您将看到类似于以下的输出:
/Users/sammy/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0
Now that you have set up and explored your gem workflow, you are free to install Rails.
既然您已经设置并浏览了gem工作流程,就可以自由安装Rails。
To install Rails, use the gem install
command along with the -v
flag to specify the version. For this tutorial, we will use version 5.2.3
:
要安装Rails,请使用gem install
命令以及-v
标志来指定版本。 在本教程中,我们将使用版本5.2.3
:
gem install rails -v 5.2.3
gem安装导轨-v 5.2.3
The gem
command installs the gem you specify, as well as every dependency. Rails is a complex web development framework and has many dependencies, so the process will take some time to complete. Eventually you’ll see a message stating that Rails is installed, along with its dependencies:
gem
命令将安装您指定的gem以及每个依赖项。 Rails是一个复杂的Web开发框架,具有许多依赖性,因此该过程将需要一些时间才能完成。 最终,您会看到一条消息,说明已安装Rails及其依赖项:
Output
...
Successfully installed rails-5.2.3
38 gems installed
Note: If you would like to install a different version of Rails, you can list the valid versions of Rails by doing a search, which will output a long list of possible versions. We can then install a specific version, such as 4.2.7:
注意 :如果要安装其他版本的Rails,可以通过搜索列出有效的Rails版本,这将输出一长串可能的版本。 然后,我们可以安装特定版本,例如4.2.7 :
gem install rails -v 4.2.7
gem安装导轨-v 4.2.7
If you would like to install the latest version of Rails, run the command without a version specified:
如果要安装最新版本的Rails,请在不指定版本的情况下运行命令:
rbenv works by creating a directory of shims, or libraries that intercept calls and change or redirect them. In this case, shims point Ruby commands to the files used by the Ruby version that’s currently enabled. Through the rehash
sub-command, rbenv maintains shims in that directory to match every Ruby command across every installed version of Ruby on your server. Whenever you install a new version of Ruby or a gem that provides commands, such as Rails, you should use rehash
.
rbenv通过创建shims的目录来工作,该目录是拦截呼叫并更改或重定向它们的库。 在这种情况下,填充将Ruby命令指向当前启用的Ruby版本使用的文件。 通过rehash
子命令,rbenv在该目录中维护垫片,以匹配服务器上每个已安装Ruby版本的每个Ruby命令。 每当您安装新版本的Ruby或提供命令(例如Rails)的gem时,都应该使用rehash
。
To rehash the directory of shims, run the following command:
要重新哈希目录,请运行以下命令:
Verify your installation of Rails by printing its version with this command:
通过使用以下命令打印Rails的版本来验证您的Rails安装:
You will see the version of Rails that was installed:
您将看到已安装的Rails版本:
Output
Rails 5.2.3
With Rails successfully installed, you can begin testing your Ruby on Rails installation and start to develop web applications. In the next step, you will learn how to update and uninstall rbenv and Ruby.
成功安装Rails之后,您就可以开始测试Ruby on Rails的安装并开始开发Web应用程序。 在下一步中,您将学习如何更新和卸载rbenv和Ruby。
When maintaining projects, it is useful to know how to update and uninstall when the need arises. In this step, you will upgrade rbenv, then uninstall Ruby and rbenv from your machine.
维护项目时,了解在需要时如何更新和卸载很有用。 在此步骤中,您将升级rbenv,然后从计算机上卸载Ruby和rbenv。
You can upgrade rbenv and ruby-build using Homebrew by running the following command:
您可以通过运行以下命令使用Homebrew升级rbenv和ruby-build:
If rbenv or ruby-build need to be updated, Homebrew will do it for you automatically. If your set up is already up to date, you will get output similar to the following:
如果需要更新rbenv或ruby-build,Homebrew将自动为您完成。 如果您的设置已经是最新的,则将获得类似于以下内容的输出:
Output
Error: rbenv 1.1.2 already installed
Error: ruby-build 20190615 already installed
This will ensure that we are using the most up-to-date version of rbenv available.
这将确保我们使用的是最新版本的rbenv。
As you download additional versions of Ruby, you may accumulate more versions than you would like in your ~/.rbenv/versions
directory. Using the ruby-build plugin’s uninstall
subcommand, you can remove these previous versions.
当您下载其他版本的Ruby时,您可能会积累比~/.rbenv/versions
目录中更多的版本。 使用ruby-build插件的uninstall
子命令,可以删除这些以前的版本。
For example, run the following to uninstall Ruby version 2.1.3:
例如,运行以下命令来卸载Ruby 2.1.3版本:
rbenv uninstall 2.1.3
rbenv卸载2.1.3
With the rbenv uninstall
command you can clean up old versions of Ruby so that you do not have more installed than you are currently using.
使用rbenv uninstall
命令,您可以清理Ruby的旧版本,以便安装的数量不超过当前使用的数量。
If you’ve decided you no longer want to use rbenv, you can remove it from your system.
如果您决定不再使用rbenv,则可以将其从系统中删除。
To do this, first open your ~/.bash_profile
file in your editor:
为此,请首先在编辑器中打开~/.bash_profile
文件:
Find and remove the following line from the file to stop rbenv from starting when you open the Terminal:
查找并从文件中删除以下行,以在打开终端时阻止rbenv启动:
...
eval "$(rbenv init -)"
Once you have deleted this line, save the file and exit the editor.
删除此行后,保存文件并退出编辑器。
Run the following command to apply the changes to your shell:
运行以下命令以将更改应用于您的shell:
Next, remove rbenv and all installed Ruby versions with this command:
接下来,使用以下命令删除rbenv和所有已安装的Ruby版本:
Finally, remove the rbenv package itself with Homebrew:
最后,使用Homebrew删除rbenv软件包本身:
Check the rbenv version to make sure that it has been uninstalled:
检查rbenv版本以确保已将其卸载:
You will get the following output:
您将获得以下输出:
Output
-bash: /usr/local/bin/rbenv: No such file or directory
This means that you have successfully removed rbenv from your machine.
这意味着您已成功从计算机中删除了rbenv。
In this tutorial you installed Ruby on Rails with rbenv on macOS. From here, you can learn more about coding in Ruby with our How To Code in Ruby series. You can also explore how to use Ruby on Rails with PostgreSQL rather than its default sqlite3 database, which provides more scalability, centralization, and stability for your applications.
在本教程中,您在macOS上安装了带rbenv的Ruby on Rails。 从这里,您可以通过我们的《 如何在Ruby中编码》系列来学习有关在Ruby中进行编码的更多信息。 您还可以探索如何在PostgreSQL上使用Ruby on Rails,而不是在默认的sqlite3数据库中使用它,它为应用程序提供了更大的可伸缩性,集中性和稳定性。
翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-ruby-on-rails-with-rbenv-on-macos