0
0

Ruby,Railsのバージョンアップ

Posted at

背景

  • Ruby2.7.7とRails6.0.6.1で開発したアプリを運用します。デプロイ先はherokuで特に問題なくアプリは動いていました。
  • しかしながら、Ruby2.6.ZとRails6.1.Zでは、Status(Rails Maintenance Plicy)が「EOL」となっています。
  • そこで、Status(Rails Maintenance Plicy)が「Maintained for Security Issues」となっているRuby3.0.ZとRails6.1.Zへのバージョンアップを試みました。

<参考記事>

現在のバージョン

Ruby:2.7.7
Rails:6.0.6.1
bundler:2.4.13
node:16.20.0
npm:8.19.4
yarn:1.22.19
webpack:4.46.0
※開発環境は、macOS Monterey バージョン12.1です。
※エディターは、VSCodeを使用しています。
※デプロイ先は、herokuです。

今回のアップデート

ruby:2.7.7⇨3.0.6p216
rails:6.0.6.1⇨6.1.0
bundler:2.4.13⇨2.5.3、2.4.13(default:2.4.13)

Rubyのバージョンアップ

''''''

ターミナル
# rbenvとrubyリストの更新
brew upgrade rbenv ruby-build

# リストの確認
rbenv install --list

# rubyのインストール
rbenv install 3.0.6

<参考記事>

rbenvではrubyのインストールがうまくいかなかったため、rvmでインストールを試みました。

ターミナル
rvm install "ruby-3.0.6"

rvmでもrubyのインストールがうまくいきませんでした。

opensslでエラーが発生したためです。

そこで、openssl経由でrubyをインストールしました。

ターミナル
#opensslのインストール
brew install openssl

#openssl経由でのrubyのインストール
rvm install 3.0.6 --with-openssl-dir=`brew --prefix openssl@1.1`

プロフェクトディレクトリ直下の.ruby-versionファイルのコードを以下のとおり修正しました。

.ruby-version
ruby '2.7.7'
ruby '3.0.6'

Railsのバージョンアップ

Gemfileに以下のコードを書く。

Gemfile
gem 'rails', '~> 6.1

「bundle update rails」を実行して、railsをアップデートしようとすると、「Your Ruby version is 3.0.6, but your Gemfile specified 2.7.7」というエラーが発生しました。対処方法は以下のとおりです。

Gemfile
ruby '3.0.6'
ターミナル
bundle install

ターミナルが以下の状態でフリーズしました。
※4時間放置しても動きがありませんでした。

ターミナル
Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies...

bundlerを以下のコードで最新の状態にしました。

ターミナル
gem update bundler

#「gem update bundler」の結果を表示
gem list bundler

- ** LOCAL GEMS ***

bundler (2.5.3, 2.4.13)
bundler-unload (1.0.2)
rubygems-bundler (1.4.5)

#bundler」のバージョンを確認
bundle -v
Bundler version 2.4.13

bundlerのdefaultを2.4.13から2.5.3に更新することはできませんでした。
※更新方法がわかる方は、コメントで教えてください。

したがって、bundle installを実行する際は、以下のコードを実行します。

ターミナル
bundle _2.5.3_ install

以下のエラーが発生しました。

ターミナル
An error occurred while installing mysql2 (0.5.5), and
Bundler cannot continue.

In Gemfile:
  mysql2

上記のエラーの対処方法は、以下のとおりです。

ターミナル
#mysqlのインストール
brew install mysql

#zstdのインストール
brew install zstd

#システムがZstandardライブラリの場所を認識できるように、DYLD_LIBRARY_PATH環境変数にライブラリのパスを追加
export DYLD_LIBRARY_PATH=/opt/homebrew/lib:$DYLD_LIBRARY_PATH

「bundle 2.5.3 install」実行後に、以下のエラーが発生しました。

ターミナル
bundle _2.5.3_ install

Ignoring mysql2-0.5.5 because its extensions are not built. Try: gem pristine mysql2 --version 0.5.5
Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies...
Using thor 1.3.0 (was 1.2.1)
Using tzinfo 2.0.6 (was 1.2.11)
Using websocket-driver 0.7.6 (was 0.7.5)
Using activesupport 6.1.7.6 (was 6.0.6.1)
Using activemodel 6.1.7.6 (was 6.0.6.1)
Using actionview 6.1.7.6 (was 6.0.6.1)
Using activerecord 6.1.7.6 (was 6.0.6.1)
Using activejob 6.1.7.6 (was 6.0.6.1)
Using actionpack 6.1.7.6 (was 6.0.6.1)
Using actioncable 6.1.7.6 (was 6.0.6.1)
Using activestorage 6.1.7.6 (was 6.0.6.1)
Using actionmailer 6.1.7.6 (was 6.0.6.1)
Using actionmailbox 6.1.7.6 (was 6.0.6.1)
Using actiontext 6.1.7.6 (was 6.0.6.1)
Using railties 6.1.7.6 (was 6.0.6.1)
Using rails 6.1.7.6 (was 6.0.6.1)
Installing mysql2 0.5.5 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/Users/k/.rvm/gems/ruby-3.0.6/gems/mysql2-0.5.5/ext/mysql2
/Users/k/.rvm/rubies/ruby-3.0.6/bin/ruby extconf.rb
--with-opt-dir\=/path/to/zstd
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_gc_mark_movable()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_enc_interned_str() in ruby.h... yes
-----
Using --with-openssl-dir=/opt/homebrew/opt/openssl@1.1
-----
-----
Using mysql_config at /opt/homebrew/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_DEFAULT_AUTH in mysql.h... yes
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 /opt/homebrew/Cellar/mysql/8.2.0_1/lib
-----
creating Makefile

current directory:
/Users/k/.rvm/gems/ruby-3.0.6/gems/mysql2-0.5.5/ext/mysql2
make DESTDIR\= sitearchdir\=./.gem.20231226-68033-66nggl
sitelibdir\=./.gem.20231226-68033-66nggl clean

current directory:
/Users/k/.rvm/gems/ruby-3.0.6/gems/mysql2-0.5.5/ext/mysql2
make DESTDIR\= sitearchdir\=./.gem.20231226-68033-66nggl
sitelibdir\=./.gem.20231226-68033-66nggl
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/path/to/zstd/lib'
ld: library not found for -lzstd
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/k.rvm/gems/ruby-3.0.6/gems/mysql2-0.5.5 for
inspection.
Results logged to

#省略#

An error occurred while installing mysql2 (0.5.5), and
Bundler cannot continue.

In Gemfile:
  mysql2

以下のコードを実行することで、拡張機能を修正しました。

ターミナル
gem pristine mysql2 --version 0.5.5

再度「bundle 2.5.3 install」を実行すると、以下のとおりbundle installが成功します。

ターミナル
bundle _2.5.3_ install

Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies...
Using thor 1.3.0 (was 1.2.1)
Fetching hashie 5.0.0
Using tzinfo 2.0.6 (was 1.2.11)
Using websocket-driver 0.7.6 (was 0.7.5)
Using activesupport 6.1.7.6 (was 6.0.6.1)
Using activemodel 6.1.7.6 (was 6.0.6.1)
Using actionview 6.1.7.6 (was 6.0.6.1)
Using activejob 6.1.7.6 (was 6.0.6.1)
Using activerecord 6.1.7.6 (was 6.0.6.1)
Using actionpack 6.1.7.6 (was 6.0.6.1)
Using actioncable 6.1.7.6 (was 6.0.6.1)
Using activestorage 6.1.7.6 (was 6.0.6.1)
Using actionmailer 6.1.7.6 (was 6.0.6.1)
Using actionmailbox 6.1.7.6 (was 6.0.6.1)
Using actiontext 6.1.7.6 (was 6.0.6.1)
Using railties 6.1.7.6 (was 6.0.6.1)
Using rails 6.1.7.6 (was 6.0.6.1)
Fetching devise_token_auth 1.2.2
Installing hashie 5.0.0
Installing devise_token_auth 1.2.2
Fetching omniauth 2.1.1
Installing omniauth 2.1.1
Bundle complete! 33 Gemfile dependencies, 138 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

次いで以下のコードを実行し、RubyとRailsのアップデートを完了しました

ターミナル
bundle update rails

サーバー立ち上げ後のエラー解消

「rails s」を実行した後、以下のエラーが発生しました。rmagickとImageMagickの互換性が合っていないと判断しました。

ターミナル
There was an error while trying to load the gem 'rmagick'. (Bundler::GemRequireError)
Gem Load Error is: This installation of RMagick was configured with ImageMagick 6.9.12 but ImageMagick 6.9.13-2 is in use.
Please re-install RMagick to correct the issue.

一旦、以下のコードでrmagickを再インストールしました。

ターミナル
gem uninstall rmagick
bundle install

ついで、「convert --version」を実行して、ImageMagicのバージョンを確認しました。

ところが「zsh: command not found: convert」というエラーが発生しました。

これは、ImageMagicがそもそもインストールされていない場合のエラーです。

よって、以下のコードでImageMagicをインストールしました。

ターミナル
brew install imagemagick

再度「convert --version」を実行した結果、無事にImageMagickがインストールできました。

ターミナル
convert --version

Version: ImageMagick 7.1.1-24 Q16-HDRI aarch64 21856 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP(5.0) 
Delegates (built-in): bzlib fontconfig freetype gslib heic jng jp2 jpeg jxl lcms lqr ltdl lzma openexr png ps raw tiff webp xml zlib zstd
Compiler: gcc (4.2)

最後に「rails s」を実行したところ、サーバーは無事に立ち上がりました。

まとめ

Rubyは12月に、Railsは3月に最新のバージョンアップ情報が公開されます。
それらのタイミングでこまめにアプリをバージョンアップしていった方が、後々の大きなトラブルを回避できると思います。
今回はStatus(Rails Maintenance Plicy)の「EOL」がバージョンアップの動機となりましたが、最新のバージョンアップ情報が公開されるタイミングでバージョンアップを検討していきます。

0
0
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
0