rails newして新たなアプリケーションを作成しようとしたら、指定したrailsのgemがないというエラーが出ました。
Sayo-MacBook-Pro:sample SAYO$ rails _6.1.3.1_ new sample --database=mysql --skip-test
Traceback (most recent call last):
2: from /Users/SAYO/.rbenv/versions/2.5.1/bin/rails:23:in `<main>'
1: from /Users/SAYO/.rbenv/versions/2.5.1/lib/ruby/site_ruby/2.5.0/rubygems.rb:300:in `activate_bin_path'
/Users/SAYO/.rbenv/versions/2.5.1/lib/ruby/site_ruby/2.5.0/rubygems.rb:281:in `find_spec_for_exe': can't find gem railties (= 6.1.3.1) with executable rails (Gem::GemNotFoundException)
このエラーを解決するために、railsのgemをインストールしました。
Sayo-MacBook-Pro:sample SAYO$ gem install rails -v 6.1.3.1
Fetching activesupport-6.1.3.1.gem
Fetching actionview-6.1.3.1.gem
Fetching actionpack-6.1.3.1.gem
Fetching railties-6.1.3.1.gem
Fetching activerecord-6.1.3.1.gem
Fetching activemodel-6.1.3.1.gem
Fetching activejob-6.1.3.1.gem
Fetching activestorage-6.1.3.1.gem
Fetching rails-6.1.3.1.gem
Fetching actiontext-6.1.3.1.gem
Fetching actionmailer-6.1.3.1.gem
Fetching actionmailbox-6.1.3.1.gem
Fetching actioncable-6.1.3.1.gem
Successfully installed activesupport-6.1.3.1
Successfully installed actionview-6.1.3.1
Successfully installed actionpack-6.1.3.1
Successfully installed railties-6.1.3.1
Successfully installed activemodel-6.1.3.1
Successfully installed activerecord-6.1.3.1
Successfully installed activejob-6.1.3.1
Successfully installed activestorage-6.1.3.1
Successfully installed actiontext-6.1.3.1
Successfully installed actionmailer-6.1.3.1
Successfully installed actionmailbox-6.1.3.1
Successfully installed actioncable-6.1.3.1
Successfully installed rails-6.1.3.1
Parsing documentation for activesupport-6.1.3.1
Installing ri documentation for activesupport-6.1.3.1
Parsing documentation for actionview-6.1.3.1
Installing ri documentation for actionview-6.1.3.1
Parsing documentation for actionpack-6.1.3.1
Installing ri documentation for actionpack-6.1.3.1
Parsing documentation for railties-6.1.3.1
Installing ri documentation for railties-6.1.3.1
Parsing documentation for activemodel-6.1.3.1
Installing ri documentation for activemodel-6.1.3.1
Parsing documentation for activerecord-6.1.3.1
Installing ri documentation for activerecord-6.1.3.1
Parsing documentation for activejob-6.1.3.1
Installing ri documentation for activejob-6.1.3.1
Parsing documentation for activestorage-6.1.3.1
Installing ri documentation for activestorage-6.1.3.1
Parsing documentation for actiontext-6.1.3.1
Installing ri documentation for actiontext-6.1.3.1
Parsing documentation for actionmailer-6.1.3.1
Installing ri documentation for actionmailer-6.1.3.1
Parsing documentation for actionmailbox-6.1.3.1
Installing ri documentation for actionmailbox-6.1.3.1
Parsing documentation for actioncable-6.1.3.1
Installing ri documentation for actioncable-6.1.3.1
Parsing documentation for rails-6.1.3.1
Installing ri documentation for rails-6.1.3.1
Done installing documentation for activesupport, actionview, actionpack, railties, activemodel, activerecord, activejob, activestorage, actiontext, actionmailer, actionmailbox, actioncable, rails after 28 seconds
13 gems installed
そしてもう一度rails newします。データベースにMySQLを指定します。
Sayo-MacBook-Pro:sample SAYO$ rails _6.1.3.1_ new sample --database=mysql --skip-test
Sayo-MacBook-Pro:sample SAYO$ cd sample/
Sayo-MacBook-Pro:sample SAYO$ bundle install
Fetching gem metadata from <https://rubygems.org/>............
Resolving dependencies....
Using rake 13.0.3
Using concurrent-ruby 1.1.8
Using minitest 5.14.4
Using racc 1.5.2
Using crass 1.0.6
Using rack 2.2.3
Using nio4r 2.5.7
Using websocket-extensions 0.1.5
Using marcel 1.0.1
Using mini_mime 1.0.3
Using bindex 0.8.1
Using msgpack 1.4.2
Using bundler 2.2.16
Using byebug 11.1.3
Using ffi 1.15.0
Using rb-fsevent 0.11.0
Using method_source 1.0.0
Using zeitwerk 2.4.2
Using thor 1.1.0
Using tilt 2.0.10
Using semantic_range 3.0.0
Using spring 2.1.1
Using turbolinks-source 5.2.0
Using i18n 1.8.10
Using tzinfo 2.0.4
Using nokogiri 1.11.4 (x86_64-darwin)
Using rack-test 1.1.0
Using rack-mini-profiler 2.3.2
Using rack-proxy 0.6.5
Using sprockets 4.0.2
Using websocket-driver 0.7.3
Using puma 5.3.1
Using mail 2.7.1
Using bootsnap 1.7.5
Using rb-inotify 0.10.1
Using sassc 2.4.0
Using turbolinks 5.2.1
Using activesupport 6.1.3.2
Using loofah 2.9.1
Using listen 3.5.1
Using rails-dom-testing 2.0.3
Using globalid 0.4.2
Using activemodel 6.1.3.2
Using jbuilder 2.11.2
Using rails-html-sanitizer 1.3.0
Using activejob 6.1.3.2
Using activerecord 6.1.3.2
Fetching mysql2 0.5.3
Using builder 3.2.4
Using erubi 1.10.0
Using actionview 6.1.3.2
Using actionpack 6.1.3.2
Using actioncable 6.1.3.2
Using activestorage 6.1.3.2
Using sprockets-rails 3.2.2
Using actionmailer 6.1.3.2
Using railties 6.1.3.2
Using actionmailbox 6.1.3.2
Using sassc-rails 2.1.2
Using webpacker 5.4.0
Using sass-rails 6.0.0
Using web-console 4.1.0
Using actiontext 6.1.3.2
Using rails 6.1.3.2
Installing mysql2 0.5.3 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /Users/SAYO/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.5.3/ext/mysql2
/Users/SAYO/.rbenv/versions/2.5.1/bin/ruby -I /Users/SAYO/.rbenv/versions/2.5.1/lib/ruby/site_ruby/2.5.0 -r ./siteconf20210519-27070-xoi5mn.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
-----
Using mysql_config at /usr/local/opt/[email protected]/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for SSL_MODE_DISABLED in mysql.h... yes
checking for SSL_MODE_PREFERRED in mysql.h... yes
checking for SSL_MODE_REQUIRED in mysql.h... yes
checking for SSL_MODE_VERIFY_CA in mysql.h... yes
checking for SSL_MODE_VERIFY_IDENTITY in mysql.h... yes
checking for MYSQL.net.vio in mysql.h... yes
checking for MYSQL.net.pvio in mysql.h... no
checking for MYSQL_ENABLE_CLEARTEXT_PLUGIN in mysql.h... yes
checking for SERVER_QUERY_NO_GOOD_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_NO_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_WAS_SLOW in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_ON in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_OFF in mysql.h... yes
checking for my_bool in mysql.h... yes
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /usr/local/opt/[email protected]/lib
-----
creating Makefile
current directory: /Users/SAYO/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.5.3/ext/mysql2
make DESTDIR\\= clean
current directory: /Users/SAYO/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.5.3/ext/mysql2
make DESTDIR\\=
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1
make failed, exit code 2
Gem files will remain installed in /Users/SAYO/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.5.3 for inspection.
Results logged to /Users/SAYO/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-20/2.5.0/mysql2-0.5.3/gem_make.out
An error occurred while installing mysql2 (0.5.3), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.3' --source '<https://rubygems.org/'`> succeeds before bundling.
In Gemfile:
mysql2
bundle install を実行したらエラーが発生しました。なので下記のコマンドを実行しました。
Sayo-MacBook-Pro:sample SAYO$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib"
Sayo-MacBook-Pro:sample SAYO$ bundle install
このコマンドでMySQLのgemをインストールすることができました。
参考 : https://qiita.com/SAYJOY/items/dd7c8fc7a3647e7ff969
config/database.ymlを確認すると以下のようになっています。
default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password:
host: localhost
development:
<<: *default
database: sample_development
test:
<<: *default
database: sample_test
production:
<<: *default
database: sample_production
username: sample
password: <%= ENV['SAMPLE_DATABASE_PASSWORD'] %>
上記のdatabase.ymlにパスワード等を入れれば使えるようになりますが、パスワードをGitHubで管理するのはとても危険なので、環境変数として管理します。そのためにGemfileにdotenv
を導入してbundle install
します。
gem 'dotenv-rails'
そしてアプリケーションのルートディレクトリに.env
ファイルを追加します。
$ touch .env