LoginSignup
33
25

More than 3 years have passed since last update.

RailsでRMagickを使う

Posted at

RMagickとは

rubyで画像処理(加工)を扱いたい場合に使用する。
例えば、画像を拡大縮小、文字の挿入、エフェクトなど。

Railsで導入してみる

開発環境
mac
ruby:2.5.0
rails:5.2.3

Gemfile
gem 'carrierwave'
gem 'rmagick'

# bundle install実行

おそらく下記のようなエラーになる。

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/uenoyuuki/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/rmagick-4.0.0/ext/RMagick
/Users/uenoyuuki/.rbenv/versions/2.5.0/bin/ruby -r ./siteconf20190916-2480-i0d2pg.rb extconf.rb
checking for brew... yes
checking for clang... yes
checking for pkg-config... yes


ERROR: Can't install RMagick 4.0.0. Can't find ImageMagick with pkg-config


*** 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=/Users/uenoyuuki/.rbenv/versions/2.5.0/bin/$(RUBY_BASE_NAME)

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

  /Users/uenoyuuki/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-18/2.5.0-static/rmagick-4.0.0/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/uenoyuuki/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/rmagick-4.0.0 for inspection.
Results logged to /Users/uenoyuuki/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-18/2.5.0-static/rmagick-4.0.0/gem_make.out

An error occurred while installing rmagick (4.0.0), and Bundler cannot continue.
Make sure that `gem install rmagick -v '4.0.0' --source 'https://rubygems.org/'` succeeds before bundling.

bundle installの前にgem install rmagick -v '4.0.0'を試せ!
てきなことが書いてあるので、実行。
上記と似たようなエラーがでました笑

解決法

以下のことを実行する必要があるようです。
1.Homebrew で ImageMagick 6 と pkg-config をインストール。
2.環境変数 PKG_CONFIG_PATH を指定
3.gem コマンドで rmagick をインストール

順に見ていきます。

まずはじめに、ImageMagick 6 と pkg-configをインストールします。
以下を実行。

$ brew install imagemagick@6
$ brew install pkg-config

次に、環境変数を指定します。
PATHの指定ですが、$ brew install imagemagick@6コマンドの入力に成功した場合、下の方に、以下のような説明が表示されるかと思います。

If you need to have imagemagick@6 first in your PATH run:
  echo 'export PATH="/usr/local/opt/imagemagick@6/bin:$PATH"' >> ~/.bash_profile

For compilers to find imagemagick@6 you may need to set:
  export LDFLAGS="-L/usr/local/opt/imagemagick@6/lib"
  export CPPFLAGS="-I/usr/local/opt/imagemagick@6/include"

For pkg-config to find imagemagick@6 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/imagemagick@6/lib/pkgconfig"

これの一番最後の部分をそのまま実行してPATHを通します。以下を実行。

$ export PKG_CONFIG_PATH=/usr/local/opt/imagemagick@6/lib/pkgconfig

最後に、gem コマンドで rmagick をインストールします。

$ gem install rmagick

これで準備が整いましたので、繰り返しになりますが、
Gemfile以下のようにして、bundle install実行。

Gemfile
gem 'carrierwave'
gem 'rmagick'

# bundle install実行

これで終了です。
ありがとうございました!

33
25
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
33
25