Rails new fails to install mysql

This is my first Qiita post! At first, I was planning to write an article while making a task management app for Rails and Vue, but at the time of rails new, myql installation did not go well and I stumbled, so I decided to write this article. However, the solution is written at the end, and until then it is a story until the solution is reached, so it is better to skip it. If you would like to read my own memorandum, please read it from above. Some people may know the environment, but it's a Mac. (If you use brew with zsh, it's a Mac, right?) As I recently learned, zsh seems to read Z shell.

The project should start

Anyway, it's rails new.

% rails -v
Rails 6.0.3.4

% rails new memo-memo -d mysql --skip-test 

An error occurred immediately w

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.

I will do as I was told.

% gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'

Since a Pemission error has occurred, stop thinking and add sudo here as well.

% sudo gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'

I'm not sure this time.

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/Cellar/mysql/8.0.22_1/lib

Let's make sure that mysql is installed correctly on the Mac in the first place. Then Brew seemed to be dissatisfied with me and gave me various hints.

% brew info mysql

mysql: stable 8.0.22 (bottled)
Open source relational database management system
https://dev.mysql.com/doc/refman/8.0/en/
Conflicts with:
  mariadb (because mysql, mariadb, and percona install the same binaries)
  percona-server (because mysql, mariadb, and percona install the same binaries)
/usr/local/Cellar/mysql/8.0.22_1 (294 files, 296.5MB) *
  Poured from bottle on 2020-12-13 at 08:50:29
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/mysql.rb
License: GPL-2.0
==> Dependencies
Build: cmake ✘
Required: [email protected] ✔, protobuf ✔
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Analytics
install: 85,291 (30 days), 236,256 (90 days), 823,613 (365 days)
install-on-request: 83,799 (30 days), 231,594 (90 days), 798,215 (365 days)
build-error: 0 (30 days)

Apparently mariadb and percona-server are conflicting and I wondered if cmake wasn't installed. So, first, I tried installing the simpler cmake. Then I hit brew info mysql again.

% brew install cmake
% brew info mysql

For the time being, I made progress because I changed from ✖️ of cmake to a check mark. For the time being, I checked if mysql can be installed.

% sudo gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'

I was able to confirm that the installation was not possible as usual. Now let's check the error firmly.

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

    current directory: /Library/Ruby/Gems/2.6.0/gems/mysql2-0.5.3/ext/mysql2
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby -I /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0 -r ./siteconf20201218-6446-1qkv5ft.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/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... no
-----
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/Cellar/mysql/8.0.22_1/lib
-----
creating Makefile

current directory: /Library/Ruby/Gems/2.6.0/gems/mysql2-0.5.3/ext/mysql2
make "DESTDIR=" clean

current directory: /Library/Ruby/Gems/2.6.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: warning: directory not found for option '-L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.Internal.sdk/usr/local/lib'
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 /Library/Ruby/Gems/2.6.0/gems/mysql2-0.5.3 for inspection.
Results logged to /Library/Ruby/Gems/2.6.0/extensions/universal-darwin-19/2.6.0/mysql2-0.5.3/gem_make.out

Should I go through libpath? I will think about it. (I will notice later that this interpretation is wrong)

% export LIBRARY_PATH=/usr/local/Cellar/mysql/8.0.22_1/lib
% sudo gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'

However, the content of the error did not change. Since I wrote Gonyo Gonyo after Creating Makefile here, I was running some Makefile when gem install, and in the middle I failed with ld: liblary not found for -lssl and the installation did not fail. I thought. (I apologize if I'm wrong.)

I decided to raise the white flag here and look for someone who had the same problem. Then, the following excellent articles were hit! Troubleshooting when installing mysql2 gem

It was a story that could be solved from the beginning if you look here ww I'm sorry for those who have read this far. Please note that it is a realistic error resolution story while writing Qiita.

In other words, I set the path to LIBRARY_PATH =/usr/local/Cellar/mysql/8.0.22_1/lib, but it says library not found because the library I was looking for was not found, and --with-cppflags By specifying --with-ldflags, you will be able to find the library properly and install it. Please refer to the original article to be exact.

Solutions

% brew info openssl
For compilers to find openssl you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl/include"

% sudo gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/' -- --with-cppflags=-I/usr/local/opt/openssl/include --with-ldflags=-L/usr/local/opt/openssl/lib

% bundle install

Thank you for your hard work. If you did rails new, but if anyone stumbled upon installing mysql, I would appreciate it if you could refer to it.

Recommended Posts

Rails new fails to install mysql
mysql2 fails to install with bundle install
How to install MySQL
[Rails] How to install devise
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to install Font Awesome
[Rails] How to add new pages
[Rails] How to install ImageMagick (RMajick)
Steps to install MySQL 8 on CentOS 8
[Rails] How to install Font Awesome
[Rails 6] MySQL 2 cannot be bundle install
Rails DB PostgreSQL changed to MySQL
How to install Swiper in Rails
Install Webpacker and Yarn to run Rails
Deploy to heroku with Docker (Rails 6, MySQL)
How to use MySQL in Rails tutorial
Docker container build fails to install php-radis
[Rails] rails new to create a database with PostgreSQL
Error in bundle install when running rails new
Introducing New Relic to Rails apps on Heroku
[Rails MySQL] How to reset DB on heroku
Error deploying rails5 + Mysql to heroku with Docker-compose
[Ruby on Rails] How to install Bootstrap in Rails
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
How to write Rails
Introducing CircleCI to Rails
Introducing Bootstrap to Rails 5
Introducing Bootstrap to Rails !!
When semantic-ui install fails
[Java] Connect to MySQL
Introduce Vue.js to Rails
Mac Rails Install Error
How to install Docker
How to uninstall Rails
How to install docker-machine
[Rails] Development with MySQL
Install MySQL 5.6 on CentOS6 [How to specify the version]
[Ruby on Rails] From MySQL construction to database change
[Rails] How to install a decorator using gem draper
How to install ngrok
After setting database to mysql, bundle install causes an error
Super easy in 2 steps! How to install devise! !! (rails 5 version)
Options for rails new and settings to be done after rails new
What to do if mysql2 gets a bundle install error
[Rails] Launch a new project
[rails] How to post images
rails new application launch command
Rails6 (MySQL, Ubuntu environment, Cloud9)
Install Sidekiq pro on Rails
[Rails] How to use enum
bundle install does not install mysql2
New features from Java7 to Java8
[Rails] How to use enum
How to read rails routes
How to install Boots Faces
How to use rails join
(For beginners) [Rails] Install Devise
Connect to MySQL 8 with Java
Install Rails on macOS Catalina
[Rails] Add column to devise