You may be found some online interpreters to code, compile and run Ruby codes online, but if you are willing to have Ruby in your system terminal, you need to install it first.
In this article, we will discuss how we run a Ruby program in the system console and install Ruby in our system if necessary.
We will see an example relevant to the topic to make the topic easier. Also, we are going to see the installation process of Ruby in local systems so that you can take a look at it if Ruby is not present in your system.
Execute a Ruby Program in the Console
To execute a Ruby program in your console, follow the example below. In our below example, we are going to run a Ruby file named
example.rb, and the command will look like this:
And it will show you the output of the
Interactive Ruby Environment
If you want to code directly in the console, then you can start the Interactive Ruby Environment by the following command:
Now you can run the Ruby command directly in your console like the below:
irb(main):001:0> puts "DelftStack" DelftStack => nil irb(main):002:0>
Install Ruby in Your System
You cannot run the Ruby commands or files in your system if your system doesn’t contain Ruby. To install Ruby in your system environment, follow the below steps.
Before we start, we need to download the
RubyInstallerfrom this page.
Here you will have two steps, the first one is
with Devkit, and the second one is
without Devkit. Please note that if you are willing to work on a Ruby on Rails app, you must install the
Now when you are done with the first step, you need to open the installer and install it step by step like the below images:
Accept the license and click
Specify the path to install and click
Select components and click
When you are done with the installation process, an important checkbox on the final screen,
Run 'ridk install' to set up MSYS2will appear. You need to check it, and it will send you to a terminal window asking you to select a component to install.
You need to hit Enter, which will install the base
MSYS2system, check for the updates, and finally install the development toolchain.
Check if Ruby was installed successfully
To check if Ruby has been installed successfully in your system, open your terminal and type the below command.
If everything is fine, you will get the below output in your console.
ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x64-mingw-ucrt]
Now Ruby is available in your system, and you can run any Ruby program using your terminal.