[Ruby Installation] Install multiple Ruby versions on macOS

Multiple Ruby versions

Using a Ruby version manager is critical if you plan on running many projects with different Ruby version. A project might not be compatible with the version of Ruby you have installed. We need a way to switch between versions of Ruby without installing and uninstalling them every time.

We need a way to switch between versions of Ruby without installing and uninstalling them every time.

There are two different ways to install and run multiple Ruby versions.

  • Host Mode - Install and run Ruby within host or virtual machine by Ruby Version Manager(RVM), asdf or rbenv.

  • Container Mode - Install and run Ruby within container by Docker and Docker Compose, or Kuberneters(K8S).

Prerequisites

  • Xcode Command Line Tools
    The Xcode Command Line Tools Package is a small self-contained package available for download separately from Xcode and that allows you to do command line development in macOS which is consists of the macOS SDK and command-line tools such as Clang, which are installed at this

    For more information about installing and using Xcode Command Line Tools, see the Xcode Command Line Tools

  • Homebrew
    Homebrew is the Missing Package Manager for macOS (or Linux).

    For more information about installing and using Homebrew, see the Homebrew - https://brew.sh/.

1. Host Mode

Install and run Ruby within host or virtual machine by Ruby Version Manager(RVM), asdf or rbenv.

1.1. Ruby Version Manager(RVM)

Ruby Version Manager(RVM) is a command-line tool which allows you to easily install, manage, and work with multiple Ruby environments from interpreters to sets of gems.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Install GPG keys
$ gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

# Install Rvm
$ \curl -sSL https://get.rvm.io | bash -s stable

# Reload rvm
$ source ~/.rvm/scripts/rvm

# Install Ruby 2.6.1
$ rvm install 2.6.1

$ ruby -v
ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-darwin19

See RVM: Ruby Version Manager - Installing RVM - https://rvm.io/rvm/install#installation to learn more.

1.2. asdf

Manage multiple runtime versions with a single CLI tool, extendable via plugins - docs at asdf-vm.com.

asdf is a CLI tool that can manage multiple language runtime versions on a per-project basis. It is like gvm, nvm, rbenv & pyenv (and more) all in one! Simply install your language’s plugin!

Install asdf.

1
2
3
4
5
# Install asdf dependencies
$ brew install coreutils curl git

# Install asdf
$ brew install asdf

Install asdf Ruby Plugin.

1
2
3
4
5
# Install Ruby plugin requirements
$ brew install openssl readline

# Install Ruby plugin
$ asdf plugin-add ruby https://github.com/asdf-vm/asdf-ruby.git

Update Ruby plugin.

1
$ asdf plugin update ruby

List all Ruby versions.

1
$ asdf list all ruby

Install a Ruby version manually.

1
2
3
4
$ asdf install ruby 2.7.2

# Or install another version
# asdf install ruby 2.6.1

See follow links to learn more:

asdf-vm/asdf: Extendable version manager with support for Ruby, Node.js, Elixir, Erlang & more - https://github.com/asdf-vm/asdf

All Plugins - https://asdf-vm.com/#/plugins-all

asdf-vm/asdf-ruby: Ruby plugin for asdf version manager - https://github.com/asdf-vm/asdf-ruby

1.3. rbenv

Groom your app’s Ruby environment with rbenv.

Use rbenv to pick a Ruby version for your application and guarantee that your development environment matches production. Put rbenv to work with Bundler for painless Ruby upgrades and bulletproof deployments.

rbenv/rbenv: Groom your app’s Ruby environment - https://github.com/rbenv/rbenv

Compatibility note: rbenv is incompatible with RVM. Please make sure to fully uninstall RVM and remove any references to it from your shell initialization files before installing rbenv.

2. Container Mode

Install and run Ruby within container by Docker and Docker Compose, or Kuberneters(K8S).

2.1 Docker and Docker Compose

Create or update Dockerfile.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Dockerfile

ARG VERSION=3.0.2
FROM ruby:${VERSION}

# throw errors if Gemfile has been modified since Gemfile.lock
RUN bundle config --global frozen 1

WORKDIR /usr/src/app

COPY Gemfile Gemfile.lock ./
RUN bundle install

COPY . .

CMD ["./your-daemon-or-script.rb"]

Build docker image and run container.

1
2
3
$ docker build -t my-ruby-app .

$ docker run -it --name my-running-script my-ruby-app

docker-compose.yml describes the services that comprise your app (a database and a web app), how to get each one’s Docker image (the database just runs on a pre-made PostgreSQL image, and the web app is built from the current directory), and the configuration needed to link them together and expose the web app’s port.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
version: "3.9"
services:
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: password
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db

You can now boot the app with docker-compose up:

1
$ docker-compose up

See follow links to learn more.

Install Docker Engine | Docker Documentation - https://docs.docker.com/engine/install/

Install Docker Compose | Docker Documentation - https://docs.docker.com/compose/install/

Quickstart: Compose and Rails | Docker Documentation - https://docs.docker.com/compose/rails/

2.2 Kubernetes (K8S)

Create or update k8s manifests file k8s.yml.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# k8s.yml

apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
labels:
app: myapp
tier: app
spec:
replicas: 2
minReadySeconds: 5
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
selector:
matchLabels:
app: myapp
tier: app
template:
metadata:
labels:
app: rubytags
tier: app
spec:
imagePullSecrets:
- name: docker-registry
containers:
- name: myapp
image: my-ruby-app
imagePullPolicy: Always
ports:
- containerPort: 3000
1
$ kubectl create -f k8s.yml

References

[1] RVM: Ruby Version Manager - Installing RVM - https://rvm.io/rvm/install#installation

[2] asdf-vm/asdf: Extendable version manager with support for Ruby, Node.js, Elixir, Erlang & more - https://github.com/asdf-vm/asdf

[3] All Plugins - https://asdf-vm.com/#/plugins-all

[4] asdf-vm/asdf-ruby: Ruby plugin for asdf version manager - https://github.com/asdf-vm/asdf-ruby

[5] Install Docker Engine | Docker Documentation - https://docs.docker.com/engine/install/

[6] Install Docker Compose | Docker Documentation - https://docs.docker.com/compose/install/

[7] Quickstart: Compose and Rails | Docker Documentation - https://docs.docker.com/compose/rails/

[8] Ruby - Official Image | Docker Hub - https://hub.docker.com/_/ruby

[8] rbenv/rbenv: Groom your app’s Ruby environment - https://github.com/rbenv/rbenv

[9] A Comparison of Ruby Version Managers for macOS - SitePoint - https://www.sitepoint.com/ruby-version-managers-macos/

[10] Schniz/fnm: 🚀 Fast and simple Node.js version manager, built in Rust - https://github.com/Schniz/fnm

[11] TaKO8Ki/frum: A little bit fast and modern Ruby version manager written in Rust - https://github.com/tako8ki/frum

[12] Install Ruby with Frum · Mac Install Guide - https://mac.install.guide/ruby/14.html

[13] [Deploying Ruby on Rails Apps on Kubernetes | Littlelines - https://littlelines.com/blog/2020/04/08/deploying-ruby-on-rails-apps-on-kubernetes)(https://littlelines.com/blog/2020/04/08/deploying-ruby-on-rails-apps-on-kubernetes)