Choose a system package or a managed Ruby
Linux distributions package Ruby differently. A distribution’s ruby package is maintained through its package manager and may be older than the newest upstream Ruby. It is convenient for simple scripts and software that expects the distribution version. Avoid changing that package manually, because other tools may depend on it.
For a project that requires a particular maintained Ruby release, a version manager such as rbenv installs a separate Ruby under your user account. Check Ruby’s branch-status page and your project’s requirements before choosing. Do not assume that the newest upstream Ruby is compatible with every existing project.
Install with your distribution’s package manager
Use the command for your distribution, not all three. Debian and Ubuntu provide ruby-full; Fedora provides ruby; Arch Linux provides ruby. Package names and bundled versions can differ across releases, so check the distribution’s package page when a command is unavailable. These commands install the distribution-managed Ruby and may prompt for administrator approval.
After installation, verify with ruby --version and command -v ruby. If the version is too old for your project, use a version manager instead of replacing OS files. Do not use sudo gem install as a default way to add project packages to a system Ruby.
Debian or Ubuntu
sudo apt-get update
sudo apt-get install ruby-fullFedora
sudo dnf install rubyArch Linux
sudo pacman -S rubyPrepare to build a separately managed Ruby
rbenv with ruby-build can compile a Ruby release for your user account. A source build needs a compiler and development headers; missing OpenSSL, zlib or libyaml headers are common causes of failure. The dependencies vary by distribution and Ruby version. For Debian or Ubuntu, the ruby-build maintainers list the packages below for recent stable Rubies. On Fedora or Arch, follow the distribution-specific list in ruby-build’s suggested build environment rather than copying an apt command.
You need Git to obtain rbenv and ruby-build from their official repositories. Install Git through your distribution’s package manager if it is not already available. Avoid piping a downloaded script into a shell or changing system-wide permissions to make a build pass.
Debian or Ubuntu — compiler and common Ruby build headers
sudo apt-get install git build-essential autoconf libssl-dev libyaml-dev zlib1g-dev libffi-dev libgmp-dev rustcInstall rbenv and select a Ruby
Follow rbenv’s official Git-checkout instructions if your distribution’s rbenv package is unavailable or old. Clone rbenv into your home directory, initialize it for your shell, and restart that shell. Add ruby-build as the rbenv plugin so rbenv install is available. Before running the version commands, consult Ruby’s branch-status page and replace X.Y.Z with the complete maintained version compatible with your project. rbenv global sets your user default; rbenv local X.Y.Z, run inside a project folder, selects its project version.
The commands below are for a typical POSIX-compatible shell. If your shell or distribution uses different startup files, follow the rbenv instructions for that shell. They do not replace your distribution-managed Ruby.
Terminal — install rbenv and ruby-build from their official repositories
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
~/.rbenv/bin/rbenv initTerminal — after restarting the shell
git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
rbenv install -l
rbenv install X.Y.Z
rbenv global X.Y.ZVerify Ruby and run a file
Run ruby --version and gem --version to confirm the tools are available. If you installed through rbenv, run rbenv which ruby to see which executable was selected. Save the line below as hello.rb and run ruby hello.rb in its folder. The expected output is Hello, Codectionary!. Ruby itself is enough for this example; Rails and extra gems are not required.
If the file is not found, use pwd and ls to check your current directory and filename. A .rb file is plain text, not a system package.
Terminal — inspect Ruby
ruby --version
gem --version
command -v rubyhello.rb
puts "Hello, Codectionary!"Terminal — run the script
ruby hello.rbPATH, permissions and compiler errors
If ruby is not found, start a new shell and inspect command -v ruby. For rbenv, complete its shell initialization and compare with rbenv which ruby; if you installed a system package, check whether its package manager reported a successful install. When gem installation requests system-directory write permission, first check whether the shell is using the OS Ruby instead of your user-managed Ruby. Do not change ownership of /usr or use sudo gem install as a routine fix.
When compiling fails, read the build log for the missing library, header or compiler. Install the relevant development package for your distribution using ruby-build’s current guidance, then retry. Build requirements and compiler compatibility change, so a command copied for a different distribution or an end-of-life Ruby branch may not work.
Syntax
ruby --version
ruby hello.rbExamples
Run a Linux Ruby script
Save as hello.rb and run ruby hello.rb in a terminal.
puts "Hello, Codectionary!"- The saved .rb file is Ruby source.
- puts writes the String and a newline to standard output.
- The terminal shows Hello, Codectionary! when the selected Ruby runs the file.
Best practices
- Choose between a distribution package and a user-managed Ruby for the project’s needs.
- Check branch maintenance and project compatibility before selecting a Ruby release.
- Read compiler errors and use distribution-specific dependencies; avoid blanket permission changes.
Runtime & versions
Ruby interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Ruby documentation