Following the Ruby community’s advice to avoid using the system-installed Ruby instance for development, you install chruby using the official installation instructions and intend to configure your Ruby versions using ruby-install. After installing chruby, you open a terminal and see the following error when running the command on your Unix-like OS (Ubuntu Linux or MacOS):
$ chruby
chruby: command not found
What happened?
Let's take a closer look at what happened. The installation script automatically adds a /etc/profile.d script at /etc/profile.d/chruby.sh. The /etc/profile.d scripts are themselves auto-discovered and then executed by the /etc/profile script, which one might expect to happen upon logging in:
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
However, /etc/profile is invoked only for login shells. A login shell is what we use when logging in via SSH or via a TTY. Our terminal instance is an interactive non-login shell and so /etc/profile is not executed, including our new chruby.sh script that would have made the chruby command available to our session.
To make the chruby command available to an interactive shell, we must configure our shell accordingly. For Bash this involves editing the ~/.bashrc or /etc/bash.bashrc files, which are the current user and global configuration scripts for Bash respectively:
source /usr/local/share/chruby/chruby.sh
For ZSH on MacOS, the equivalent files would be ~/.zshrc and /etc/zshrc. If using a shell other than Bash or ZSH, consult the documentation to ensure the correct configuration script is updated.
Correctly configured, we now see the following when running chruby:
$ chruby
ruby-3.3.0
What next?
Although the convention for Unix-like application installers is not to touch the user's data, the Google Cloud SDK installer asks users whether it should update their ~/.bashrc file as part of the installation process:
Modify profile to update your $PATH and enable bash completion? (Y/n)?
The chruby installer could help users by offering to do the same, even if only handling the most popular shells like Bash or ZSH.