画像編集する機能が必要になったため、Railsで使えるgemを探すと、RMagickが便利そうだったので、導入の流れを紹介します。
導入にいたっては、gemインストール時のエラー対応でかなりハマってしまったので、筆者と同様の環境下の方であれば参考になると思います。

本記事での利用環境
Rails 6.0
ruby 2.6.5
Amazon Linux AMI(AWSのCloud9を利用)

インストール対象
ImageMagick 6.9
rmagick 4.2.2

まず、RMagickについて知りたい方は、下記チェックしてみてください。

例えば、画像の切り抜き方法だと下記に記載があります。

ImageMagickをインストールする

まずImageMagickのインストールが必要です。
インストール方法は、利用している環境によって異なります。
筆者の環境と同じcloud9であれば、ターミナルから下記コマンドでインストールできます。

ターミナル

sudo yum -y install ImageMagick ImageMagick-devel

ImageMagickとImageMagick-develともにインストール必要です。
正常にインストール完了したかどうか、下記のコマンドで確認できます。

ターミナル

convert -version

参考までに、筆者の環境下での出力結果は下記の通りです。

ターミナル

Version: ImageMagick 6.9.10-68 Q16 x86_64 2021-02-03 https://imagemagick.org
Copyright: © 1999-2019 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP(3.1) 
Delegates (built-in): bzlib fontconfig freetype gslib jng jpeg lcms ltdl lzma png ps tiff wmf x xml zlib

もし、やっぱりImageMagickいらない、となれば下記のコマンドで削除することも可能です。

ターミナル

sudo yum -y remove ImageMagick
sudo yum -y remove ImageMagick-devel

gem ‘rmagick’を追加してbundle installする

ImageMagickインストール後、gem ‘rmagick’を追加してbundle installします。

gem 'rmagick'

ターミナル

bundle install

エラーもなく、無事インストールできれば、以上で完了です。
筆者の場合は、エラーが発生したため、これからエラーを解決していきます。

エラーを確認&修正

bundle installすると、下記のエラーメッセージが表示されました。

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

    current directory: /home/ec2-user/.rvm/gems/ruby-2.6.5/gems/rmagick-4.2.2/ext/RMagick
/home/ec2-user/.rvm/rubies/ruby-2.6.5/bin/ruby -I /home/ec2-user/.rvm/rubies/ruby-2.6.5/lib/ruby/site_ruby/2.6.0 -r ./siteconf20210806-10706-10mubyf.rb extconf.rb
checking for brew... no
checking for pacman... no
checking for Ruby version >= 2.3.0... yes
checking for pkg-config... yes
checking for outdated ImageMagick version (<= 6.7.7)... no
checking for gcc... yes
checking for __GNUC__... *** 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=/home/ec2-user/.rvm/rubies/ruby-2.6.5/bin/$(RUBY_BASE_NAME)
/home/ec2-user/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/mkmf.rb:467:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
        from /home/ec2-user/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/mkmf.rb:585:in `block in try_compile'
        from /home/ec2-user/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/mkmf.rb:534:in `with_werror'
        from /home/ec2-user/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/mkmf.rb:585:in `try_compile'
        from /home/ec2-user/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/mkmf.rb:852:in `macro_defined?'
        from /home/ec2-user/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/mkmf.rb:994:in `block in have_macro'
        from /home/ec2-user/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/mkmf.rb:959:in `block in checking_for'
        from /home/ec2-user/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/mkmf.rb:361:in `block (2 levels) in postpone'
        from /home/ec2-user/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/mkmf.rb:331:in `open'
        from /home/ec2-user/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/mkmf.rb:361:in `block in postpone'
        from /home/ec2-user/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/mkmf.rb:331:in `open'
        from /home/ec2-user/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/mkmf.rb:357:in `postpone'
        from /home/ec2-user/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/mkmf.rb:958:in `checking_for'
        from /home/ec2-user/.rvm/rubies/ruby-2.6.5/lib/ruby/2.6.0/mkmf.rb:993:in `have_macro'
        from extconf.rb:99:in `configure_compile_options'
        from extconf.rb:19:in `initialize'
        from extconf.rb:398:in `new'
        from extconf.rb:398:in `<main>'

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

  /home/ec2-user/.rvm/gems/ruby-2.6.5/extensions/x86_64-linux/2.6.0/rmagick-4.2.2/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /home/ec2-user/.rvm/gems/ruby-2.6.5/gems/rmagick-4.2.2 for inspection.
Results logged to /home/ec2-user/.rvm/gems/ruby-2.6.5/extensions/x86_64-linux/2.6.0/rmagick-4.2.2/gem_make.out

ゴニョゴニョ書かれてますが、15行目に”Check the mkmf.log file for more details”と記載があり、52行目に格納先の記載があるため、開いてみます。

"gcc -o conftest -I/home/ec2-user/.rvm/rubies/ruby-2.6.5/include/ruby-2.6.0/x86_64-linux -I/home/ec2-user/.rvm/rubies/ruby-2.6.5/include/ruby-2.6.0/ruby/backward -I/home/ec2-user/.rvm/rubies/ruby-2.6.5/include/ruby-2.6.0 -I.  -fopenmp -DMAGICKCORE_HDRI_ENABLE=0 -DMAGICKCORE_QUANTUM_DEPTH=16 -I/usr/include/ImageMagick-6    -fopenmp -DMAGICKCORE_HDRI_ENABLE=0 -DMAGICKCORE_QUANTUM_DEPTH=16 -I/usr/include/ImageMagick-6   conftest.c  -L. -L/home/ec2-user/.rvm/rubies/ruby-2.6.5/lib -Wl,-rpath,/home/ec2-user/.rvm/rubies/ruby-2.6.5/lib  -lMagickCore-6.Q16      -lMagickCore-6.Q16    -Wl,-rpath,'/../lib' -Wl,-rpath,'/../lib' -lruby  -lm   -lc"
gcc: error: libgomp.spec: No such file or directory
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: int main(int argc, char **argv)
4: {
5:   return 0;
6: }
/* end */

2行目に記載があるように、”gcc: error: libgomp.spec: No such file or directory”が原因のようです。
エラー解決方法についてgoogleさんに聞いてみると、下記の記事ででているエラーと同じ事象っぽいです。

さらに調べていると、下記の記事もヒットしました。

つまり、下記ディレクトリに該当のファイルが見当たらないのが原因のようです。
/usr/lib64/libgomp.spec
/usr/lib64/libgomp.a
/usr/lib64/libgomp.so

ということで、同じようなエラーが出た場合はシンボリックリンクを貼ることで解決できます。

※注意事項※
/usr/lib/gcc/x86_64-amazon-linux/4.8.5/ファイル名 の太字箇所は、ご自身の環境に合わせてください。Linuxであれば、”cd..”や”ls”を駆使して該当のファイル名がどこにあるか探す必要があります。(筆者はLinux初心者のため、もっと良い探し方があるかもしれませんが、、)

ターミナル

sudo ln -s /usr/lib/gcc/x86_64-amazon-linux/4.8.5/libgomp.spec /usr/lib64/libgomp.spec

ターミナル

sudo ln -s /usr/lib/gcc/x86_64-amazon-linux/4.8.5/libgomp.a /usr/lib64/libgomp.a

ターミナル

sudo ln -s /usr/lib64/libgomp.so.1.0.0 /usr/lib64/libgomp.so

もしシンボリックリンクを誤って貼ってしまった場合は、ファイルを削除してもう一度やり直してください。

ターミナル(カレントディレクトリを /usr/lib64/へ移動してから下記コマンドで削除)

sudo rm libgomp.spec

再度bundle installする

これまでの流れでエラー解決できたはずなので、再度bundle installを行います。

ターミナル

bundle install

エラーもなくgemが追加されればOKです。
念の為、gemfile.lockに”rmagick”が追加されているか確認しましょう。
これでRMagickが使えるようになります。
お疲れ様でした。

ちなみに、本番環境においても同様にImageMagickをインストールして、シンボリックリンクを反映させる必要があります。
本番環境へ事前準備なしでデプロイすると、案の定エラー発生しました。
お気をつけください。