Installing Ruby in a Dockerfile for multiple users

John Cline
2 min readSep 15, 2023

I recently ran into an issue where we had a version incompatibility between two dependencies, Postgres and Ruby. The official Ruby Docker images for 2.7.8 only supported up to Debian Bullseye, however we were migrating to Postgres 15 which is only available on Debian Bookworm which doesn’t have any pre-built packages for Ruby 2.x.

To further complicate things, we require root privileges during the Docker build process but we run our application as non-root. I couldn’t find any good clear examples of how to install an arbitrary version of Ruby in a Dockerfile for all users, but managed to cobble this together. Example Dockerfile and explanation below.

FROM debian:bookworm-slim

# Install packages needed for ruby-build
RUN apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
build-essential \
ca-certificates \
curl \
git \
libz-dev

# Check out ruby-build and install
RUN git clone https://github.com/sstephenson/ruby-build.git /usr/local/ruby-build
RUN /usr/local/ruby-build/install.sh

# Install the version of Ruby we want and add to the path
RUN ruby-build 2.7.8 /usr/local/ruby-2.7.8
ENV PATH="/usr/local/ruby-2.7.8/bin:${PATH}"

RUN echo "$(ruby --version)"

We use ruby-build to build the version of Ruby we need from source, and install to /usr/local and add to the path so that all users can execute. ruby-build installs any needed dependencies automatically, so we only need git to do the initial clone.

Inspiration from Taichi NAKASHIMA’s dockerfile-rbenv that installs rbenv and ruby-build in a Dockerfile.

--

--