Skip to content

Instantly share code, notes, and snippets.

@jhass
Last active October 20, 2024 06:51
Show Gist options
  • Save jhass/8839655bb038e829fba1 to your computer and use it in GitHub Desktop.
Save jhass/8839655bb038e829fba1 to your computer and use it in GitHub Desktop.
My Ruby setup on Archlinux

Ruby on Archlinux

I thought I would document my setup, since it's somewhat non-standard but working quite well for me.

What this does

  • Install major Ruby versions at their latest patch release
  • Allow to switch between them seamlessly
  • Use chruby
  • Encourage bundler usage

What this does not

  • Install files outside your home folder that are not tracked by your package manager
  • Use ruby-install, ruby-build, rbenv or RVM
  • Allow you to switch to a specific outdated patchlevel version of Ruby, out of the box.

Setup

yay is used as an example AUR helper, use whichever you like or do it manually. Prefixed with # means run as root, prefixed with $ means run as the user you regularly work with.

# pacman -S ruby
$ yay -S chruby ruby-bundler
$ mkdir -p ~/.rubies/3.0/bin
$ ln -s /usr/bin/ruby ~/.rubies/3.0/bin/ruby
$ echo "gem: --no-user-install --env-shebang" > ~/.gemrc

Unfortunately the ruby2.7 package in community changed the packaging style compared to what has been done historically. To make it available we need another symlink:

$ mkdir -p ~/.rubies/2.7/bin
$ ln -s /usr/bin/ruby-2.7 ~/.rubies/2.7/bin/ruby

Edit your ~/.$SHELLrc and add:

# chruby
source /usr/share/chruby/chruby.sh
source /usr/share/chruby/auto.sh
RUBIES=(/opt/ruby* $HOME/.rubies/*)

Optionally, but the entire point of this setup, install older Ruby versions:

$ yay -S ruby2.6 ruby2.6-bundler ruby2.7

If you want to make other Ruby distributions available just add them to RUBIES if they provide a bin/ruby. Else symlink them into .rubies like we did above for ruby2.7.

Upgrading

When the ruby package moves to a new minor or major version (major.minor.teensy), do the following (adjusting the versions of course):

$ mv ~/.rubies/3.0 ~/.rubies/3.1

Most likely I or somebody else will upload a package for the old release to the AUR:

$ yay -S ruby3.0

Depending on how it's packaged you might need to add a new symlink to .rubies.

Per project setup

Create a .ruby-version file with the desired version to use, like

2.7

Use a Gemfile and bundle install to install gems into your home folder, or bundle install --path vendor/bundle to install gems to a per project directory.

System wide gem installation

Do not do gem install as root, install the packages from the AUR like we already did for ruby-bundler, ruby2.7-bundler and so on. If the gem you want to install system wide isn't packaged yet, create one! Have a look at existing packages for inspiration or facilitate tools like gem2arch.

Scripting

Use /usr/bin/chruby-exec 2.7 -- regular command to switch to a different Ruby environment in your scripts. This only works for scripts run as users that have this setup. Same holds true for systemd units, while you can use chruby-exec in ExecStart and friends, your service needs to have a User= to one with this setup.

@manueltorrez
Copy link

Why do you use the AUR version? Is it better? Isn't better to use $ sudo pacman -S ruby? Thanks!

@jhass
Copy link
Author

jhass commented May 15, 2021

yay will install from the regular repositories if the package is found there and only if not found it'll look into the AUR.

The regular repositories and the AUR usually do not share any package names.

@rahil627
Copy link

thanks for the write-up. I just went through a similar process, making a symlink for the packaged ruby... (why on earth should we have to compile ruby using ruby-install??)... I think you can just do chruby system to set chruby to the system-installed ruby, but the link def makes things more clear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment