Thuta Learning
Ruby
BasicProgrammingBeginner

Get Started (Setup)

RubyLesson 325 min

What you'll walk away with

  • Choose an install route for your operating system: RubyInstaller on Windows, a version manager such as rbenv on macOS and Linux
  • Verify the installation with ruby -v and gem -v, and recognise a PATH problem when the command is not found
  • Create hello.rb and run it with ruby hello.rb from the folder that contains it
  • Use irb and ruby -e for one-line experiments, and ruby -c to check syntax

Explain it simply

Setup in plain words

Ruby code does nothing until a program called the Ruby interpreter reads it. Setting up Ruby therefore means three things: installing that interpreter, choosing a text editor to write code in, and learning to use a terminal to start the interpreter on your file. Think of a kettle: the kettle (the interpreter) must be plugged in, and you must know which switch (the terminal command) turns it on. The command ruby -v is the check that the plug is in: if it prints a version number, everything else will work.

Problem → Why → Solution

The problem: what does "command not found" really mean?

You install Ruby, open a terminal, type ruby -v, and the shell answers command not found. Or Ruby starts but complains about your file. Two different things have gone wrong, and to a beginner the two messages look alike. The first means the terminal cannot find the ruby program at all; the second means ruby ran fine but there is no file with that name in the folder you are standing in.

Fixing the wrong one can cost an evening: reinstalling Ruby does nothing for a file that sits in a different folder. On Windows cmd the first message appears in the form 'ruby' is not recognized as an internal or external command.

terminal
$ ruby -v
bash: ruby: command not found

$ ruby hello.rb
ruby: No such file or directory -- hello.rb (LoadError)

Read which program is complaining

In the first message the shell is complaining (bash: ruby: ...); in the second Ruby itself is complaining (ruby: No such file...). The name before the colon tells you who is speaking, and that decides whether to fix the installation or the file path. On Windows the name before the colon may be the full path of ruby.exe; the idea is the same.

Core concept: interpreter, editor, terminal and PATH

Setting up Ruby means getting three things to work together: the interpreter, a text editor and a terminal. The interpreter is a program called ruby; the editor is anywhere you can save plain text with a .rb extension (VS Code, for example); the terminal is where you type ruby hello.rb. The glue that holds them together is PATH, the list of folders your terminal searches whenever you type a command name. Installers add Ruby's bin folder to PATH, but a terminal that was already open during the install does not see the change, which is why opening a new terminal is the first fix for "command not found".

Which Ruby you install matters more than beginners expect. macOS ships with a Ruby of its own and Linux distributions package one, but that Ruby belongs to the operating system, may be old, and needs administrator rights to change. A version manager such as rbenv (with ruby-build) or RVM installs Ruby into your home folder instead, lets you keep several versions side by side and switch per project. On Windows the usual route is RubyInstaller, which sets up PATH for you; WSL2, a Linux environment inside Windows, is a popular alternative, and then the Linux advice applies.

OSCommon routeWorth knowing
WindowsRubyInstallerAdds Ruby to PATH; the Devkit variant offers an MSYS2 tools step that some gems need later
macOSA version manager such as rbenv, installed with HomebrewDo not use the built-in system Ruby for your own projects
LinuxA distribution package or a version managerPackages (for example ruby-full on Debian-based systems) are quick but may lag behind

Once Ruby is installed, three commands confirm everything. ruby -v prints the version, so you know which Ruby answers when you type ruby. gem -v checks RubyGems, the library installer that ships with Ruby. irb opens an interactive prompt where you type one line of Ruby at a time and see the result immediately, which makes it the best notebook for experiments. Your own programs live in files: save a plain text file named hello.rb and run it by name from the folder that contains it.

1. Install Ruby

Pick one route from the table for your OS. On Windows, keep the installer option that adds Ruby to PATH ticked.

2. Open a new terminal and check

Open a fresh terminal after the install and type ruby -v. A version number means the terminal can find Ruby.

3. Create hello.rb

Make a folder and save hello.rb in it with your editor. Lowercase names without spaces cause fewer errors.

4. Run it from the folder that holds it

Move the terminal into that folder with cd, then type ruby hello.rb.

Mental model: install, PATH, run
  1. Install Ruby
  2. A new terminal finds ruby on PATH
  3. ruby -v proves it works
  4. ruby hello.rb runs your file

If ruby is not found, fix the installation or PATH; if the file is not found, check which folder you are in.

General pattern: the ruby command

bash
ruby -v                  # which Ruby is on the PATH?
ruby filename.rb         # run a whole file
ruby -e 'code'           # run one line without a file
ruby -c filename.rb      # check the syntax only
irb                      # interactive prompt

Calling ruby with a file name runs the file. -v shows the version, -e runs the code that follows without needing a file, and -c checks syntax without running anything. irb is a separate command of its own.

Example: hello.rb with a version check

Save this as hello.rb and run ruby hello.rb. It prints a greeting, reports which Ruby implementation is running, and checks that the version is at least 3.0. The check uses Gem::Version because comparing version numbers as plain strings gives wrong answers.

hello.rb
# hello.rb
puts "Ruby is ready!"
puts "Interpreter: #{RUBY_ENGINE}"

minimum = Gem::Version.new("3.0")
current = Gem::Version.new(RUBY_VERSION)
puts "Version check passed" if current >= minimum

Code breakdown

  1. The first success lineputs "Ruby is ready!"

    If this line appears, the installation, the PATH and the file location are all correct. puts prints the text followed by a newline.

  2. A built-in constantputs "Interpreter: #{RUBY_ENGINE}"

    RUBY_ENGINE is a constant built into Ruby (the capital letters mark a constant). It is "ruby" on CRuby and "jruby" on JRuby.

  3. A version objectminimum = Gem::Version.new("3.0")

    Gem::Version is a class that understands how version numbers sort. The :: points at Version inside the Gem namespace. minimum becomes an object you can compare.

  4. The running versioncurrent = Gem::Version.new(RUBY_VERSION)

    RUBY_VERSION is a String such as "4.0.9". Wrapping it in Gem::Version gives two objects of the same kind, so the comparison can be trusted.

  5. A statement modifierputs "Version check passed" if current >= minimum

    An if at the end of the line is called a modifier: the statement before it runs only when the condition is true. "10.0.0" >= "3.0" is false (strings are compared character by character and "1" sorts before "3"), but with Gem::Version it is true.

You should see
Ruby is ready!
Interpreter: ruby
Version check passed

Three ways to run Ruby

The ruby command comes in different shapes for different purposes. Use a file for programs you want to keep, and -e for a quick calculation. When you want to explore, irb is better, because it shows the result of every expression after =>. On Windows, wrap the code of -e in double quotes and use single quotes inside it: ruby -e "puts 'hi'".

CommandWhen to use it
ruby file.rbPrograms you want to keep and run again
ruby -e '...'One quick calculation or check without creating a file
irbTrying ideas line by line and seeing every result
ruby -c file.rbChecking syntax without running anything
irb session
irb(main):001> 6 * 7
=> 42
irb(main):002> "ruby".upcase
=> "RUBY"
irb(main):003> exit

The session above shows the typical shape; the prompt may look slightly different depending on your irb version. Typing exit leaves irb.

Fixing "command not found"

1. Open a new terminal

Installers change PATH only for terminals opened afterwards. Close the old terminal and open a new one.

2. See what the shell sees

Type which ruby on macOS and Linux, or where ruby on Windows. A path tells you which Ruby is used; no output means Ruby is not on PATH.

3. Check the version manager

With rbenv, list what is installed with rbenv versions and pick one with rbenv global followed by a version. If the shell setup step was skipped, the shims will not work.

4. Reinstall with the PATH option

On Windows, if the steps above fail, run the RubyInstaller again and make sure the option that adds Ruby to PATH is ticked.

In real projects

Ruby versions in real projects

Teams rarely trust whatever Ruby happens to be installed. A project pins its version in a .ruby-version file that tools such as rbenv, RVM and chruby read, and a Gemfile can state the same requirement; Bundler refuses to continue when the running Ruby does not match. Continuous integration servers and Docker images then install exactly that version, so the program behaves the same on every laptop and server. The official ruby images on Docker Hub are tagged by version for this reason.

When you join a team, your first task is usually the same as this lesson: install the pinned Ruby, confirm it with ruby -v, and run the project's tests. Knowing how to diagnose a PATH problem and which Ruby is answering is a skill you will use again and again, because a machine can easily hold two Rubies, an old system one and a newer one you installed, and the one that comes first on PATH wins.

Common mistakes

Common beginner mistakes

1. Using a terminal opened before the install

If you keep using the terminal that was open when the installer finished, ruby is reported as command not found, because that terminal's PATH dates from before the install. Closing it and opening a new one is faster than reinstalling.

2. Wrong folder, or a hidden .txt extension

ruby hello.rb only looks for hello.rb in the current folder. Use pwd (cd on Windows) and ls (dir on Windows) to check where you are. On Windows, with extensions hidden, Notepad can save the file as hello.rb.txt, and ruby then reports No such file or directory -- hello.rb (LoadError).

3. Typing the prompt symbol too

Tutorials write $ ruby -v to show the prompt. If you type the $ yourself, the shell tries to run a command named $ and fails. Type only the text that follows the dollar sign.

Best practices

Best practices

  • Install Ruby with a version manager such as rbenv instead of using the system Ruby for your own projects, so you never need administrator rights to add gems.
  • Name files in lowercase snake_case without spaces, for example hello.rb or price_calculator.rb.
  • When you suspect a typo, check it with ruby -c file.rb first; it gives a quick answer and changes nothing.

Mini challenge

Mini challenge: a Ruby doctor script

Write doctor.rb, a script that tells you whether the Ruby on your machine meets a required version. Check two versions and print each result as yes or no.

  • Define at_least?(minimum) that compares two Gem::Version objects, one from RUBY_VERSION and one from minimum, and returns true or false
  • Define answer(flag) that turns true into "yes" and false into "no"
  • Print "Ruby 2.7 or newer: yes" and "Ruby 99.0 or newer: no" using both methods
  • Do not compare versions as plain strings, and be able to explain why not
doctor.rb
# doctor.rb
def at_least?(minimum)
  # TODO: compare Gem::Version objects, not plain strings
end

def answer(flag)
  # TODO: return "yes" for true and "no" for false
end

# TODO: print the result for "2.7" and for "99.0"

Expected: The program prints two lines (as long as your Ruby is between 2.7 and 99.0): Ruby 2.7 or newer: yes Ruby 99.0 or newer: no

Show hint

Gem::Version.new(RUBY_VERSION) >= Gem::Version.new(minimum) already returns true or false. Build the text with interpolation: "Ruby 2.7 or newer: #{answer(at_least?("2.7"))}". Using double quotes again inside #{} is fine in Ruby.

Show solution
doctor.rb
def at_least?(minimum)
  Gem::Version.new(RUBY_VERSION) >= Gem::Version.new(minimum)
end

def answer(flag)
  flag ? "yes" : "no"
end

puts "Ruby 2.7 or newer: #{answer(at_least?("2.7"))}"
puts "Ruby 99.0 or newer: #{answer(at_least?("99.0"))}"

Quick quiz 1 / 3

Right after installing Ruby, ruby -v reports command not found. What is the most likely cause?

Quick quiz 2 / 3

What does ruby -c hello.rb do?

Quick quiz 3 / 3

What does puts "10.0.0" >= "3.0" print, and why?

What to remember

  • Ruby setup needs an interpreter, an editor and a terminal, and PATH connects the terminal to ruby
  • ruby -v tells you which Ruby answers, ruby file.rb runs a file, ruby -c only checks syntax, and irb is for experiments
  • For command not found open a new terminal; for No such file check your folder and the file name
  • Compare version numbers with Gem::Version; as plain strings, "10.0.0" >= "3.0" is false

Ruby 4.0 Release — Ruby

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.