LoginSignup
14
8

More than 5 years have passed since last update.

RMagickのインストールで奈落に落ちた

Last updated at Posted at 2018-05-05

背景

GWもそろそろ終わりですね。昨日僕はのんびり部屋掃除をしていました。その時、ふと思ったことがあります。

「Ruby書きたい」

「画像処理してみたい」

もともとRubyは基礎の基礎を勉強中で、同時に画像処理にも少し興味があったため、どうせなら両方同時に!!GWだし!!(?)ということで前から気になっていたRMagickを使ってみることにしました。

…とそううまくはいかず、タイトルの通り導入の段階でエラーを吐いたので今回はどう直したかを書いておきます。

環境:Ubuntu 16.04 LTS
Ruby:2.4.1

いざ

RMagickのインストールにはImageMagickが入っていることが前提となるため、まずはImageMagickがあるかどうかを調べます。

~$ convert -version
Version: ImageMagick 6.8.9-9 Q16 x86_64 2017-07-31 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules OpenMP
Delegates: bzlib cairo djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png rsvg tiff wmf x xml zlib

convertはImageMagickでフォーマットの変換などを行うコマンドです。続けて-versionと打つと、インストールされているならばバージョン情報が出てきます。調べると、RmagickはImageMagickがバージョン7以降になると使えないらしく、自分のPCでは既にバージョン6のものが入っていました。ラッキーですね。プリインストールされてたのかなんなのかは不明ですが。ではこのままRMagickをサクッと入れてしまいましょう。

長い戦いの始まり

念の為gem updateを行ってから。

~$ gem install rmagick
Fetching: rmagick-2.16.0.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing rmagick:
    ERROR: Failed to build gem native extension.

    current directory: /home/username/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rmagick-2.16.0/ext/RMagick
/home/username/.rbenv/versions/2.4.1/bin/ruby -r ./siteconf20180504-18151-h5fr0a.rb extconf.rb
checking for gcc... yes
checking for Magick-config... no
checking for pkg-config... yes
Package MagickCore was not found in the pkg-config search path.
Perhaps you should add the directory containing `MagickCore.pc'
to the PKG_CONFIG_PATH environment variable
No package 'MagickCore' found
checking for outdated ImageMagick version (<= 6.4.9)... *** 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/username/.rbenv/versions/2.4.1/bin/$(RUBY_BASE_NAME)

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

  /home/username/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/extensions/x86_64-linux/2.4.0-static/rmagick-2.16.0/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /home/username/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rmagick-2.16.0 for inspection.
Results logged to /home/username/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/extensions/x86_64-linux/2.4.0-static/rmagick-2.16.0/gem_make.out

エラーかい。注目したのはここです。

Package MagickCore was not found in the pkg-config search path.
Perhaps you should add the directory containing `MagickCore.pc'
to the PKG_CONFIG_PATH environment variable
No package 'MagickCore' found

pkg-configの中にMagicCore.pcがないぞ。環境変数PKG_CONFIG_PATHに通しとけよ。ということなので素直にexportで解決…と思いきや、

~$ find /usr/lib/pkgconfig MagickCore.pc
/usr/lib/pkgconfig
/usr/lib/pkgconfig/xxxxxx.pc
/usr/lib/pkgconfig/yyyyyy.pc
/usr/lib/pkgconfig/zzzzzz.pc
find: `MagickCore.pc': そのようなファイルやディレクトリはありません

export PKG_CONFIG_PATH=/usr/lib/pkgconfigしても状況が変わらないため、findかけたらそんなものはないと。ここらへんから何をしていいかわからなくなりましたね。なんせ環境変数とかパスとか全くわかってない…。ので実はこの記事にもたくさんの「なんもわかってないね」が飛んでくると思いますが、なにもわかってないので色々意見や知識などを下さい。

悪あがきした

ここは特に読まなくて結構です。インターネットの海をわたっても自分と同じ症状の人が見つからないので(ImageMagickがインストールされている時点でMagickCore.pc自体はある場合がほとんどだった)色々試しました。全部失敗してます。

  • ImageMagickをアンインストール→再インストール(brew)

    • 状況は変わらず。後々、brewでインストールするとバージョン7が入るかもということやlinuxbrew側でエラーが起きたりしててんやわんや。
  • apt-getでもう一度やってみる

    • これも変わらず。
  • pkg-configをapt-getで再インストール

    • 迷走が止まらない。変わらず。

たどりついた答え

~$ sudo apt-get install aptitude
~$ sudo aptitude install libimagemagick libmagickcore libmagickcore-dev libmagickwand-dev

aptitudeをインストールして(デフォルトで入ってないのか)関連ライブラリや推奨ライブラリをまとめてインストールしました。

~$ gem install rmagick
Building native extensions.  This could take a while...
Successfully installed rmagick-2.16.0
1 gem installed
~$

で  き  た

ImageMagickがプリインストールされているならその他必要なものもあるやろーと思ってたらここが甘かったようですね。関連ライブラリのことを全く考えていなかったです。

色々知識のないまま試行錯誤してしまった記録を綴ったものなのでツッコミどころは多々あると思います。今回のエラーの原因やこうするといいよーと言うアドバイス、説明など何でも受け付けているのでコメントなどよろしくお願いします!最後まで読んでいただきありがとうございました!

参考にしたもの

14
8
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
14
8