The installation of gem rmagick fails

The installation of rmgick fails even if you follow the instructions in the official documentation.
Does anyone know how to solve this problem?

official documentation:

ci

rmagick:
    working_directory: ~/app
    docker:
      - image: circleci/ruby:2.5.1-node-browsers
    steps:
      - checkout
      - run: sudo apt-get update && sudo apt-get install python3-pip groff-base
      - run: sudo apt-get install imagemagick libmagickcore-dev libmagickwand-dev
      - restore_cache: &restore_gems_cache
          keys:
            - production-rails-peep-bundle-{{ checksum "Gemfile.lock" }}
            - production-rails-peep-bundle-
      - run: bundle check --path=vendor/bundle || bundle install --path vendor/bundle --jobs 4 --retry 3

The error log is as follows

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

   current directory: /usr/local/bundle/gems/rmagick-4.1.2/ext/RMagick
/usr/local/bin/ruby -r ./siteconf20201109-18-1o9h6zv.rb extconf.rb
checking for brew... no
checking for pacman... no
checking for Ruby version >= 2.3.0... yes
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
Package 'MagickCore', required by 'virtual:world', not found


ERROR: Can't install RMagick 4.1.2.
Can't find the ImageMagick library or one of the dependent libraries.
Check the mkmf.log file for more detailed information.


*** 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=/usr/local/bin/$(RUBY_BASE_NAME)

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

 /usr/local/bundle/extensions/x86_64-linux/2.5.0/rmagick-4.1.2/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /usr/local/bundle/gems/rmagick-4.1.2 for
inspection.
Results logged to
/usr/local/bundle/extensions/x86_64-linux/2.5.0/rmagick-4.1.2/gem_make.out

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

In Gemfile:
 rmagick
The command '/bin/sh -c set -x  && apk update  && apk add --no-cache --virtual build-dependencies      build-base      git      mysql-dev      ca-certificates      openssl  && echo 'gem: --no-rdoc --no-ri' >> /.gemrc  && gem install bundler  && bundle install -j 4  && rm -rf /usr/lib/lib/ruby/gems/*/cache/*  && rm -rf ~/.gem  && apk del build-dependencies  && apk add --no-cache mariadb-client-libs imagemagick' returned a non-zero code: 5

Exited with code exit status 5
CircleCI received exit code 5

Hi @muramura!

Welcome to Discuss and thanks for posting such helpful information!

I did find some posts online of people also running into similar errors when installing (though it seemed like local installs vs. installing in a CI container):

This one too:

There was also a note in the docs you linked to with a similar error and possible workaround of setting the path:

PATH="/usr/lib/x86_64-linux-gnu/ImageMagick-6.8.9/bin-Q16:$PATH"

With all the above said I was trying to replicate the error you were seeing on your builds and was unable to in my own test, so if the above doesn’t get it working for you can you either:

  1. Share a link to a failed build here in this post so I can investigate further (only if you are comfortable posting it)
  2. If you don’t want to publicly share the link, submit a support request and note this post in the submission and include a link to a build. I can then grab that ticket and continue the investigation there

Thank you!
-Nick