GitLab CI failing due to gem load error - ruby

I have a ruby script that fails the GitLab CI build with:
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- git (LoadError)
The ruby script:
require 'git'
Here's the .gitlab-ci.yml file:
image: gitlab/gitlab-ce:latest
job1:
script:
- apt-get update && apt-get -y install ruby && apt-get install git
- gem install git
- ./script.rb
I've also tried adding a gemfile with the git gem and running:
image: gitlab/gitlab-ce:latest
job1:
script:
- apt-get update && apt-get -y install ruby && apt-get install git
- gem install bundler
- bundle install
- ./script.rb
But I get the same error

So it was quite simple.
Needed to sudo the gem install:
image: gitlab/gitlab-ce:latest
job1:
script:
- apt-get update && apt-get -y install ruby && apt-get install git
- sudo gem install git
- ./script.rb

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

gem command not found when install rbenv in debian

I'm creating a Dockerfile to run truffleruby. I'm getting an error when trying to install bundler and foreman. The error is /bin/sh: 1: gem: not found
Dockerfile
FROM debian:buster-slim
# Install packages for building ruby
RUN apt update -y && apt install -y git curl libssl-dev libpq-dev libreadline-dev zlib1g-dev \
autoconf bison build-essential libyaml-dev \
libreadline-dev libncurses5-dev libffi-dev libgdbm-dev
RUN apt clean
# Install rbenv and ruby-build
RUN git clone https://github.com/sstephenson/rbenv.git /root/.rbenv
RUN git clone https://github.com/sstephenson/ruby-build.git /root/.rbenv/plugins/ruby-build
RUN /root/.rbenv/plugins/ruby-build/install.sh
ENV PATH /root/.rbenv/bin:$PATH
RUN echo 'eval "$(rbenv init -)"' >> /etc/profile.d/rbenv.sh # or /etc/profile
RUN echo 'eval "$(rbenv init -)"' >> .bashrc
RUN . ~/.bashrc
RUN rbenv install truffleruby-20.3.0
RUN rbenv global truffleruby-20.3.0
RUN rbenv rehash
ENV BUNDLER_VERSION=2.2.4 NODE_ENV=production RAILS_ENV=production RAILS_SERVE_STATIC_FILES=true RAILS_LOG_TO_STDOUT=true PORT=3000
ENV CONFIGURE_OPTS --disable-install-doc
RUN apt-get install -y curl -sL https://deb.nodesource.com/setup_14.x | bash - && \
apt-get update && apt-get install -y nodejs && \
apt-get clean
RUN rbenv versions
RUN gem install bundler:2.2.4 foreman
RUN mkdir /app
WORKDIR /app
COPY Gemfile Gemfile.lock ./
RUN bundle config set --local deployment 'true'
RUN bundle config set --local without 'development test'
RUN bundle install
COPY . .
EXPOSE 3000
CMD ["foreman", "start"]
tail of build
Removing intermediate container 1a445fde7fc0
---> 43c3d72b7eb6
Step 17/27 : RUN rbenv versions
---> Running in feb5bb9361cc
* truffleruby-20.3.0 (set by /root/.rbenv/version)
Removing intermediate container feb5bb9361cc
---> c7d1a5826af5
Step 18/27 : RUN gem install bundler:2.2.4 foreman
---> Running in 998461afc89c
/bin/sh: 1: gem: not found
The command '/bin/sh -c gem install bundler:2.2.4 foreman' returned a non-zero code: 127
You don't generally use version managers like rbenv in Docker. There are a couple of reasons for this. One is that an image usually only contains a single application and its single runtime, so you'd never have more than one Ruby in an image and therefore there's no need to switch. A second is that most common paths of running containers (including docker run and the Dockerfile RUN directive) don't look at shell dotfiles like .bashrc or /etc/profile, so the version manager setup will never get run.
TruffleRuby is distributed (among other ways) as a standalone tar file so you can just install that in your Dockerfile. I'd make the Dockerfile look roughly like:
FROM debian:buster-slim
# Install the specific dependency packages TruffleRuby recommends
# (build-essential is much larger but might actually be necessary)
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive \
apt-get install --no-install-recommends --assume-yes \
curl \
gcc \
libssl-dev \
libz-dev \
make
# Download and unpack TruffleRuby
ARG TRUFFLERUBY_VERSION=20.3.0
ENV PATH /opt/truffleruby-$TRUFFLERUBY_VERSION-linux-amd64/bin:$PATH
RUN cd /opt \
&& curl -L https://github.com/oracle/truffleruby/releases/download/vm-$TRUFFLERUBY_VERSION/truffleruby-$TRUFFLERUBY_VERSION-linux-amd64.tar.gz | tar xz \
&& /opt/truffleruby-$TRUFFLERUBY_VERSION-linux-amd64/lib/truffle/post_install_hook.sh
# Now build and install your application
RUN gem install bundler:2.2.4
WORKDIR /app
COPY Gemfile Gemfile.lock ./
RUN bundle config set --local deployment 'true'
RUN bundle config set --local without 'development test'
RUN bundle install
COPY . .
ENTRYPOINT ["bundle", "exec"]
EXPOSE 3000
CMD ["rails", "start"]
You can reasonably split this into two separate Dockerfiles. End the first one before the "build and install your application" comment, and build it with docker build -t myname/truffleruby:20.3.0 -f Dockerfile.truffleruby .. Then the second one can begin with FROM myname/truffleruby:20.3.0 in the same way as the standard Docker Hub ruby image.
Does the same work with CRuby?
I'd suspect RUN doesn't read shell files, so PATH needs to be modified.
RUN git clone https://github.com/sstephenson/rbenv.git /root/.rbenv
...
RUN /root/.rbenv/plugins/ruby-build/install.sh
ENV PATH /root/.rbenv/bin:$PATH
seems a bit weird, is rbenv cloned and installed in the same place?
I would skip rbenv in Docker, and instead just:
RUN ruby-build truffleruby ~/.rubies/truffleruby
ENV PATH $HOME/.rubies/truffleruby/bin:$PATH

How to install latest Ruby on Gitlab config for Docker

Here's how my config looks like:
deploy_hybrid:
stage: deploy
before_script:
- apt update -qq
- apt install ruby-full -qq
- gem install cocoapods
- pod repo update
However, it's always installing Ruby 2.1
WARNING: apt does not have a stable CLI interface yet. Use with
caution in scripts. The following extra packages will be installed:
libruby2.1 libtcl8.5 libtcltk-ruby libtk8.5 libxft2 libxss1 ri ruby
ruby-dev ruby2.1 ruby2.1-dev ruby2.1-doc ruby2.1-tcltk
rubygems-integration

Docker: Ruby version upgrade from 2.2.0 to 2.4.0

I am using Ruby version 2.2.0 in my Rails Application and Rails version is 4.2.0.
Currently, Project is built through docker, so I am thinking of to upgrade the ruby version with 2.4.0 version.
I believe I need to change the docker image to upgrade the ruby version.
I already check a few of the articles but didn't get enough information.
Docker file
FROM ruby:2.2.0
RUN apt update && \
apt install -y --no-install-recommends \
git \
curl \
gnupg2 \
libpq-dev \
libmysqlclient-dev \
nodejs \
graphviz \
&& rm -rf /var/lib/apt/lists/*
Once ruby version upgraded then I will upgrade the rails version.
Any help will be appreciated.
You can always build your own Docker image as such:
FROM ubuntu
RUN apt-get update
RUN apt-get install -y build-essential wget
RUN wget --no-check-certificate -O ruby-install.tar.gz https://github.com/postmodern/ruby-install/archive/master.tar.gz
RUN tar -xzvf ruby-install.tar.gz
RUN cd ruby-install-master && make install
RUN cd /
RUN rm -rf ruby-install-master && rm -rf ruby-install.tar.gz
RUN ruby-install --latest
RUN ruby-install -i /usr/local/ ruby 2.4.0 -- --disable-install-doc
RUN gem update --system --no-document
RUN gem install bundler --force
depends on how one manages ruby application and ruby version. here are few things i would offer to check:
update Gemfile if bundler is used
$ cat Gemfile | head -n 1
ruby '2.4.0'
update .ruby-version if rbenv or [rvm][3] is used
$ cat .ruby-version
2.4.0
update Dockerfile if docker is used
$ cat Dockerfile | grep FROM
FROM ruby:2.4.0

Gitlab: gem not found

I am trying to deploy our app to Heroku and our settings in .gitlab-ci.yml looks like
staging_heroku:
stage: deploy
script:
- git remote add heroku https://heroku:$STAGING_HEROKU_KEY#git.heroku.com/staging-myapp.git
- git push -f heroku master
This is what we see in logs
Cloning repository...
Cloning into '/builds/org/project'...
Checking out 340111af as dev/feature1...
Skipping Git submodules setup
Downloading artifacts for maven-build (17234382)...
Downloading artifacts from coordinator... ok id=17234382 responseStatus=200 OK token=2YSHdANA
/bin/sh: eval: line 46: apt-get: not found
$ apt-get update -yqqq
ERROR: Job failed: exit code 127
These runners do not even have apt-get and so I can not install gem.
I even tried git command, but even that is not found. Can someone help?
You need to install ruby and ruby-dev first! (and rubygems-integration for Debian 8)
staging:
type: deploy
script:
- apt-get update -yq
- apt-get install -y ruby ruby-dev rubygems-integration
- gem install dpl
- dpl --provider=heroku --app=teeth-taroko --api-key=$HEROKU_STAGING_API_KEY
only:
- develop

Resources