Today, Mytour will guide you on executing Ruby code across Windows, macOS, Ubuntu, and Debian Linux. To run a Ruby program, the Ruby software must be pre-installed on your computer. While macOS and most Linux distributions come with Ruby pre-installed, ensure you have the latest version before running your program. Additionally, if the Ruby code you need to deploy is written by you using a text editor or developer environment, save it as a .rb file to execute it from the command line.
Steps
On macOS

Open the Terminal application. Macs come with the Ruby interpreter pre-installed in the operating system, making it straightforward to run Ruby scripts. To open Terminal, you need to:
- Click the Launchpad icon in the Dock (multicolored squares).
- Type terminal in the search field.
- Click the Terminal icon.

Install the latest version of Ruby. The version pre-installed on your Mac might be outdated and not updated with system upgrades. Follow these steps to install the newest version:
- If Homebrew is not installed, enter /bin/ -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" and press Return to install Homebrew.
- Type brew install ruby and press Return.
- Enter open -e ~/.zshrc and press Return to open the shell configuration file in TextEdit.
- Add the following lines at the end of the file if your Mac uses an Intel-based chip:
- if [ -d "/usr/local/opt/ruby/bin" ]; then
- export PATH=/usr/local/opt/ruby/bin:$PATH
- export PATH=`gem environment gemdir`/bin:$PATH
- fi
- Add the following lines at the end of the file if your Mac uses an Apple silicon chip:
- if [ -d "/opt/homebrew/opt/ruby/bin" ]; then
- export PATH=/opt/homebrew/opt/ruby/bin:$PATH
- export PATH=`gem environment gemdir`/bin:$PATH
- fi
- Save and close the file.
- Close and reopen the Terminal window.
- Enter brew pin ruby and press Return.

Use the cd command to navigate to the appropriate directory. When Terminal opens, the default location is your home directory. To run Ruby code, you need to access the folder where the Ruby script is saved. For example, if the script is on your desktop, type cd Desktop and press Return.
- You can view the list of files in the current directory by typing ls -a and pressing Return.

Type ruby scriptname.rb and press ⏎ Return. Replace scriptname.rb with the actual name of the Ruby script you want to run. The Ruby script will then execute.
On Windows

Install Ruby on your PC. If Ruby is not already installed, download the Windows version from https://rubyinstaller.org/downloads. The installation process is straightforward—double-click the downloaded file and follow the on-screen instructions.
- If you're unsure which version to download, check the recommended option in the right column on the installer's website.
- During installation, keep the default settings (unless you know what changes are needed). The defaults will add Ruby to your system path, allowing you to run ruby commands from the command prompt.

Launch the Start Command Prompt with Ruby application. You can find it in the Start menu after installing Ruby.
- Alternatively, click the Search bar (or magnifying glass icon) next to the Start button, type Command, and select Start Command Prompt With Ruby from the search results.

Use the cd command to navigate to the directory containing the Ruby script. When Command Prompt opens, you'll be in the home directory (usually C:\Users\yourname). If the Ruby script is on the desktop, type cd Desktop or C:\Users\yourname\Desktop and press Enter.

Type ruby scriptname.rb and press ⏎ Return. Remember to replace scriptname.rb with the actual name of the Ruby script you want to execute. The Ruby script will then start running.
Debian and Fedora Linux

Open the command line window. You can do this by pressing Control + Alt + T or clicking the Terminal icon in the applications list.

Type ruby -v and press ↵ Enter. This command checks the Ruby version. If the current version is older than 2.7.1, consider upgrading.

Install or update Ruby if necessary. If Ruby is not installed or you're using an outdated version:
- Enter sudo apt-get update and press Enter to update the package list.
- Type sudo apt-get install ruby-full and press Enter to install the latest version of Ruby.

Use the cd command to navigate to the directory containing the Ruby script. For example, if the script is in a code folder within your home directory, type cd code and press Enter.
- Enter ls -a and press Enter to view files in the current directory.

Type ruby scriptname.rb and press ↵ Enter. Replace scriptname.rb with the actual name of the Ruby script you want to run. The Ruby script will then begin executing.