0

Firstly, I am new to Linux so excuse me if any terminology is wrong; I'll try to phrase the problem as competently as possible.

I have installed Ruby (2.4.0) via Linuxbrew. The ruby command works fine; it installed correctly. However, when I try to use the gem command (which Ruby should have installed) I receive this error: bash: /usr/bin/gem: No such file or directory

Now, because I installed this with Linuxbrew I know that this directory isn't correct. For example:

result of which gem : /home/me/.linuxbrew/bin/gem

result of which ruby : /home/me/.linuxbrew/bin/ruby

Therefore, it seems gem is installed but the gem command isn't linked to the correct path. I assume I need to direct the gem command to the path of which gem as opposed to /usr/bin/gem that bash is saying doesn't exist. How would I go about changing this? I tried in vain to change the bash_profile but I'm not sure what to do.

Again, excuse me if ruby and gem are not referred to as commands and if the problem isn't the "default directory" as stated in the title. I wasn't sure how to label it.

EDIT/TL;DR:

Basically, how can I make gem execute this: /home/me/.linuxbrew/bin/gem instead of looking for the program in /usr/bin/gem?

jww
  • 97,681
  • 90
  • 411
  • 885
swhizzle
  • 141
  • 10
  • When you invoke `gem` in a shell, the shell looks for an executable named `gem` in all the directories listed in the PATH environment variable. (`echo $PATH` to see this colon separated list). For a short term fix, prepend `/home/me/.linuxbrew/bin` to the path with `PATH=/home/me/.linuxbrew/bin:$PATH`. – William Pursell Mar 24 '17 at 21:40
  • [How to check if a program exists from a Bash script?](https://stackoverflow.com/q/592620/608639), [How to check if command exists in a shell script?](https://stackoverflow.com/q/7522712/608639), etc. – jww Oct 05 '19 at 02:53

3 Answers3

1

Instead of running gem, run /home/me/.linuxbrew/bin/gem, i.e. type the full path name (followed by any arguments you may need).

If this becomes too tiresome, you could change your PATH. Prepend your bin directory with

PATH=$HOME/.linuxbrew/bin:$PATH
Jens
  • 69,818
  • 15
  • 125
  • 179
1

First, the reason you get the error /usr/bin/gem not found, is that earlier in the same shell session, the file used to be there. Bash will cache this to speed things up when running the same command many times. Running hash -r will clear this.

Editing PATH you seem to have managed, hence the which command gives the result it does.

Jens
  • 69,818
  • 15
  • 125
  • 179
Stian Skjelstad
  • 2,277
  • 1
  • 9
  • 19
0

To answer my own question-

As I had previously installed and uninstalled Ruby via apt-get instead of Linuxbrew in the same Terminal window, Bash was looking for gem in usr/bin as opposed to the path specified in my bash_profile to Linuxbrew.

Therefore, Stian's answer above with hash -r would also work, I am sure.

swhizzle
  • 141
  • 10