Muneer Muhammed
Muneer Muhammed

Reputation: 953

How to install Ruby on docker?

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.

Upvotes: 16

Views: 42638

Answers (6)

brainless
brainless

Reputation: 91

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

Upvotes: 2

Janis Peisenieks
Janis Peisenieks

Reputation: 4988

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.

Upvotes: 2

Jacob
Jacob

Reputation: 71

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.

Upvotes: 5

VonC
VonC

Reputation: 1323035

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.

Upvotes: 13

grosser
grosser

Reputation: 15097

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

Upvotes: 16

Kevin
Kevin

Reputation: 15934

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"

Upvotes: 20

Related Questions