LoginSignup
0
1

More than 3 years have passed since last update.

herokuにデプロイする際に嵌ったのでその備忘録(can't find the 'libpq-fe.h header、バイナリファイルを実行できません:実行形式エラー、remote: ! Do not authenticate with username and password using git.)

Last updated at Posted at 2020-05-13

環境・事前状況

・Windows10home on VirtualBox Ubuntu
・rails6
・railsアプリ作成済み。
・gitコマンド使用可能。
・herokuアカウント作成済み。
・heroku CLI インストール未了(herokuコマンドが使えるようになるやつ)

herokuにデプロイするまでのまとめ

 まずherokuコマンドを使えるようにする。

 herokuのdbはPostgresQLなのでGemfileにその記述などをしてからデプロイする。
 
 Gitでcommitしてからherokuにデプロイする流れになる。

 herokuコマンドだけでデプロイできなければ、sudoを付けてからしてみる。

 公式情報が分かりやすい。

 Rails 6.xでHerokuを使い始める
 https://devcenter.heroku.com/articles/getting-started-with-rails6

 Heroku CLI
 https://devcenter.heroku.com/articles/heroku-cli

備忘録

 heroku CLIインストール。実際は以下の公式で記載していたインストール方法ではなく、brew install heroku/brew/herokuでインストールしたのでエラーが発生し、後から以下のコマンドをし直すことになった。

$ sudo snap install --classic heroku

 herokuのdbはPostgresQLなので、sqlite3を本番環境から外し、pgを本番環境に入れる。
 Gemfileの記述は以下のとおりでなくてもよく、pgが本番環境で使えるようにすればよい。
 PostgresQLが本番と決まっていれば、開発、テストもpgの方がいい気がする。

Gemfile
gem "pg", group: :production

group:development, :test
  省略
  gem "sqlite3"
  省略
end

 cdでrailsアプリに移動するのを忘れずに。

$ bundle install

以下エラー(pgインストールできなった)
An error occurred while installing pg(1.2.3), and Bundler cannot continue.
Make sure that `gem install pg -v '1.2.3' --source 'https://rubygems.org/'` succeeds before bunding.

書いてある通りに実行。

$ gem install pg -v '1.2.3' --source 'https://rubygems.org/'

以下エラー
No pg_config... trying anyway.if building fails, please try again with --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
can't find the 'libpq-fe.h header
省略

libpq-fe.h headerなるものがないと言っている。
調べるとheader(って何?)はlibpq-devパッケージの一部であり、同パッケージまたはそれに相当するものをOSにインストールする必要があるとのこと。

$ sudo apt-get install libpq-dev

ちなみにその前に$ sudo yum install -y postgresql-develをすればいいよという記事を見つけたので実行したが、私の環境では「パッケージ postgresql-develは利用できません」と出た。yumでするとよくこのような表示が出る印象がある。RedHat系だから?

再度実行し、成功。

$ bundle install

sqliteではないので、以下のとおり修正。

config/databese.yml
production:
  <<: *default
  adapter: postgresql
  encoding: unicode
  pool: 5

herokuでアプリを作成。CLIのインストールをsudo snap install --classic herokuでしていなかったのでエラー。

$ heroku create
$ sudo heroku create

両方ともエラー
バイナルファイルを実行できません:実行形式エラー

以下の公式で記載されていたコマンドを実行。
最初にCLIをbrew install heroku/brew/herokuでインストールしていたため上手くいかなかった。最初から以下のコマンドでOK。

$ sudo snap install --classic heroku

heroku v7.41.1 from Heroku installed

再度挑戦。

$ sudo heroku create

以下のとおりエラー
heroku: Waiting for login... done

ClIError: timed out

 いつまでたってもログインができない。
 最初からcreateではなくlogin→createでやってみる。

$ heroku login --interactive

バイナリファイルを実行できません: 実行形式エラー

$ sudo heroku login --interactive

Email: herokuに登録したメアド入力
password: herokuに登録したパスワード入力
logged in as メアド

$ sudo heroku create
成功

 次はgit。
 commitしてherokuにpushする。

$ git add .
$ git commit -m "commit message"
$ git push heroku master
Username for 'https://git.heroku.com': 入力
password for 'https://(入力したユーザーネーム)git.heroku.com': 入力

以下エラー
remote: ! Do not authenticate with username and password using git.
省略

 UsernameとPasswordは何を入力?
 herokunのメアドとパスワードでは×。
 メアドとAPIキーも×。
 sudo heroku auth:tokenで表示されたものでも×。
 とりあえずsudoつけてみる。

$ sudo git push heroku master

 成功。
 UsernameとPasswordは聞かれなかった。
 これでherokuにrailsアプリがデプロイされた。
 herokuにデータベースを移行。


$ sudo heroku run rails db:migrate

 アプリを開いてみる。

$ sudo heroku open

Error opening web browser.
Error: Exited with code 3

Manually visit https://自分のアプリ名.heroku.com/ in your browser

 エラー。言われるとおりにブラウザで開いてみる。
 アプリの起動が確認できた。
 アプリのビューをちょっとだけ追加し、herokuも更新してみる。

$ git add .
$ git commit -m "commit message"
$ sudo git push heroku master

 アプリを確認。
 ビューは更新されていた。
 レコードを追加したときはまたdb:migrateをするのだろうか。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1