Kickstart with Ruby
Today I had to build a Jekyll pipeline so I needed a clean way to install native ruby and gems.
Ruby is a dynamic, object-oriented programming language.
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.
Disclaimer: The following software to be installed under a specific user not root!
Install rvm (Ruby Version Manager).
Note: unlike rubygems and bundler(package managers), rvm is a version manager
Get the gpg keys.
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
Fetch the script and run it in bash.
\curl -sSL https://get.rvm.io | bash
source ~/.rvm/scripts/rvm
rvm -v
Helpful. Check what versions are available rvm list known
Install ruby
rvm install ruby
note, the command will install all dependencies: sudo yum install 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
Or specify another version rvm use 2.5.6 --default
Check for troubles:
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
Add current user (spokane) to the rvm group
sudo usermod -aG rvm spokane
rvm reload
Note RVM reloaded! Yep!
You need that as dependencies for jekyll
rubygems
Rubygems is a medium level package manager: 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
Install dependencies (around 6M)
It will install curl gpg gcc gcc-c++ make patch autoconf automake bison libffi-devel libtool patch readline-devel sqlite-devel zlib-devel openssl-devel
sudo dnf install ruby-devel @development-tools
sudo dnf install rubygems
Make sure you’ll have latest lists sudo gem update
Then check the system overall: sudo gem update --system
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
ruby hello.rb
Ruby functions
def print_hi(name)
puts "Hi, #{name}"
end
print_hi('Yuri')
#=> prints 'Hi, Yuri' to STDOUT.