How to Install Ruby in Termux and Run a Script

How to Install Ruby in Termux and Run a Script

Want to run Ruby programs on your Android phone? You're in the right place. Termux brings a full Linux environment right to your mobile device, meaning you can code, build, and test applications without needing a laptop. Whether you're a student learning object-oriented programming or a developer testing a script on the go, setting up Ruby is surprisingly straightforward.

In this guide, we'll walk you through how to install ruby termux, handle common roadblocks—including the dreaded termux ruby gem install error—and write your very first working script.

Quick Summary: Ruby in Termux at a Glance

If you need a fast answer on what you're dealing with, here is a quick breakdown:

  • Primary Tool: Termux terminal emulator for Android.
  • Core Package Name: ruby
  • Recommended Build Tools: build-essential (crucial for gems with native extensions).
  • Primary Search Intent: Installing Ruby, running scripts, and fixing installation errors on mobile.

Prerequisites Before You Begin

Before we dive into the installation process, let's make sure your environment is ready. You don't need a rooted phone, but you do need a recent version of Termux. (Note: Avoid the Google Play Store version of Termux, as it is deprecated and no longer receives updates. Grab the latest version from F-Droid or GitHub instead).

Once you have your terminal open, always start by updating your package lists. This prevents missing dependency errors later.

pkg update && pkg upgrade -y

Let's get into the step-by-step installation.

Step-by-Step Guide: How to Install Ruby Termux

Installing Ruby on your Android device takes just a couple of commands. Follow these steps carefully:

  1. Open your Termux app.
  2. Run the install command block to fetch Ruby and its essential build tools.

The Install Command Block

Type or paste the following command into your terminal and hit enter:

pkg install ruby build-essential -y

Why do we include build-essential? When installing certain Ruby gems, your device needs to compile native C/C++ extensions. Without build tools like make and gcc, those gem installations will fail outright. Getting them now saves you a massive headache later.

Once the installation finishes, verify that Ruby was installed correctly by checking its version number:

ruby -v

If you see a version number pop up (like Ruby 3.x.x), congratulations! You've successfully installed Ruby on Termux.

How to Write and Run Your First Ruby Script

Now that the language is installed, let's create a real use-case example. We'll write a simple script that greets the user and performs a basic calculation.

First, create a new file named hello.rb using your favorite command-line text editor (like nano) or by using a simple echo command:

nano hello.rb

Inside the editor, type the following code:

# 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 the file and exit the editor (in nano, press Ctrl+O, then Enter to save, and Ctrl+X to exit).

Now, run your script using the Ruby interpreter:

ruby hello.rb

You should see your script execute smoothly right inside your mobile terminal!

Troubleshooting: Fixing the Termux Ruby Gem Install Error

One of the most common issues developers face is hitting a termux ruby gem install error when trying to install packages like Nokogiri, Rails, or other gems that require compilation. Let's look at 2-3 common errors and how to fix them.

Error 1: Failed to build native extension (Missing headers or build tools)

The Symptom: You run gem install [gem-name], and the terminal vomits a wall of red text ending in mkmf.rb can't find header files for ruby.

The Fix: This happens because Ruby development headers are missing. Fix it by installing the ruby development package:

pkg install ruby-dev -y

Error 2: Package compilation failure due to missing libraries (libffi, openssl, etc.)

The Symptom: Gem installation stops abruptly because it cannot find external libraries like libffi or zlib.

The Fix: Termux requires specific library packages to be present on your system architecture. Install common dependencies before trying your gem command again:

pkg install libffi zlib openssl -y

Error 3: Permission Denied or Read-Only File System errors

The Symptom: Gems fail to install because Ruby tries to write to a protected system directory.

The Fix: Instead of installing gems globally into system paths where permissions might block you, install them locally for your user space:

gem install --user-install [gem-name]

Comparison Note: Termux vs. Alternative Tools

You might wonder how running Ruby in Termux compares to using cloud-based IDEs, online compilers, or a dedicated desktop machine. Here is a quick comparison:

Environment Pros Cons
Termux (Local Android) Works completely offline; full terminal control; highly portable. Limited by phone screen size/keyboard; compiling heavy gems can drain battery.
Cloud IDEs / Replit No local storage required; heavy lifting done on cloud servers. Requires an active internet connection; potential latency.
Traditional PC / Laptop Fastest compilation speeds; ideal for massive Rails applications. Not portable; requires carrying hardware around.

For quick scripting, learning syntax, and managing lightweight automation tasks on the go, Termux wins out due to its offline capabilities and pure Linux environment.

Expert Tips for Ruby Development in Termux

  • Use a good keyboard: Standard mobile phone keyboards can make typing symbols like {}, #, and | tedious. Consider installing a keyboard app like Hacker's Keyboard for an easier coding experience.
  • Keep packages updated: Mobile environments change fast. Run pkg update periodically to keep your Ruby version and dependencies secure.
  • Leverage Git: Combine your Ruby setup with Git (pkg install git) to easily pull and test your repositories directly from GitHub on your phone.

Frequently Asked Questions (FAQ)

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 SQLite require additional package installations (pkg install sqlite postgresql). Keep in mind that heavy database compilation can be resource-intensive on mobile devices.

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 resides entirely within the Termux app ecosystem.

Why is my gem installation taking so long?

Compiling native C extensions on mobile CPU architectures (like ARM) requires processing power. If a gem takes a minute or two to build, be patient—your phone isn't frozen, it's just working hard to compile the source code.

Conclusion

Running Ruby on your Android device opens up a world of mobile programming possibilities. By following the steps outlined above, you can successfully install Ruby, write functional scripts, and troubleshoot common gem installation errors like a pro. Whether you're commuting, waiting in line, or just relaxing away from your desk, your phone is now a portable coding station.

Ready to take your mobile workflow further? Open up your terminal today, run your first installation command block, and start experimenting with your own custom Ruby scripts!