How to Set Ruby Environment in Linux

How to set Ruby Environment in Linux?

How did you install your ruby? apt-get install ? I highly recommend using RVM it will make your life easier. But in General you can set up your Environment variables in Linux in a standard way:

To display current environment:

set

You can modify each environmental or system variable using the export command. Set the PATH environment variable to include the directory where you installed the bin directory with perl and shell scripts:

export PATH=${PATH}:/home/username/bin

OR

export PATH=${PATH}:${HOME}/bin

You will need to check out the path of your Ruby file using

which ruby 

Then to set it up:

export PATH=${PATH}:/Users/info/.rvm/rubies/ruby-2.0.0-p353/bin

The ~/.bash_profile ($HOME/.bash_profile) or ~/.prfile file is executed when you login using console or remotely using ssh. Add the path to ~/.bash_profile file

How can I add/set Ruby path on bash?

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

The environment variables for Ruby on Rails I set on my Ubuntu server are not being read

here is what you could do, open(create one if it does not exist) the .bash_profile file.

Add all the export lines into the file.

export SENDGRID_USERNAME=""
export SENDGRID_PASSWORD=""
export DATABASE_NAME=""

then save the file and run source ~/.bash_profile. Then open rails console and either run ENV or ENV["SENDGRID_USERNAME"], you should see the value you have assigned to the corresponding key.

Also as an alternative we have gem which you could consider once you know how .bash_profile setup works.

How configure bash environment with rbenv?

I've manually update the file .bash_profile to unset all variables environnements which in use with rvm and now it works perfectly :

$ cat /home/atlantic/.bash_profile 
# include .bashrc if it exists
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

unset rvm_bin_path
unset GEM_HOME
unset IRBRC
unset MY_RUBY_HOME
unset rvm_path
unset rvm_prefix
unset rvm_version
unset GEM_PATH
unset RUBY_VERSION

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

The result :

$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 2.5.1
- RUBY VERSION: 2.3.1 (2016-04-26 patchlevel 112) [x86_64-linux]
- INSTALLATION DIRECTORY: /home/atlantic/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0
- USER INSTALLATION DIRECTORY: /home/atlantic/.gem/ruby/2.3.0
- RUBY EXECUTABLE: /home/atlantic/.rbenv/versions/2.3.1/bin/ruby
- EXECUTABLE DIRECTORY: /home/atlantic/.rbenv/versions/2.3.1/bin
- SPEC CACHE DIRECTORY: /home/atlantic/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /home/atlantic/.rbenv/versions/2.3.1/etc
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /home/atlantic/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0
- /home/atlantic/.gem/ruby/2.3.0
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /home/atlantic/.rbenv/versions/2.3.1/bin
- /home/atlantic/.rbenv/libexec
- /home/atlantic/.rbenv/plugins/ruby-build/bin
- /home/atlantic/.rbenv/shims
- /home/atlantic/.rbenv/bin
- /usr/local/rvm/gems/ruby-2.0.0-p247/bin
- /usr/local/rvm/gems/ruby-2.0.0-p247@global/bin
- /usr/local/rvm/rubies/ruby-2.0.0-p247/bin
- /usr/local/rvm/bin
- /usr/local/sbin
- /usr/local/bin
- /usr/sbin
- /usr/bin
- /sbin
- /bin
- /usr/games

Thanks for your suggestions and your help.
David.

Environment variables in ruby script on Linux

The .env file is loaded from the root of your project. Specifically in your case from the current directory. Try moving into the directory containing the .env file before executing the script.

cd /path/to/ && ./myscript.rb

Update:

You can load the .env file from the script path without moving there:

config = File.absolute_path(File.join(File.expand_path(__FILE__), '..', '.env'))
Dotenv.load(config)

Is it possible to set Ruby ENV with bash in Chef?

A better way to ask this would have been how to set a user as a Ruby ENV. Solved with the following, for UID=501:

ENV['CHEFUSER'] = Etc.getpwuid(501).name

How to set Ruby's load path externally

See the "Ruby and Its World" chapter from The Pickaxe Book, specifically the section on environment variables. Excerpt:

RUBYLIB 
Additional search path for Ruby programs ($SAFE must be 0).
DLN_LIBRARY_PATH
Search path for dynamically loaded modules.
RUBYLIB_PREFIX
(Windows only) Mangle the RUBYLIB search path by adding this
prefix to each component.

How to change ruby version

There is lots of advise in the comments to your question, some of it is advanced-ish rbenv or rvm usage.

My advice: Decide on how to manage multiple rubies - either use your OS package manager (in your case the apt-get/PPA stuff) OR rvm OR rbenv.

For the OS package manager, there should be a way to call ruby with version explicitely (e.g. /usr/bin/ruby1.9.3), or research on and call update-alternative. As bundler comes with a gem, you might get the interpreters confused here.

For rvm, change ruby version with rvm use 2.5.1 (once it is installed).

For rbenv I actually do not know but it should be trivial, too (and people are happy with it; it just happens that I tried rvm first and it worked like a charm, never evaluated rbenv).

I usually install one "system" ruby (apt-get install ruby1.9.3) and use rvm afterwards. You can still switch to the packaged "production" ruby with rvm use system.

Update 2017: Most distros ship with a ruby version installed already, so you probably don't have to install it manually. Run ruby -v or which ruby to see if a ruby interpreter is already installed.

In your case I would probably deinstall all system rubys (apt-get purge ...), remove the PPAs, remove your ~/.rvm and rbenv and start from scratch (install packaged stable ruby, then rvm and use rvm (r.g. rvm install 2.3.1) from there on).

Installing Ruby on Linux

  • Update your package manager first:

    sudo apt-get update
  • This must finish without error or the following step will fail.
    Install curl:

    sudo apt-get install curl
  • You’ll use curl for installing RVM.

  • Install RVM

    curl -L https://get.rvm.io | bash -s stable --ruby
  • You Already Have RVM Installed
    If you already have RVM installed, update it to the latest version and install Ruby:

    rvm get stable --autolibs=enable
  • Install Ruby:

    rvm install ruby


Related Topics



Leave a reply



Submit