How to install ruby on RHEL 8 / CentOS 8

Ruby is a very flexible scripting language, it’s popularity well-earned by it’s power. In this tutorial we will install Ruby on a RHEL 8 / CentOS 8, and write the famous “Hello World” program to test that our installation is working as intended. Note however, that with all languages, Ruby’s capabilities are far more than simple text printing on the command line.

In this tutorial you will learn:

  • How to install Ruby with dnf
  • How to get Ruby version information
  • How to write and run the “Hello World” example program in Ruby

Hello World example with Ruby.

Hello World example with Ruby.

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System RHEL 8 / CentOS 8
Software Ruby 2.5
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to install ruby on Redhat 8 step by step instructions

The installation of Ruby is a one-liner, as the packages are present in the base repositories after we enable the subscription management sources.

  1. We’ll use dnf to install the required package and it’s dependencies:
    # dnf install ruby
  2. When the installation is finished, we can query for the version of Ruby:
    $ ruby -v
    ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
  3. If the output is similar to the above, we are ready to write the “Hello World” program. With a text editor, such as vim, we create a new text file called helloworld-from-ruby.rb with the following content:
    #!/usr/bin/ruby
    print "\nHello World from Ruby!\n\n"

    We have added the first line so the shell can recognize what will interpret the contents of the file. If we omit it, we could run the program by prefixing the filename with the interpreter:

    $ ruby helloworld-from-ruby.rb


    But with these two lines, we can run it like any other shell script. That is, we need to set execute right on it:

    $ chmod +x helloworld-from-ruby.rb

    And simply start it:

    $ ./helloworld-from-ruby.rb 
    
    Hello World from Ruby!

    Which verifies our successful installation of Ruby.