How to set up ruby on Docker - getting nokogiri error - ruby

My Doc Dockerfile has FROM Ubuntu:18.04
I want to use ruby so i did
gem install bundler
However when I try to
bundle
I get an error with nokogiri
Installing nokogiri 1.10.2 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /var/lib/gems/2.5.0/gems/nokogiri-1.10.2/ext/nokogiri
/usr/bin/ruby2.5 -r ./siteconf20190414-288-1mfprtc.rb extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/include/ruby.h
extconf failed, exit code 1
Gem files will remain installed in /var/lib/gems/2.5.0/gems/nokogiri-1.10.2 for inspection.
Results logged to /var/lib/gems/2.5.0/extensions/x86_64-linux/2.5.0/nokogiri-1.10.2/gem_make.out
An error occurred while installing nokogiri (1.10.2), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.10.2' --source 'http://rubygems.org/'` succeeds before bundling.

You have a dependency called nokogiri that has external dependency, usually it's libxml and libxslt.
All you need to do is apt get install libxml2-dev libxslt1-dev
Here's a working example of a Ruby Dockerfile
FROM ruby:2.6.2
RUN apt-get update -qq && apt-get install -y build-essential
RUN apt-get install -y libxml2-dev libxslt1-dev
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY Gemfile* /usr/src/app/
RUN bundle install -j3
ADD . /usr/src/app
ENTRYPOINT ["bundle", "exec"]
CMD ["rails", "server"]

Related

You don't have write permissions for the /usr/lib/ruby/gems/2.7.0 directory (Alpine linux docker image)

I can not install ruby's gems on alpine docker image. I've tried different approaches from other questions to solve ERROR: While executing gem ... (Gem::FilePermissionError), but there are solutions either for Ubuntu or for Mac OS.
Part of docker file code:
RUN set -ex \
&& apk add --no-cache --update ruby ruby-dev ruby-bundler \
&& gem install --no-document --source https://rubygems.org --version 3.6.6 inspec
OUTPUT:
+ apk add --no-cache --update ruby ruby-dev ruby-bundler
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/community/x86_64/APKINDEX.tar.gz
(1/11) Installing yaml (0.2.5-r0)
(2/11) Installing ruby-libs (2.7.3-r1)
(3/11) Installing ruby (2.7.3-r1)
(4/11) Installing ruby-etc (2.7.3-r1)
(5/11) Installing ruby-io-console (2.7.3-r1)
(6/11) Installing ruby-bundler (2.2.20-r0)
(7/11) Installing libgmpxx (6.2.1-r0)
(8/11) Installing gmp-dev (6.2.1-r0)
(9/11) Installing libucontext (1.1-r0)
(10/11) Installing libucontext-dev (1.1-r0)
(11/11) Installing ruby-dev (2.7.3-r1)
Executing busybox-1.33.1-r2.trigger
Executing glibc-bin-2.33-r0.trigger
/usr/glibc-compat/sbin/ldconfig: /usr/glibc-compat/lib/ld-linux-x86-64.so.2 is not a symbolic link
OK: 1409 MiB in 141 packages
+ gem install --no-document --source https://rubygems.org --version 3.6.6 inspec
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /usr/lib/ruby/gems/2.7.0 directory.
The command '/bin/sh -c set -ex && apk add --no-cache --update ruby ruby-dev ruby-bundler && gem install --no-document --source https://rubygems.org --version 3.6.6 inspec' returned a non-zero code: 1 ```
This seems to be a problem with docker versions earlier than 20.10.4, and at the time of writing this, DockerHub runs an older version.
Option 1:
If you have control over the version of your docker engine, upgrade it to the latest version (comments suggest at least version 20.10.4).
Option 2:
Use FROM alpine:3.13.
With the latest alpine (3.14) it breaks.
Option 3:
Use the official ruby alpine image. I tested with FROM ruby:3-alpine.
References:
The issue opened on alpine/aports
The relevant release notes section for alpine 3.14

Cannot install openssl-dev for docker ruby alpine

My Dockerfile
FROM ruby:2.6.3-alpine
RUN apk add --no-cache build-base tzdata postgresql-dev postgresql-client
# throw errors if Gemfile has been modified since Gemfile.lock
RUN bundle config --global frozen 1
ENV APP_PATH /app
RUN mkdir -p $APP_PATH
WORKDIR $APP_PATH
COPY Gemfile $APP_PATH
COPY Gemfile.lock $APP_PATH
RUN bundle install
COPY . $APP_PATH
EXPOSE 3000
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]
I notice installing postgresql-dev will cause installing openssl-dev.
Anyway, run docker-compose up will output
$ docker-compose up
Building sidekiq
Step 1/12 : FROM ruby:2.6.3-alpine
---> 6ddb199f039f
Step 2/12 : RUN apk add --no-cache build-base tzdata postgresql-dev postgresql-client bash
---> Running in 386a4486295f
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/community/x86_64/APKINDEX.tar.gz
(1/28) Upgrading libcrypto1.1 (1.1.1c-r0 -> 1.1.1d-r2)
(2/28) Upgrading libssl1.1 (1.1.1c-r0 -> 1.1.1d-r2)
(3/28) Installing bash (5.0.0-r0)
Executing bash-5.0.0-r0.post-install
(4/28) Installing binutils (2.32-r0)
(5/28) Installing libmagic (5.37-r1)
(6/28) Installing file (5.37-r1)
(7/28) Installing isl (0.18-r0)
(8/28) Installing libgomp (8.3.0-r0)
(9/28) Installing libatomic (8.3.0-r0)
(10/28) Installing mpfr3 (3.1.5-r1)
(11/28) Installing mpc1 (1.1.0-r0)
(12/28) Installing gcc (8.3.0-r0)
(13/28) Installing musl-dev (1.1.22-r3)
(14/28) Installing libc-dev (0.7.1-r0)
(15/28) Installing g++ (8.3.0-r0)
(16/28) Installing make (4.2.1-r2)
(17/28) Installing fortify-headers (1.1-r0)
(18/28) Installing build-base (0.5-r1)
(19/28) Installing libedit (20190324.3.1-r0)
(20/28) Installing db (5.3.28-r1)
(21/28) Installing libsasl (2.1.27-r4)
(22/28) Installing libldap (2.4.48-r0)
(23/28) Installing libpq (11.6-r0)
(24/28) Installing postgresql-client (11.6-r0)
(25/28) Installing openssl-dev (1.1.1d-r2)
in ubuntu, it is forever frozen for step (25/28) Installing openssl-dev (1.1.1d-r2).
In mac, it shows
(25/27) Installing postgresql-libs (11.6-r0)
ERROR: Failed to create usr/lib/libssl.a: No error information
ERROR: openssl-dev-1.1.1d-r2: BAD signature
Is there anything wrong? Also, how can I get a bit more detailed info/log why it cannot install openssl-dev?

Docker - ruby, nokogiri - An error occurred while installing nokogiri (1.10.2),

Using docker and trying to build an image but I get the above error when I bundle due to the nokogiri gem issue
My Docker file is
RUN apt update
RUN apt install -y ruby
RUN gem install bundler
RUN cd /usr/src/app
WORKDIR /web
ADD Gemfile /web/
RUN bundle
However, when I RUN bundle at the end ^ I get the following error:
...
Fetching mini_portile2 2.4.0
Installing mini_portile2 2.4.0
Fetching nokogiri 1.10.2
Installing nokogiri 1.10.2 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /var/lib/gems/2.5.0/gems/nokogiri-1.10.2/ext/nokogiri
/usr/bin/ruby2.5 -r ./siteconf20190414-8-1rrc2i8.rb extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/include/ruby.h
extconf failed, exit code 1
Gem files will remain installed in /var/lib/gems/2.5.0/gems/nokogiri-1.10.2 for
inspection.
Results logged to
/var/lib/gems/2.5.0/extensions/x86_64-linux/2.5.0/nokogiri-1.10.2/gem_make.out
An error occurred while installing nokogiri (1.10.2), and Bundler cannot
continue.
Make sure that `gem install nokogiri -v '1.10.2' --source
'http://rubygems.org/'` succeeds before bundling.
In Gemfile:
capybara was resolved to 3.16.2, which depends on
xpath was resolved to 3.2.0, which depends on
nokogiri
The command '/bin/sh -c bundle' returned a non-zero code: 5
Add the following line to the Dockerfile, it will handle the dependency
RUN apt install -y build-essential patch ruby-dev zlib1g-dev liblzma-dev
just before the RUN bundle command
and then run the build again
docker build -t dock .
...
Fetching nokogiri 1.10.2
Installing nokogiri 1.10.2 with native extensions
Fetching rack 2.0.7
Installing rack 2.0.7
...
Bundle complete! 7 Gemfile dependencies, 33 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
Removing intermediate container 146ce309d0a5
---> cd36c7c1e577
Successfully built cd36c7c1e577
Successfully tagged dock:latest
...
Thanks to https://nokogiri.org/tutorials/installing_nokogiri.html for this find.
For alpine
RUN set -x \ && apk upgrade --no-cache \ && apk add --no-cache
--virtual build-dependencies \
build-base \ && apk add --no-cache \
libxml2-dev \
libxslt-dev \ && gem install nokogiri \
-- --use-system-libraries \
--with-xml2-config=/usr/bin/xml2-config \
--with-xslt-config=/usr/bin/xslt-config \ && apk del build-dependencies

Error on installing gem Nokogiri-1.8.4

OS: AMI 2018.03.0 (HVM)
Ruby version: 2.3.7p456 (2018-03-28 revision 63024) [x86_64-linux-gnu] - installed using yum install ruby23
gem install nokogiri throws:
Building native extensions. This could take a while...
ERROR: Error installing nokogiri:
ERROR: Failed to build gem native extension.
current directory: /home/ec2-user/.gem/ruby/2.3/gems/nokogiri-
1.8.4/ext/nokogiri
/usr/bin/ruby2.3 -r ./siteconf20180801-6218-101te4s.rb extconf.rb
mkmf.rb can't find header files for ruby at
/usr/share/ruby/include/ruby.h
extconf failed, exit code 1
Gem files will remain installed in /home/ec2-
user/.gem/ruby/2.3/gems/nokogiri-1.8.4 for inspection.
Results logged to /home/ec2-user/.gem/ruby/2.3/extensions/x86_64-
linux/2.3/nokogiri-1.8.4/gem_make.out
what am i missing here?
From the error output, it would seem it can't find the development dependencies for Ruby, which Nokogiri is trying to use in order to build the gem natively.
You can run yum -y install gcc ruby-devel rubygems to make certain the dependencies are installed.
Basing my answer somewhat from https://stackoverflow.com/a/4502672/1129980
Update
Tested this now on a new EC2 instance:
yum install -y ruby23-devel gcc libxml2 libxml2-devel libxslt libxslt-devel patch
gem install nokogiri --no-ri --no-rdoc
That works :)
Try running the below mentioned commands
sudo yum -y install ruby-dev ( or ruby-devel)
sudo yum -y install libgmp-dev libgmpxx4ldbl ruby-dev ruby2.3-dev

json gem install error on bundle install

When I try to do a bundle install I get the following json error. The bundle install does not complete. What gives?
Errno::EACCES: Permission denied - /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/json-1.7.5/.gitignore
An error occurred while installing json (1.7.5), and Bundler cannot continue.
Make sure that `gem install json -v '1.7.5'` succeeds before bundling.
>>$ gem install json -v '1.7.5'
ERROR: While executing gem ... (Errno::EACCES)
Permission denied - /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/json-1.7.5/.gitignore
EDIT: The sudo install command gives the following error:
>>$ sudo gem install json -v '1.7.5'
Building native extensions. This could take a while...
ERROR: Error installing json:
ERROR: Failed to build gem native extension.
/usr/local/Cellar/ruby/1.9.3-p194/bin/ruby extconf.rb
creating Makefile
make
sh: make: command not found
Gem files will remain installed in /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/json-1.7.5 for inspection.
Results logged to /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/json-1.7.5/ext/json/ext/generator/gem_make.out
you need to install ruby1.8-dev and not just ruby1.8, you are missing the development headers.
if on OSX:
sudo install ruby1.8-dev
if on unix
sudo aptitude install ruby1.8-dev
or
sudo apt-get install ruby1.8-dev
go into your directory with rubygems source and run:
sudo ruby setup.rb
after that run:
if on OSX:
sudo gem install json -v '1.7.5'
some unix system may need gem version command to work properly so you should run:
sudo gem1.8 install json -v '1.7.5'
Try using this script
sudo chown -R $USER /usr/local
Found the answer here: "sh: make: command not found" when running "$ bundle" after adding redcarpet gem to Rails app Needed to upgrade command line tools after installing Mountain Lion

Resources