Install Ruby

The Ruby language is the programming language that powers Discourse and a number of other software we use casually at :ps:. Over the years, installing Ruby has taken various forms. Despite fantastic work done by the Debian Ruby team to package Ruby software, Debian usually lags too much to be useful.

The preferred method for installation is therefore to use ruby-install with some configuration options.

Quick!

  1. Install build dependencies: rustc libjemalloc-dev libjemalloc
  2. Configure Ruby with --with-jemalloc --enable-yjit
  3. Use postmodern’s chruby and ruby-install.

Install ruby-install

mkdir -p ~/src/3rd-party/postmodern
cd $_
git clone github.com/postmodern/ruby-install/
cd ruby-install
sudo make install
cd ..
rehash # if you're using zsh
hash -r # if you're using bash or some other shell

Install ruby

# Install normal Ruby dependencies
sudo apt install -y build-essential bison zlib1g-dev libyaml-dev libssl-dev libgdbm-dev libreadline-dev libffi-dev
# Install dependencies for extra configuration
sudo apt install -y libjemalloc libjemalloc-dev rustc
# Retrieve the latest Ruby versions
ruby-install -U
# Configure the latest version with `jemalloc` and `yjit` support
# Either system wide as root:
sudo ruby-install --system 3.3.5 -- --with-jemalloc --enable-yjit
# Or as a user: 
ruby-install -c --system 3.3.5 -- --with-jemalloc --enable-yjit
# -c will remove sources after compilation, so it may be used for both options above

Install chruby

There are various ways to use multiple versions of Ruby, depending on the software you want to use: rbenv, rvm, and chruby are the most used. The latter has a number of advantages over the former ones. It’s very lightweight and won’t try to do a lot of magic: it simply changes some environment variables and updates the command lookup table.

cd ~/src/3rd-party/postmodern
git clone https://github.com/postmodern/chruby
cd chruby
sudo make install

Configure chruby

Now hook this into your shell’s RC file, e.g.:

echo 'source /usr/local/share/chruby/chruby.sh' | tee -a $ZDOTDIR/.zshrc
echo 'source /usr/local/share/chruby/auto.sh' | tee -a $ZDOTDIR/.zshrc

The last line allows chruby to switch versions automatically when changing to a directory with a .ruby-version file. Now all your shells will have chruby activated, defaulting to system ruby, and following .ruby-version files.

Choose a specific default Ruby version

You can set a different default than system ruby (which you would install with sudo apt install ruby) with a .ruby-version file in your $HOME:

cat ~/.ruby-version
3.3.5