我刚刚开始阅读Michael Hartl撰写的ruby.railstutorial.org书,并且已经完成了第一章的工作。我正在使用mac book OS X,Terminal和Sublime Text。一切都按计划进行,直到测试部署到Heroku为止。我能够连接到Heroku并运行$ git push heroku
主命令。但是部署失败了:
Installing sqlite3 (1.3.5) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/usr/local/bin/ruby extconf.rb
checking for sqlite3.h... no
sqlite3.h is missing. Try 'port install sqlite3 +universal'
or 'yum install sqlite-devel' and check your shared library search path (the
location where your sqlite3 shared library is located).
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.
Provided configuration options:
An error occurred while installing sqlite3 (1.3.5), and Bundler cannot continue.
Make sure that `gem install sqlite3 -v '1.3.5'` succeeds before bundling.
!
! Failed to install gems via Bundler.
!
! Detected sqlite3 gem which is not supported on Heroku.
! http://devcenter.heroku.com/articles/how-do-i-use-sqlite3-for-development
!
! Heroku push rejected, failed to compile Ruby/rails app
这是我的Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.8'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
group :development, :test do
gem 'sqlite3', '1.3.5'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.5'
gem 'coffee-rails', '~> 3.2.2'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.2.3'
end
gem 'jquery-rails', '2.0.2'
group :production do
gem 'pg', '0.12.2'
end
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
# Use unicorn as the app server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'debugger'
我指定sqlite3进行开发而不是生产,所以我认为Heroku会一起忽略它,但似乎并非如此。
另外,当我创建我正在使用的包时 $ bundle install - withoutout production
我知道有些人建议只安装PG并使用它,但我真的希望尽可能坚持本教程,然后再冒险尝试不同的方法。
此刻我有点失落,不知道如何从这里开始。您可以提供的任何帮助将非常感激。
由于
答案 0 :(得分:22)
无论出于何种原因,Heroku都无法安装sqlite3 gem。但你可以告诉bundler
除了开发时它不应该尝试。
在Gemfile
中,将gem 'sqlite3'
替换为:
group :development, :test do
gem 'sqlite3'
end
group :production do
gem 'pg'
end
然后,heroku上的bundler以production
运行,不会尝试安装它。
答案 1 :(得分:14)
我终于能够成功部署到Heroku。感谢evanc3指向我在Heroku网站上的一篇文章。在部署到Heroku之前,我似乎忘了提交我的Gemgile更新。因此,对于刚刚开始的所有人,您需要确保在部署到Heroku之前提交更改。
答案 2 :(得分:9)
Heroku不支持sqlite3 ...
从Gemfile中删除sqlite3,改为使用pg gem。在gem文件中进行以下更改
在Gemfile
gem 'sqlite3'
到
gem 'pg' #you will have to install postgresql
重要提示:运行
git add .
git commit
git push heroku master
注意:如果你打算为heroku进行部署,我建议你也应该在开发阶段使用postgres(在你的计算机上安装postgresql),heroku更喜欢psql。
如果要使用sqllite进行开发,使用postgresql进行Heroku,请使用以下配置。
group :development do
gem 'sqlite3' #gem to use in development environment
end
group :production do
gem 'pg' #gem to use in production environment
end
Heroku将使用pg
gem,因为heroku在生产环境中运行您的应用程序
答案 3 :(得分:1)
在Heroku上,您的应用无法访问文件系统。这有很多原因 - 这主要是因为您可以通过添加新实例(即一次运行多个服务器)来扩展应用程序的性能,并且这些实例不能保证在同一台物理计算机上 - 复制跨越的文件会非常慢。
SQLite只是将数据库存储到db /文件夹中的文件中,这就是它与Heroku不兼容的原因。
帮助链接中建议的最佳选择是远离SQLite,因为SQLite和PostgreSQL(Heroku的首选数据库)之间有时会出现微妙的不兼容性,并且您希望在之前找到它你部署到生产!
安装PostgreSQL后(具体如何执行此操作取决于您的操作系统),然后将gem 'pg'
添加到您的Gemfile中。
答案 4 :(得分:0)
我有一个解决方案,如果你的gemfile中没有sqlite3,你仍然会收到此错误。
最有可能的是,你有一个使用sqlite3作为依赖项的gem,它包含了你不知道的gem。
1)转到Gemfile.lock并搜索sqlite。
2)找到使用sqlite的gem,然后将gem移动到开发或测试组中。
3)捆绑
答案 5 :(得分:0)
在 Rails 教程中,他们让您在开发中使用 sqlite3
,在生产中使用 pg
。 heroku 部署可能失败的一个原因是,如果您在 heroku 上的 production
部署仍然引用 sqlite
中的 database.yml
而不是 postgres
。
tldr; 您的 config/database.yml
应与此处显示的相匹配:
https://github.com/mhartl/sample_app_6th_ed/blob/main/config/database.yml
最重要的是您的 production
配置行应该与此匹配(以便 adapter: postgres
而不是 adapter: sqlite3
):
production:
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
database: sample_app_production