16

I am trying to install ruby on docker. I could install the 1.9 versions but it is not possible to install the latest version such as 2.2.0 and above. I am actually trying to set up calabash on docker. Have tried this. Whenever I try to install calabash-android in it getting the error

ERROR:  Error installing calabash-android:
luffa requires Ruby version >= 2.0.
Community
  • 1
  • 1
Muneer Muhammed
  • 883
  • 3
  • 10
  • 29

6 Answers6

20

If you're starting FROM a different base Docker instance, you can simply RUN commands that install Ruby from your base instance's package management system. For example, this GitHub Gist shows how to use apt-get to install Ruby on a Ubuntu instance:

# Pull base image.
FROM dockerfile/ubuntu

# Install Ruby.
RUN \
  apt-get update && \
  apt-get install -y ruby

And this Gist shows a Dockerfile that's configured to install RVM and Ruby on a Ubuntu instance:

FROM ubuntu

RUN apt-get update

# basics
RUN apt-get install -y openssl

# install RVM, Ruby, and Bundler
RUN \curl -L https://get.rvm.io | bash -s stable
RUN /bin/bash -l -c "rvm requirements"
RUN /bin/bash -l -c "rvm install 2.0"
RUN /bin/bash -l -c "gem install bundler --no-ri --no-rdoc"
Kevin
  • 14,655
  • 24
  • 74
  • 124
  • 1
    Looks like link to gist with Dockerfile for ruby via rvm is wrong. Also it may be required to add curl to `# basics` like `RUN apt-get install -y openssl curl`. [link to gist I've tried](https://gist.github.com/malovme/ad28917f1d86fac20b9c2634fbd0a048) – Michael Malov Apr 13 '18 at 18:43
16

This makes ruby available for any future RUN command and not just bash:

FROM debian:stretch-slim
RUN \
  apt-get update && apt-get install -y --no-install-recommends --no-install-suggests curl bzip2 build-essential libssl-dev libreadline-dev zlib1g-dev && \
  rm -rf /var/lib/apt/lists/* && \
  curl -L https://github.com/sstephenson/ruby-build/archive/v20180329.tar.gz | tar -zxvf - -C /tmp/ && \
  cd /tmp/ruby-build-* && ./install.sh && cd / && \
  ruby-build -v 2.5.1 /usr/local && rm -rfv /tmp/ruby-build-* && \
  gem install bundler --no-rdoc --no-ri
grosser
  • 14,707
  • 7
  • 57
  • 61
  • 2
    took me two days of faffing around to get here, my environment is docker using image openjdk:8 to use in android build pipeline – hyena Nov 28 '19 at 06:12
13

You could start view a dockerfile starting with:

# 2016
FROM ruby:2.3.0

# 2020
# Import your ruby version
FROM ruby:2.7.1
# Install bundler gem
RUN gem install bundler
# Assign a work directory
WORKDIR /work

That would use the docker image ruby, with ruby already installed.

The 2020 version comes from "Ruby version management with docker" from Arjun Das, mentioned by ArMD in the comments.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks @VonC for your help. Can I manually install ruby when I am inside the container? – Muneer Muhammed Mar 30 '16 at 11:37
  • 2
    @muneermuhammed no: you would specify the installation of ruby in a Dockerfile in order to build an image with ruby installed, which then allows you to run a container in which ruby is available. You don't install anything directly in a container. Plus, if your Dockerfile starts with FROM ruby:2.3.0, you don't have to install ruby at all. It is already installed for you. – VonC Mar 30 '16 at 11:40
  • Can I simply run the command as RUN ruby:2.3.0 if my docker file starts with FROM ubuntu:14.04 ? – Muneer Muhammed Mar 30 '16 at 11:53
  • 1
    @muneermuhammed no, RUN is for shell commands, not docker image names. You would need, in your Dockerfile, to reproduce the content of https://github.com/docker-library/ruby/blob/c88f3a67da720bfa9fb1717960d90fd5db11c757/2.3/Dockerfile in order to install ruby: it is done on top of a Debian Jessie distribution but might work on top of an ubuntu one. – VonC Mar 30 '16 at 11:55
  • @muneermuhammed a simlper alternative is to use the `FROM ruby:2.3.0` directive, and see if what you are doing on top of that is compatible with a Debien jessie distribution. – VonC Mar 30 '16 at 11:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107732/discussion-between-muneer-muhammed-and-vonc). – Muneer Muhammed Mar 30 '16 at 11:57
  • I always start my ruby setup with the dockerfile mentioned here - https://thearjunmdas.github.io/entries/dev-with-ruby-docker/ – ArMD Dec 09 '20 at 10:16
  • 1
    @ArMD Thank you. I have included your comment in the answer for more visibility. – VonC Dec 09 '20 at 10:22
4

Low reputation so I can't comment inline (all those years of lurking, sigh), but in case anyone else happens across this while searching for ways to install old ruby versions to docker, I found @grosser's answer very helpful - it worked where trying to install via RVM simply wouldn't, at least for me.

I would, however, recommend using the recommended approach for installing ruby-build - the following worked for me:

<prior steps>
RUN git clone https://github.com/rbenv/ruby-build.git && \
  PREFIX=/usr/local ./ruby-build/install.sh && \
  ruby-build -v 2.4.1 /usr/local && \
  gem install bundler -v <VERSION HERE> --no-ri --no-rdoc && bundle install
<following steps>

Key point here is that this keeps you up to date with ruby-build instead of being hard-coded to the 2018-03-29 version as in a previous @grosser's comment.

Cadoiz
  • 1,446
  • 21
  • 31
Jacob
  • 61
  • 4
2

Thanks to @Jacob and @grosser, I've managed to set up mine in a similar, if a bit more unpacked way:

# Install Local ruby
RUN git clone https://github.com/rbenv/rbenv.git ~/.rbenv \
  &&  echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc \
  &&  echo 'eval "$(rbenv init -)"' >> ~/.bashrc

ENV HOME /home/jenkins # Change this dir as needed.
ENV PATH "$HOME/.rbenv/bin:$HOME/.rbenv/shims:$PATH"
ENV RUBY_VERSION 2.6.3

RUN mkdir -p "$(rbenv root)"/plugins \
    && git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build

RUN rbenv install $RUBY_VERSION

RUN rbenv global $RUBY_VERSION && rbenv versions && ruby -v

# RUN curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash # Uncomment this to get rbenv to validate your setup.
Cadoiz
  • 1,446
  • 21
  • 31
Janis Peisenieks
  • 4,938
  • 10
  • 55
  • 85
2

If you want to use things like bundle install and don't use a base image with pre-installed devtools like Ubuntu, you need to install these packages:

RUN apt-get update && apt-get install -y ruby ruby-dev ruby-bundler build-essential
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
brainless
  • 91
  • 1
  • 9