docker-alpine: Unable to install ruby gem 'nokogiri'

Hi, I am using base image of gliderlabs/alpine:3.1, and found that not able to instal ruby gems nokogiri.

My Dockerfile

RUN apk add --update \
  bash \
  ca-certificates \
  libxml2 \
  libxslt \
  gcc \
  ruby \
  ruby-bundler \
  ruby-dev \
  nodejs \
  mysql-client \
  imagemagick \
  nginx \
  && rm -rf /var/cache/apk/* \
  && adduser -D app \
  && gem install bundler --no-document \
  && gem install rails nokogiri \
  && npm install -g react-tools babel

As I want to install nokogiri, following error shows:

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

    /usr/bin/ruby extconf.rb
 Building nokogiri using packaged libraries.
 -----
 libiconv is missing.  please visit http://nokogiri.org/tutorials/installing_nokogiri.html for help with installing dependencies.
 -----
*** 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/bin/ruby
    --help
    --clean
    --use-system-libraries
    --enable-static
    --disable-static
    --with-zlib-dir
    --without-zlib-dir
    --with-zlib-include
    --without-zlib-include=${zlib-dir}/include
    --with-zlib-lib
    --without-zlib-lib=${zlib-dir}/lib
    --enable-cross-build
    --disable-cross-build

 extconf failed, exit code 1

Gem files will remain installed in /home/app/webapp/vendor/bundle/ruby/2.1.0/gems/nokogiri-1.6.3.1 for inspection.
Results logged to /home/app/webapp/vendor/bundle/ruby/2.1.0/extensions/x86_64-linux/2.1.0/nokogiri-1.6.3.1/gem_make.out
An error occurred while installing nokogiri (1.6.3.1), and Bundler cannot
continue.

Is there any way to avoid this?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 2
  • Comments: 22 (2 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks @joffotron and @mattaitchison, this fixed it for me:

FROM ruby:2.3.0-alpine
# ...
RUN apk add --update \
  build-base \
  libxml2-dev \
  libxslt-dev \
  postgresql-dev \
  && rm -rf /var/cache/apk/*

# Use libxml2, libxslt a packages from alpine for building nokogiri
RUN bundle config build.nokogiri --use-system-libraries

Adding libxml2-dev libxslt-dev to apk add fixed it for me…

nokogiri now bundles its own libxml2, which doesn’t compile on alpine (looks like a pthreads thing maybe?). You can get it to use the libxml2(-dev) package on Alpine by:

gem install nokogiri --use-system-libraries

or

bundle config build.nokogiri --use-system-libraries
bundle install

Hopefully this helps someone else looking for help.

As of nokogiri 1.6.8, the --use-system-libraries work-around is unnecessary.

In my case RUN apk update && apk add build-base was enough to install nokogiri 1.8.2 because:

In Nokogiri 1.6.0 and later libxml2 and libxslt are bundled with the gem, …

source: https://github.com/sparklemotion/nokogiri#requirements

Thanks @ruudud my build was actually failing when I used --use-system-libraries and I needed to remove that in order to have succesful build.

This worked for me,

RUN apk add --no-cache build-base

Here is the reference Installing Nokogiri

Cleaner version of Dockerfile for nokogiri.

FROM ruby:2.5-alpine
# ...
RUN apk add --no-cache \
  build-base \
  libxml2-dev \
  libxslt-dev
# ...

Update: @kylev add document for this issue. Refer to https://github.com/gliderlabs/docker-alpine/issues/53#issuecomment-532467114

Hi, (caveat - beginner docker user), I have managed to successfully install nokogiri on alpine 3.2 by using the package name ruby-nokogiri (see packages listing). That is, via Dockerfile:

RUN apk add ruby ruby-nokogiri

maybe that helps?

Building native extensions requires certain system libraries and the build-base package.

You can see how ruby-nokogiri is built and what dependencies are required by looking at its APKBUILD file. View Here

Feel free to reopen this issue if you are still having problems.

I added additional documentation (as @gr1d99 mentioned) in sparklemotion/nokogiri.org#23

This works for me

RUN apk update && apk upgrade

RUN apk add ruby=2.5.2-r0 ruby-bundler ruby-nokogiri ruby-dev build-base libxml2-dev libxslt-dev libffi-dev

Basically

FROM ruby:2.3-alpine

RUN mkdir /usr/src/app \
    && chown $(id -un):$(id -gn) /usr/src/app \
    && apk add --no-cache build-base \
    && gem install bundler

ENV RAILS_ENV=development

COPY . /usr/src/app

RUN cd /usr/src/app \
    && bundle install --no-color

WORKDIR /usr/src/app
EXPOSE 3000
CMD ["bundle", "exec", "rake", "server"]