Why chruby script doesn't work when in /etc/profile.d?

When I install chruby, I have to edit its /etc/profile.d/chruby.sh and remove the comment delimiters from each line. After the edit, its content is the following.

[ -n "$BASH_VERSION" ] || [ -n "$ZSH_VERSION" ] || return
source /usr/share/chruby/chruby.sh

When I restart my computer, that file does not seem to have effect: chruby is not recognized as command.

I then tried to copy the /usr/share/chruby/chruby.sh file to /etc/profile.d/chruby.sh, thinking that the first line could cause the script to terminate prematurely. This did not work either.

chruby is recognized as command only when I add the source /usr/share/chruby/chruby.sh line to the .bashrc file.
Is there any reason that would cause this code not to work?

CHRUBY_VERSION="0.3.9"
RUBIES=()

for dir in "$PREFIX/opt/rubies" "$HOME/.rubies"; do
	[[ -d "$dir" && -n "$(ls -A "$dir")" ]] && RUBIES+=("$dir"/*)
done
unset dir

function chruby_reset()
{
	[[ -z "$RUBY_ROOT" ]] && return

	PATH=":$PATH:"; PATH="${PATH//:$RUBY_ROOT\/bin:/:}"

	if (( $UID != 0 )); then
		[[ -n "$GEM_HOME" ]] && PATH="${PATH//:$GEM_HOME\/bin:/:}"
		[[ -n "$GEM_ROOT" ]] && PATH="${PATH//:$GEM_ROOT\/bin:/:}"

		GEM_PATH=":$GEM_PATH:"
		[[ -n "$GEM_HOME" ]] && GEM_PATH="${GEM_PATH//:$GEM_HOME:/:}"
		[[ -n "$GEM_ROOT" ]] && GEM_PATH="${GEM_PATH//:$GEM_ROOT:/:}"
		GEM_PATH="${GEM_PATH#:}"; GEM_PATH="${GEM_PATH%:}"
		unset GEM_ROOT GEM_HOME
		[[ -z "$GEM_PATH" ]] && unset GEM_PATH
	fi

	PATH="${PATH#:}"; PATH="${PATH%:}"
	unset RUBY_ROOT RUBY_ENGINE RUBY_VERSION RUBYOPT
	hash -r
}

function chruby_use()
{
	if [[ ! -x "$1/bin/ruby" ]]; then
		echo "chruby: $1/bin/ruby not executable" >&2
		return 1
	fi

	[[ -n "$RUBY_ROOT" ]] && chruby_reset

	export RUBY_ROOT="$1"
	export RUBYOPT="$2"
	export PATH="$RUBY_ROOT/bin:$PATH"

	eval "$("$RUBY_ROOT/bin/ruby" - <<EOF
puts "export RUBY_ENGINE=#{defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'};"
puts "export RUBY_VERSION=#{RUBY_VERSION};"
begin; require 'rubygems'; puts "export GEM_ROOT=#{Gem.default_dir.inspect};"; rescue LoadError; end
EOF
)"

	if (( $UID != 0 )); then
		export GEM_HOME="$HOME/.gem/$RUBY_ENGINE/$RUBY_VERSION"
		export GEM_PATH="$GEM_HOME${GEM_ROOT:+:$GEM_ROOT}${GEM_PATH:+:$GEM_PATH}"
		export PATH="$GEM_HOME/bin${GEM_ROOT:+:$GEM_ROOT/bin}:$PATH"
	fi
}

function chruby()
{
	case "$1" in
		-h|--help)
			echo "usage: chruby [RUBY|VERSION|system] [RUBYOPT...]"
			;;
		-V|--version)
			echo "chruby: $CHRUBY_VERSION"
			;;
		"")
			local dir star
			for dir in "${RUBIES[@]}"; do
				dir="${dir%%/}"
				if [[ "$dir" == "$RUBY_ROOT" ]]; then star="*"
				else                                  star=" "
				fi

				echo " $star ${dir##*/}"
			done
			;;
		system) chruby_reset ;;
		*)
			local dir match
			for dir in "${RUBIES[@]}"; do
				dir="${dir%%/}"
				case "${dir##*/}" in
					"$1")	match="$dir" && break ;;
					*"$1"*)	match="$dir" ;;
				esac
			done

			if [[ -z "$match" ]]; then
				echo "chruby: unknown Ruby: $1" >&2
				return 1
			fi

			shift
			chruby_use "$match" "$*"
			;;
	esac
}

I mean, are there restrictions on scripts placed in /etc/profile.d that would explain why that script does not work as expected?

Might it be that chruby should be handled analogously to Gems?

Also, look at the chruby page on AUR (latest comment). That seems to reflect on your situation?

So, perhaps it would be easier to install a different version of ruby

After reading more into it:

The GitHub page for chruby mentions this ecplicitly:

Configuration

Add the following to the ~/.bashrc or ~/.zshrc file:

source /usr/local/share/chruby/chruby.sh

I noticed that even adding source /usr/share/chruby/chruby.sh to the .bashrc file does not work; I have to execute it directly on the terminal window I open. It seems I should copy all the lines from the /usr/share/chruby/chruby.sh file to the .bashrc file.

I agree: It is much better to use a different installer/manager.

You have to reboot (or at least log off and back on) after this. Did you?

I did reboot. It is then that I noticed chruby was not found when I tried to set the Ruby version to globally use.

Is it still there in it’s original place?
/usr/share/chruby/chruby.sh

Maybe, you have to delete it first from /etc/profile.d ?

yay -S chruby does not install any /usr/local/share/chruby/chruby.sh file. I only find a /usr/share/chruby/chruby.sh file the instructions given after installing says to use.

Please do one of the following or something similar depending of your preferred shell and setup:

echo 'source /usr/share/chruby/chruby.sh' >> ~/.bashrc
echo 'source /usr/share/chruby/chruby.sh' >> ~/.zprofile

For further support/information, please see the project’s homepage!

For your convenience there’s a file at /etc/profile.d/chruby.sh which can used to enable chruby systemwide. You just need to uncomment the lines in the file.

Nobody said that.

In your OP you were attempting a system-wide use of chruby. Maybe what you did there (wrongly?) could obstruct getting it to work in the first place?

I’d try a system-wide usage of chruby only after getting it to work initially at all.

System Wide

If you wish to enable chruby system-wide, add the following to /etc/profile.d/chruby.sh:

if [ -n "$BASH_VERSION" ] || [ -n "$ZSH_VERSION" ]; then source /usr/local/share/chruby/chruby.sh ... fi

This will prevent chruby from accidentally being loaded by /bin/sh, which is not always the same as /bin/bash.

It was a reply to your previous post. I just meant that either the GitHub page is not updated, or it does not apply to EndeavourOS / Arch Linux.

I apologize: My posts did not mean to say you were wrong or to push you to find another solution. I only reported what I found on my computer after running yay -S chruby.

Your OP shows various things you did to your system that maybe counteracting against each other.

You have to clean them up before we can try for a solution, I think. :wink:

/etc/profile.d/chruby.sh only comes in to play for system-wide enabling of chruby (i.e. that is secondary at the moment).

That is wrong.

The only thing needed is adding this line to your .bashrc:

source /usr/local/share/chruby/chruby.sh

I deleted the /etc/profile.d/chruby.sh file with sudo rm /etc/profile.d/chruby.sh, I added the source /usr/share/chruby/chruby.sh line to the .bashrc file. I rebooted and now chruby works.

I was doing the mistake to verify with which chruby if all worked correctly, as that command returns which: no chruby in (/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl). That is correct: chruby is a function, as type chruby shows.

1 Like

For potential further usage of chruby system-wide, you could just reinstall the package (because you deleted the original /etc/profile.d/chruby.sh):

yay -S chruby

The new installation would not change your .bashrc

Glad it works! :v:

That is what the output of yay -S chruby suggested. Since that required to just edit a file, and since I was convinced that putting a file in the /etc/profile.d/ directory is better than adding new lines to the .bashrc file, I first tried that.
Then I tried a different way, as I wanted to create a script to install chruby on other computers.

At least, I learned a lesson, or two. :sweat_smile: :wink:

1 Like

:laughing: Like what?

That is easy:

  • Simpler is better.
  • When there are contradictory instructions, it is better to stop what you are doing, move on, and resume after waiting at least eight hours.

:stop_sign: :flight_departure: :airplane: :alarm_clock: :flight_arrival:

(Oh wait, maybe it was a rhetorical question. :wink:)

1 Like

Nope, thanks much! :wink:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.