Kickstart With Ruby Development
Purpose. Today I had to build a Jekyll pipeline so I needed a clean way to install native ruby and gems. Jekyll is a ruby gem which means it is build on top of ruby language.
Ruby is a dynamic, object-oriented programming language. Check https://www.ruby-lang.org/.
Install ruby
and `rubygems with rvm
We are using rvm
, which stands for Ruby Version Manager, which makes easier to install Ruby on Linux platform. rvm
is also helpful for managing multiple version of Ruby without conflicting, and we can switch system to any version of Ruby using a single command.
For ruby
visit: https://www.ruby-lang.org/en/
For installing rvm
https://rvm.io/rvm/install
Disclaimer: The following software to be installed under a specific user not root
!
curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
rvm --version
Check what versions are available on the market
rvm list known
or check versions available in your system:rvm list
You will install ruby
(programming language interpreter) using rvm
(ruby installer),
Running rvm get stable updates RVM to the latest stable version:
rvm get stable
Install ruby (using rvm) and set it’s latest version as default. You may remove old relics: older versions of ruby. Check the latest version of ruby on their site.
rvm install ruby-3.1.0
Tips! the command will install all dependencies: curl gpg gcc gcc-c++ make patch autoconf automake bison libffi-devel libtool patch readline-devel sqlite-devel zlib-devel openssl-devel
Or install a specific version rvm install 2.5.7
. Confirm the version: ruby -v
ruby -v
ruby lists
Pick your version
rvm use 3.1.0 --default
Additionally you may remove old versions:rvm remove ruby-2.3.0
You might need to add the current user (spokane) to the rvm group: sudo usermod -aG rvm spokane
Now there is a strange thing that happens to me lately. I installed ruby
with rvm
. Then i installed gem install bundler jekyll
but every time i reopen another terminal or restart a computer it dissapears. It is because Ruby isn’t persisting correctly when installed via rvm
, which typically happens if the environment isn’t properly loaded in new terminals. Some troubleshooting steps:
In Guake Terminal (which I use) or Gnome terminal that is: Preferences → Your Profile → “Command”. Check “Run command as a login shell”
Ensure RVM is properly loaded Add the following to your shell config file (~/.bashrc
, ~/.zshrc
, etc.):
echo 'source ~/.rvm/scripts/rvm' >> ~/.bashrc
source ~/.bashrc
After making these changes, restart your terminal and check if Ruby persists.If needed a manually loading that is: Open your terminal and run:
/bin/bash --login
rvm use ruby --default
Rubygems. A Medium Level Package Manager.
Rubygems
is a medium level package manager necessary because ruby
, at its lowest level, doesn’t really have “libraries” built in. It has the ability to “load” or “require” a file, and it has $LOAD_PATH, an array of paths to check when you ask for a filename.
rubygems
are automatically installed when you install ruby
with rvm
.
The rubygem package managers fetches gem packages from this hub: https://rubygems.org/
You can install gems at system level or at project level.Make sure you’ll have latest lists sudo gem update
.Then check the system overall: sudo gem update --system
. Each level contains a Gemfile
and a Gemfile.lock
in the root where the gem (and their versions) are listed.
Tips! It is best to avoid installing Ruby Gems as the root user.
Therefore, we need to set up a gem installation directory for your user account.
The following commands will add environment variables to your ~/.bashrc
file to configure the gem
installation path.
echo '# Install Ruby Gems to ~/gems' >> ~/.bashrc
echo 'export GEM_HOME="$HOME/gems"' >> ~/.bashrc
echo 'export PATH="$HOME/gems/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
NOTE: In order to have a jekyll pipeline these steps needs to be run on both local and on the server
Interactive Ruby
Open up a shell and type irb
and hit enter.
irb(main):003:0> 3+2
=> 5
irb(main):003:0> puts "Hello World"
Hello World
=> nil
Run your first program
$ vim hello.rb
then type inside
puts "Hello world, this is a message targeting all of you."
puts "Input your name please: "
inp = gets
puts "Thanks " + inp + " for providing your name. We will send this name to aliens soon ;)"
run it in the interpreter.
ruby hello.rb
Ruby functions
def print_hi(name)
puts "Hi, #{name}"
end
print_hi('Yuri')
#=> prints 'Hi, Yuri' to STDOUT.
Debugging
Always use rvm requirements
to investigate issues
rvm requirements
Warning! PATH is not properly set up, /home/git/.rvm/gems/ruby-2.7.1/bin is not at first place.
Usually this is caused by shell initialization files. Search for PATH=... entries.
You can also re-add RVM to your profile by running: rvm get stable --auto-dotfiles
To fix it temporarily in this shell session run: rvm use ruby-2.7.1
To ignore this error add rvm_silence_path_mismatch_check_flag=1 to your ~/.rvmrc file.
Checking requirements for centos.
Requirements installation successful.
If the above error occurs run rvm reset
. Update the system environment variables:source /etc/profile.d/rvm.sh
Conclusions:
Now you are set to develop your project which could be ruby gems or full fledged applications on Ruby on Rails or static sites like jekyll.
Unlike rubygems and bundler(package managers), rvm is a version manager.