Thecave3 Thecave3 - 6 months ago 22
Ruby Question

How can I add/set Ruby path on bash?

I'm having an issue while installing wp-scan, expecially with the command:

sudo gem install bundler && bundle install --without test


which returns:

WARNING: You don't have /root/.gem/ruby/2.3.0/bin in your PATH,
gem executables will not run.
Successfully installed bundler-1.12.4
Parsing documentation for bundler-1.12.4
Done installing documentation for bundler after 5 seconds
1 gem installed
bash: bundle: command not found


I searched and it seems I didn't set the Ruby path, but when I try to set it using "Setup", bash returns:

-e:1: unexpected fraction part after numeric literal
/root/.gem/ruby/2.3.0/
^


This is the .bashrc file:

#
# ~/.bashrc
#

# If not running interactively, don't do anything
[[ $- != *i* ]] && return

alias ls='ls --color=auto'
PS1='[\u@\h \W]\$ '
# >>>>BEGIN ADDED BY CNCHI INSTALLER<<<< #
BROWSER=/usr/bin/chromium
EDITOR=/usr/bin/nano
# >>>>>END ADDED BY CNCHI INSTALLER<<<<< #

#la riga inferiore serve a creare il path corretto di gem /ruby
PATH="$(ruby -e '/root/.gem/ruby/2.3.0/')/bin:$PATH"


What am I doing wrong?

Here is the gem env return:

RubyGems Environment:
- RUBYGEMS VERSION: 2.5.1
- RUBY VERSION: 2.3.1 (2016-04-26 patchlevel 112) [x86_64-linux]
- INSTALLATION DIRECTORY: /usr/lib/ruby/gems/2.3.0
- USER INSTALLATION DIRECTORY: /home/thecave3/.gem/ruby/2.3.0
- RUBY EXECUTABLE: /usr/bin/ruby
- EXECUTABLE DIRECTORY: /usr/bin
- SPEC CACHE DIRECTORY: /home/thecave3/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /etc
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /usr/lib/ruby/gems/2.3.0
- /home/thecave3/.gem/ruby/2.3.0
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- "gem" => "--user-install"
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /usr/local/sbin
- /usr/local/bin
- /usr/bin
- /usr/bin/site_perl
- /usr/bin/vendor_perl
- /usr/bin/core_perl
- /root/.gem/ruby/2.3.0/bin

Answer

Adding the following to your .bashrc should do the trick:

export PATH="$PATH:/root/.gem/ruby/2.3.0/bin"

$(..) is a command substitution and it a way to capture the output from a command:

a=$(echo 1234)

will assign the value 1234 to a, of course, the same can be achieved with a=1234.

ruby -e '...'

will evaluate the code passed after -e, in your case /root/.gem/ruby/2.3.0/ which is not valid Ruby code, but a path to find executables.

I also added the Ruby path to the end of the PATH variable. This is considered the best approach since the shell will search though it to find the desired program. Consider this:

~/bin % export PATH="$HOME/bin:/bin:/usr/bin" 
~/bin % cat grep
#!/bin/sh
echo "got ya"
~/bin % grep '...' '...'
got ya