Install Ruby in Termux (2026): Setup, Errors & First Script
How to Install Ruby in Termux and Run Your First Script (2026 Guide)
Want to run Ruby on your Android phone without a laptop? Termux turns your device into a full Linux terminal, so you can install Ruby, write scripts, and even test Rails apps on the go. This guide covers the correct install steps, a real working script, and fixes for the errors people actually hit — including one that most tutorials get wrong.
Quick Summary: Ruby in Termux at a Glance
- App needed: Termux, from F-Droid or GitHub (the Play Store version is outdated and no longer supported). New to Termux? Start with how to install Termux on Android.
- Core package:
ruby - Build tools package:
build-essential(needed for gems with native extensions) - Root required: No
- Common failure point: Installing gems that compile C extensions (Nokogiri, Rails, etc.)
Before You Start
Open Termux and update your package lists first — this avoids missing-dependency errors later:
pkg update && pkg upgrade -y
If this step throws errors, see how to fix "unable to locate package" in Termux before continuing. It also helps to know the basic Termux commands if you're new to the terminal.
Step 1: Install Ruby in Termux
Run the following command:
pkg install ruby build-essential -y
build-essential pulls in clang, make, and other compiler tools. Many Ruby gems need to compile native C/C++ extensions on install, and without these tools that compilation fails immediately — so install them now rather than after hitting an error.
Confirm the install worked:
ruby -v
A version number (like Ruby 3.x.x) confirms Ruby is ready to use.
Step 2: Write and Run Your First Ruby Script
Create a file using nano (see the full guide to Nano and Vim in Termux if you're unfamiliar with either editor):
nano hello.rb
Paste this in:
# A simple Ruby script for Termux
puts "Hello from Ruby running on Android!"
print "Enter your name: "
name = gets.chomp
puts "Welcome, #{name}! Let's do some math."
sum = 10 + 15
puts "The sum of 10 and 15 is: #{sum}"
Save with Ctrl+O, then Enter, and exit with Ctrl+X. Now run it:
ruby hello.rb
You now have a working Ruby script running natively on your phone.
Fixing the Termux Ruby Gem Install Error
This is where most guides go wrong — including telling you to install a ruby-dev package. That package doesn't exist in Termux. Ruby's header files are already bundled inside the main ruby package, so pkg install ruby-dev will fail with "no installation candidate." Here's what actually causes gem install failures, and the real fixes.
Error 1: "mkmf.rb can't find header files for ruby"
The Cause: Missing compiler/build tools, not missing headers.
The Fix: Make sure build-essential installed correctly, then retry the gem install:
pkg install build-essential -y
gem install [gem-name]
Error 2: Native extension fails to build (Nokogiri and similar gems)
The Cause: The gem needs external C libraries that Termux doesn't install by default.
The Fix: Install the specific libraries the gem needs, then point the install at your system libraries:
pkg install libxml2 libxslt libffi openssl pkg-config -y
gem install nokogiri -- --use-system-libraries
Error 3: Permission denied writing to system gem directory
The Fix: Install gems into user space instead of protected system paths:
gem install --user-install [gem-name]
Comparison Note: Termux vs. Alternative Tools
| Environment | Pros | Cons |
|---|---|---|
| Termux (Android) | Fully offline; real Linux terminal; highly portable. | Small on-screen keyboard; native gem compilation can be slow. |
| Cloud IDEs / Replit | No local setup; heavy lifting done on cloud servers. | Requires an active internet connection; potential latency. |
| Traditional PC / Laptop | Fastest compile speeds; ideal for large Rails applications. | Not portable; requires carrying hardware around. |
For quick scripting, learning syntax, and lightweight automation on the go, Termux wins on offline capability and a genuine Linux environment.
Running Ruby on Rails in Termux
Rails installs the same way as any other gem:
gem install rails --no-document
For a database, you'll need to install one separately — see how to install MySQL/MariaDB in Termux, or use SQLite for lighter local projects. If your Rails app needs asset compilation, you'll also want Node.js and npm installed in Termux. Once your app runs, you can serve it locally — see how to host a local website using Termux.
Expert Tips for Ruby Development in Termux
- Use version control: Combine your Ruby setup with Git in Termux to pull and test repositories directly on your phone.
- Automate repeat tasks: Wrap common commands into a Bash script in Termux instead of retyping them each time.
- Keep packages updated: Run
pkg updateperiodically — Termux packages move fast. - Fix storage errors early: If Ruby scripts can't read or write files on your SD card, check how to fix Termux storage permission issues.
Frequently Asked Questions (FAQ)
Do I need root access to run Ruby in Termux?
No. Termux runs inside an unprivileged container sandbox on your Android device — everything you install stays inside the Termux app.
Can I build full Ruby on Rails apps in Termux?
Yes, you can install Rails via gem install rails, though database setups like PostgreSQL or MySQL require extra package installations and heavier compilation on mobile hardware.
Why is my gem installation taking so long?
Compiling native C extensions on a phone's ARM processor takes longer than on a desktop CPU. If a gem takes a minute or two to build, your phone isn't frozen — it's just working hard to compile the source code.
What if I need Python or PHP instead of Ruby?
Termux supports those too — see installing Python in Termux or installing PHP in Termux.
Conclusion
Ruby on Termux gives you a real, portable coding environment — install, script, and troubleshoot without needing a computer. Now that Ruby is running, open your terminal and start building your own scripts.
Join the conversation