如何安装 Rails?

如何安装 Rails?

我在网上找不到这个特定的错误,但是在输入时sudo gem install rails -v 5.2.2出现以下错误:

    nysa@nysa-System-Product-Name:~$ sudo gem install rails -v 5.2.2
Fetching activesupport-5.2.2.gem
Fetching actionview-5.2.2.gem
Fetching actionpack-5.2.2.gem
Fetching railties-5.2.2.gem
Fetching activerecord-5.2.2.gem
Fetching activemodel-5.2.2.gem
Fetching activestorage-5.2.2.gem
Fetching actioncable-5.2.2.gem
Fetching activejob-5.2.2.gem
Fetching actionmailer-5.2.2.gem
Fetching rails-5.2.2.gem
Successfully installed activesupport-5.2.2
Successfully installed actionview-5.2.2
Successfully installed actionpack-5.2.2
Successfully installed railties-5.2.2
Successfully installed activemodel-5.2.2
Successfully installed activerecord-5.2.2
Successfully installed activestorage-5.2.2
Building native extensions. This could take a while...
ERROR:  Error installing rails:
    ERROR: Failed to build gem native extension.

    current directory: /var/lib/gems/2.5.0/gems/websocket-driver-0.7.1/ext/websocket-driver
/usr/bin/ruby2.5 -I /usr/local/lib/site_ruby/2.5.0 -r ./siteconf20190626-19849-1302wcu.rb extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/include/ruby.h

extconf failed, exit code 1

Gem files will remain installed in /var/lib/gems/2.5.0/gems/websocket-driver-0.7.1 for inspection.
Results logged to /var/lib/gems/2.5.0/extensions/x86_64-linux/2.5.0/websocket-driver-0.7.1/gem_make.out

看起来我已经安装了 rails 版本,但在输入时rails -v我收到一条消息,提示“rails”不存在。我该如何安装最新版本的 Rails 并使其正常工作?

答案1

安装 rails 涉及编译一些 C 源代码,这需要头文件无论 C 代码使用什么库。至少ruby-dev必须安装该软件包,因为它为 Ruby 本身提供了头文件。您看到的特定错误似乎是由于缺少它造成的,因为/usr/lib/ruby/include/ruby.h缺少。

安装ruby-dev通常足以解决这个问题。但是,我发现在最近没有安装的 Ubuntu 系统上zlib1g-dev,我也需要它。我猜人们通常已经安装了它,这就是为什么它不太需要安装的原因。

sudo apt update
sudo apt install ruby-dev zlib1g-dev

安装这些软件包后,您只需gem install再次运行命令即可。如果还有更多有关无法找到头文件的错误,您可以安装相应的-dev软件包,但我不认为您还需要安装任何其他东西。

ruby-dev同时安装有时修复 gem install错误提及未提及缺少的头文件的问题。


根据您要执行的操作,您可能还会考虑以下替代方案:

相关内容