0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

自作PCにwsl2で開発環境を整える~Rails導入編~

Last updated at Posted at 2021-06-26

Abstract

前回まででRubyとSQLを導入できたので、本番のRailsを入れます。

インストール

libpqを入れる

これがないとgem installができないようです。事件簿は後ろの方に書きます。

sudo apt install libpq-dev

pgを入れる

gem install pg

bundlerを入れる

gem install bundler

詳しい話はこの記事とかを読んでみるといいと思います。

railsを入れる

gem install rails

確認

rails -v

事件簿

$ gem install bundler
$ gem install rails

よし、確認だ!

$ rails -v

ん?

Could not find minitest-5.14.4 in any of the sources
Run `bundle install` to install missing gems.

足りないものがあるらしいからbundle installしろと言われた。

bundle install

ん!?

Fetching gem metadata from https://rubygems.org/............
   (中略)
Installing sassc 2.4.0 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: ~/.anyenv/envs/rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/pg-1.2.3/ext
~/.anyenv/envs/rbenv/versions/3.0.0/bin/ruby -I ~/.anyenv/envs/rbenv/versions/3.0.0/lib/ruby/3.0.0 -r
./siteconf20210626-5286-artcg.rb extconf.rb
checking for pg_config... yes
Using config values from /usr/bin/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** 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:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=~/.anyenv/envs/rbenv/versions/3.0.0/bin/$(RUBY_BASE_NAME)
        --with-pg
        --without-pg
        --enable-windows-cross
        --disable-windows-cross
        --with-pg-config
        --without-pg-config
        --with-pg_config
        --without-pg_config
        --with-pg-dir
        --without-pg-dir
        --with-pg-include
        --without-pg-include=${pg-dir}/include
        --with-pg-lib
        --without-pg-lib=${pg-dir}/lib

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  ~/.anyenv/envs/rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/extensions/x86_64-linux/3.0.0/pg-1.2.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in ~/.anyenv/envs/rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/pg-1.2.3 for inspection.   
Results logged to
~/.anyenv/envs/rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/extensions/x86_64-linux/3.0.0/pg-1.2.3/gem_make.out

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 bundling.

In Gemfile:
  pg

どうやらpgとかいうのが入らなかったらしい。Make sure that `gem install pg -v '1.2.3' --source 'https://rubygems.org/'` succeeds before bundling.と言われたのでとりあえずやってみよう。

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

が、当然にダメ。というか上の方で出てるエラーをちゃんと読むべきでしたね。

Building native extensions. This could take a while...
ERROR:  Error installing pg:
        ERROR: Failed to build gem native extension.

    current directory: ~/.anyenv/envs/rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/pg-1.2.3/ext
~/.anyenv/envs/rbenv/versions/3.0.0/bin/ruby -I ~/.anyenv/envs/rbenv/versions/3.0.0/lib/ruby/3.0.0 -r ./siteconf20210626-9298-nglvhd.rb extconf.rb
checking for pg_config... yes
Using config values from /usr/bin/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** 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:
        --with-opt-dir
            (中略)
        --without-pg-lib=${pg-dir}/lib

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  ~/.anyenv/envs/rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/extensions/x86_64-linux/3.0.0/pg-1.2.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in ~/.anyenv/envs/rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/pg-1.2.3 for inspection.   
Results logged to ~/.anyenv/envs/rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/extensions/x86_64-linux/3.0.0/pg-1.2.3/gem_make.out

さっきと同じじゃん。
Can't find the 'libpq-fe.h headerのあたりが気になったので調べてみたら、pg_configの場所を指定しろみたいな記事を見つけたのでやってみることに。

とりあえずconfigファイルを見つけたので実行。

gem install pg --with-pg-config=/lib/postgresql/13/bin/pg_config

すると......

ERROR:  While executing gem ... (OptionParser::InvalidOption)
    invalid option: --with-pg-config=/lib/postgresql/13/bin/pg_config

んー?invalidじゃねぇよ。

もう1つ書いてあった、bundleのconfigを変えてやってみる。

bundle config build.pg --with-pg-config=/lib/postgresql/13/bin/pg_config
bundle install

が、ダメ。最初と同じエラーが出ます。
もうちょっと調べてみると、「ARCHFLAGを指定してgemのインストール」みたいなのを見つけたので試してみる。

ARCHFLAGS="-arch x86_64" gem install pg

これでもダメ。
で、やはりCan't find the 'libpq-fe.h headerが気になるのでもう少し調べてみると、PostgreSQL関連のライブラリがないって話を聞いた。

sudo apt install libpq-dev
gem install pg -v `0.18.4`

おや……?

Building native extensions. This could take a while...
Successfully installed pg-1.2.3
Parsing documentation for pg-1.2.3
Installing ri documentation for pg-1.2.3
Done installing documentation for pg after 0 seconds
1 gem installed

行けた~!!!

という感じです。PostgreSQLのライブラリはちゃんと入れておきましょう。

バックナンバーと次回予告

初回:WSL導入編
前回:PostgreSQL導入編
次回:node/mongo導入編

参考

[Rails] WindowsのWSL2(Ubuntu、VSCode)にRailsをローカルインストールしてgitコマンドでgithub(master)にpushする @Taka5689
WSLで作るRuby on Rails環境構築 〜VSCode Remoteを添えて〜 @Developer_KanZG
Ruby on Rails - WSL2上で環境構築から簡単なアプリケーション開発まで @yut-nagase
gemをインストールする @pkrpkrhogehoge
pgがインストールできない @shiroitsubakito
gem install pgしてNo pg_configとなる対処法 @youcune
pg_config... no →libpq-dev いれとけ
Mavericks でgem install pgしてNo pg_configになるとき @satour
Mac/PostgreSQL gemのインストール @kawashinji
【Tips】Ubuntuへのpg gemインストール時に発生しうるエラーへの対処 @GandT

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?