Download ArticleDownload Article

This How.com.vn teaches you how to run your Ruby script in Windows, macOS, and Ubuntu or Debian Linux. To run a Ruby program, you'll need to have Ruby installed on your computer. Although macOS and most Linux distributions come with versions of Ruby pre-installed, you'll want to make sure you're using the latest version before attempting to run your program. Also, if the Ruby code you want to run is something you've written in a text editor or developer environment, you'll want to save it as a .rb file so you can run it from the command line.

Running a Ruby Script

  • To run ruby code, type ruby <scriptname.rb> into your command prompt or terminal and press Enter or Return.
  • If you haven't installed Ruby, you can get it from Rubyinstaller.org (Windows), Homebrew (Mac), or your Linux system's package manager.
  • You can check your Ruby version using the command ruby -v.
Method 1
Method 1 of 3:

macOS

Download Article
  1. How.com.vn English: Step 1 Open the Terminal app.
    Your Mac has a Ruby interpreter built in to the operating system, so running Ruby scripts will be easy. To open the Terminal:
    • Click the Launchpad icon on the Dock (the multicolored squares).
    • Type terminal into the search field.
    • Click the Terminal icon.
  2. How.com.vn English: Step 2 Install the latest version of Ruby.
    The version that comes with your Mac is old and doesn't update with system updates. Here's how you can install the latest version:
    • If you don't have Homebrew installed, type /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" and press Return to install Homebrew.[1]
    • Type brew install ruby and press Return.
    • Type open -e ~/.zshrc and press Return to open your shell configuration file in TextEdit.[2]
    • Add the following lines to the end of the file if you have an Intel-based Mac:
      • 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 these lines to the end of the file if you have an Apple silicon-based Mac:
      • 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 re-open the Terminal window.
    • Type brew pin ruby and press Return.
    Advertisement
  3. How.com.vn English: Step 3 Use the cd command to go to the proper directory.
    When you open the Terminal, you'll be in your home directory. You'll want to open the directory in which your Ruby script is saved. For example, if your script is on your desktop, you'd type cd Desktop and press Return.
    • You can see a list of files in the current directory by typing ls -a and pressing Return.
  4. How.com.vn English: Step 4 Type ruby scriptname.rb and press ⏎ Return.
    Replace scriptname.rb with the actual name of the Ruby script you want to run. This executes your Ruby script.
  5. Advertisement
Method 2
Method 2 of 3:

Windows

Download Article
  1. How.com.vn English: Step 1 Install Ruby on your PC.
    If you haven't already done so, you'll need to install Ruby for Windows, which you can get at https://rubyinstaller.org/downloads. Installation is easy—just double-click the downloaded file and follow the on- screen instructions to install.
    • If you're not sure which version to download, look in the right column of the installer's website to see which is recommended.[3]
    • When installing, keep the defaults settings unless you know what you need to change. The defaults will add the Ruby directory to your system path so you can run the ruby command from the command prompt.
  2. How.com.vn English: Step 2 Open the Start Command Prompt with Ruby app.
    You'll find it in your Start menu after installing Ruby.[4]
    • You can also just click the Search bar (or the magnifying glass icon) next to the Start button, type Command, and then click Start Command Prompt With Ruby in the search results.
  3. How.com.vn English: Step 3 Use cd to change the directory to that of your Ruby script.
    When you open the Command Prompt, you'll be in your home directory (usually C:\Users\yourname). So if your Ruby script is on your desktop, you'd type cd Desktop or C:\Users\yourname\Desktop and press Enter.
  4. How.com.vn English: Step 4 Type ruby scriptname.rb and press ⏎ Return.
    Replace scriptname.rb with the actual name of the Ruby script you want to run. This runs your Ruby script.
  5. Advertisement
Method 3
Method 3 of 3:

Debian and Fedora Linux

Download Article
  1. How.com.vn English: Step 1 Open a terminal window.
    You can do so by pressing Control + Alt + T or by clicking the Terminal icon in your app list.
  2. How.com.vn English: Step 2 Type ruby -v and press ↵ Enter.
    This checks your version of Ruby. If you're using a version that's earlier than 2.7.1, you'll want to upgrade.
  3. How.com.vn English: Step 3 Install or update Ruby if necessary.
    If you don't have Ruby or are using an earlier version, here's how to get it:
    • Type sudo apt-get update and press Enter to update your package list.
    • Type sudo apt-get install ruby-full and press Enter to install the latest version of Ruby.
  4. How.com.vn English: Step 4 Use cd to change the directory to that of your Ruby script.
    For example, if the script is in a folder called code inside of your home directory, you'd type cd code and press Enter.
    • Type ls -a and press Enter to see which files are inside the current directory.
  5. How.com.vn English: Step 5 Type ruby scriptname.rb and press ↵ Enter.
    Replace scriptname.rb with the actual name of the Ruby script you want to run. This runs your Ruby script.
  6. Advertisement

Expert Q&A

Search
Add New Question
  • Question
    How do I debug a Ruby code?
    How.com.vn English: Kevin Burnett
    Kevin Burnett
    Software Developer
    Kevin Burnett is a Software Developer with over 20 years of professional experience. He spent the majority of his career at Rosetta Stone, a language-learning software company. He has experience with both front and back-end development and works primarily in Ruby, Python, and JavaScript.
    How.com.vn English: Kevin Burnett
    Software Developer
    Expert Answer
    The best way to debug is to use a library. The best one, in my opinion, is Byebug. It's also the one that's recommended. You basically require it, and then you can start a debugger by calling the Byebug commands. When you're inside the debugger, you can do all of the things you can do within debuggers, like step through your program, figure out what variables are set, and check down your issue.
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!

      Expert Interview

      Thanks for reading our article! If you’d like to learn more about computer programming, check out our in-depth interview with Kevin Burnett.

      About This Article

      How.com.vn English: Kevin Burnett
      Written by:
      Software Developer
      This article was written by Kevin Burnett and by How.com.vn staff writer, Nicole Levine, MFA. Kevin Burnett is a Software Developer with over 20 years of professional experience. He spent the majority of his career at Rosetta Stone, a language-learning software company. He has experience with both front and back-end development and works primarily in Ruby, Python, and JavaScript. This article has been viewed 26,905 times.
      How helpful is this?
      Co-authors: 4
      Updated: March 27, 2024
      Views: 26,905
      Categories: Programming
      Thanks to all authors for creating a page that has been read 26,905 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement