How to install Ruby on docker?

27,829

Solution 1

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.

Solution 2

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"

Solution 3

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

Solution 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 @grosser's comment.

Solution 5

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/*
Share:
27,829
Muneer Muhammed
Author by

Muneer Muhammed

Updated on December 10, 2020

Comments

  • Muneer Muhammed
    Muneer Muhammed over 3 years

    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.
    
  • Muneer Muhammed
    Muneer Muhammed about 8 years
    Thanks @VonC for your help. Can I manually install ruby when I am inside the container?
  • VonC
    VonC about 8 years
    @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.
  • Muneer Muhammed
    Muneer Muhammed about 8 years
    Can I simply run the command as RUN ruby:2.3.0 if my docker file starts with FROM ubuntu:14.04 ?
  • VonC
    VonC about 8 years
    @muneermuhammed no, RUN is for shell commands, not docker image names. You would need, in your Dockerfile, to reproduce the content of github.com/docker-library/ruby/blob/… 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
    VonC about 8 years
    @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.
  • Muneer Muhammed
    Muneer Muhammed about 8 years
  • Michael Malov
    Michael Malov about 6 years
    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
  • hyena
    hyena over 4 years
    took me two days of faffing around to get here, my environment is docker using image openjdk:8 to use in android build pipeline
  • ArMD
    ArMD over 3 years
    I always start my ruby setup with the dockerfile mentioned here - thearjunmdas.github.io/entries/dev-with-ruby-docker
  • VonC
    VonC over 3 years
    @ArMD Thank you. I have included your comment in the answer for more visibility.