Install Ruby on Rails on a Raspberry Pi

22 October 2020
To leave a comment, type jam

The Ruby on Rails website asks you to "imagine what you could build if you learned Ruby on Rails." A few popular options include websites, careers, and businesses. In short—if you can build it on the web, you can build it with Rails.

This post shows one easy way to get started: installing Ruby on Rails onto the simplest computer on Earth: the Raspberry Pi. These instructions are aimed at beginners. Experienced programmers can also skip to an install script.

Our goal is the Rails welcome screen:

Ruby on Rails—Welcome Screen

If we see this screen, it means Rails is running on the Pi.

After you get through this guide, I highly recommend reading Michael Hartl's Rails Tutorial to learn the basics of Rails. But before all that, we need to get set up—and that's what this post covers.

Table of Contents - Install Ruby on Rails on Raspberry Pi

  1. About Rails on Pi
  2. For Programming Beginners (feel free to skip)
  3. Overview / Install Script
  4. Update Packages
  5. Install Foundational Packages
  6. Install RVM, Ruby, and Rails
  7. Install Yarn
  8. Create New Rails App

A. About Rails on Pi ↑ Table of Contents

Ruby on Rails ("Rails") is an open source web development framework. Rails is like Legos for building professional-grade web projects. Many high-profile websites, apps, blogs, newsletters, APIs, and other web-based tools were built on Rails (like Shopify, Twitter, Airbnb, GitHub, Pinterest, HEY).

The Raspberry Pi ("Pi") is an open source computer designed for learning. Pis are cheap, simple, and versatile. You can learn more about the Pi here, including how to make one from scratch.

Combining Rails and Pi, we can dramatically lower barriers to learning and building online.

B. For Programming Beginners ↑ Table of Contents

This installation occurs entirely at the command line. On the Pi, we use an application called the "terminal."

From the desktop, the terminal is here:

It opens this:

Terminal responds to commands:

Passing a string—"Welcome to the terminal!"—to the echo command

Note: This tutorial does not cover terminal commands beyond what we need to install Rails. For an intro to Linux commands, this is one place to start.

Quick word of encouragement

If you're new to command line computing, this tutorial will probably feel complicated and over your head. Many newbies give up during command line installations because things seem harder than they are (a shame on both points).

I encourage you to stick with it. It's worthwhile learning even if you end up moving to other technologies later.

When things seem complex, it's usually to accommodate the open source community in some meaningful way.1 One big upside to open source is a global community of smart people who make and maintain software for everyone to use. This community tends to prioritize accessibility above simplicity. It's a subtle but important part of the open source ethos.

Here are some final words of support if you run into issues or feel overwhelmed:

C. Overview / Install Script ↑ Table of Contents

Let's start with an overview of the steps we'll take.

First, we'll manually run each of these commands in the terminal:

install.sh

The lines starting with # are comments. The rest are actual commands.2

Second, when the commands finish, we'll confirm that Ruby and Rails successfully installed:

pi@raspberrypi:~ $ ruby -v
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [armv7l-linux-eabihf]

pi@raspberrypi:~ $ rails -v
Rails 6.0.3.4

Note: -v is the flag for version in both Ruby and Rails. Flags are a way to set options and pass in arguments to the commands you run. Commands you run will change their behavior based on what flags are set.

Last, we'll create a new Rails application, navigate to its directory, and start a web server:

pi@raspberrypi:~ $ rails new test_app
[output omitted]

pi@raspberrypi:~ $ cd test_app
pi@raspberrypi:~ $ rails server
[output omitted]

We should be able to open a web browser and see the Rails welcome screen at http://localhost:3000:

Ruby on Rails—Welcome Screen

The rest of this tutorial goes through each step in detail.

D. Update Packages ↑ Table of Contents

Our first task is to update Pi's current software package versions:

pi@raspberrypi:~ $ sudo apt update

We can run this command from any location on the Pi (run pwd to see your current location).

Here's what sudo apt update does:

sudo means "Super User Do." It flips the computer into "superuser" mode for the command. This allows us to work with software packages as a fully-permissioned super user.

apt means "Advanced Packaging Tool." It's software that comes with Linux for installing, removing, and performing other operations on installed software packages.

update fetches lists of relevant software versions to make sure we have information about the newest package versions and their dependencies.

No need to understand all the details right now. All you need to know is that we're grabbing information about the latest software versions. We are not installing anything yet. 3

E. Install Foundational Packages ↑ Table of Contents

The next command is simpler than it looks:

pi@raspberrypi:~ $ sudo apt install -y curl build-essential zlib1g-dev libreadline-dev libssl-dev libxml2-dev nodejs

Here we're using sudo apt install to install several foundational software development packages. The -y flag automatically answers "yes" to any yes/no prompts during the installation process. The remaining commands are the names of packages we're going to install.

The packages are:

curl—command line data transfer tool.

build-essential—libraries and tools for compiling code.

zlib1g-dev—library for compression.

libreadline-dev—library for command line interfaces.

libssl-dev—library for cryptographic protocols.

libxml2-dev—library for designing markup languages.

nodejs—a JavaScript runtime.

No need to understand the details. What you need to know is that when we run the script, apt downloads and installs the packages and their dependencies on your Pi.

F. Install RVM, Ruby, and Rails ↑ Table of Contents

Next we'll install Ruby and Rails with a program called RVM (Ruby Version Manager).4 It takes three commands to get RVM, Ruby, and Rails up and running.

First, we use gpg (GnuPrivacy Guard) to get the cryptographic keys RVM needs:

pi@raspberrypi:~ $ gpg --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

Second, we use curl and bash commands to install RVM:

pi@raspberrypi:~ $ curl -sSL https://get.rvm.io | bash -s stable --rails

The --rails flag tells RVM to install Ruby and Rails. This script will take several minutes to run.

Third and finally, we need to run a script so the system knows how to find Ruby and Rails:

pi@raspberrypi:~ $ echo 'source /home/pi/.rvm/scripts/rvm' >> ~/.bashrc

This particular command does not return any output.

At this point, Ruby and Rails are installed and available. You can double check like this:

pi@raspberrypi:~ $ ruby -v
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [armv7l-linux-eabihf]

pi@raspberrypi:~ $ rails -v
Rails 6.0.3.4

G. Install Yarn ↑ Table of Contents

The final setup step is installing Yarn directly from the official instructions.

First, we grab the key for Yarn:

pi@raspberrypi:~ $ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -

This command will return OK.

Second, we add Yarn to apt:

pi@raspberrypi:~ $ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

This command will not return any response.

Third and finally, we use apt to update and install:

pi@raspberrypi:~ $ sudo apt update && sudo apt install yarn

Note: this command runs two commands in one line. As you get better at Linux, you'll learn many more shortcuts and tricks like this.

H. Create New Rails App ↑ Table of Contents

The final step is to create a new Rails app and make sure everything works.

First, we'll change our working directory to the desktop and create a Rails app called test_app with the rails new command:

pi@raspberrypi:~ $ cd ~/Desktop
pi@raspberrypi:~ $ rails new test_app

This command will take a while to run and will generate a long, scrolling output. This is because Rails automatically installs all of the required dependencies.

Second, we'll move into the new test_app directory and start a Rails' web server with rails server to confirm that the app works:

pi@raspberrypi:~ $ cd test_app
pi@raspberrypi:~ $ rails server

Third and finally, we open a web browser, put localhost:3000 in the address bar, and see the Rails welcome screen:

Ruby on Rails—Welcome Screen

You just installed Rails on Pi! 💥

1 For example, many open source packages introduce substantial complexity to accommodate human and computer languages, hardware limitations, and other accessibility-related barriers. Complexity is also common where a particular software or feature has few users or little value (and is therefore not worth the effort to simplify).
2 Don't worry if you're not familiar with a shell script. We're not actually going to use this particular file. Instead, we're going to copy each command and go through them one-by-one. I put this all in one place so (1) you can come back to it, and (2) so experienced programmers can quickly grab what they need.
3 If you want to read more about apt update here is a good starting point.
4 I recommend against trying to fully understand how RVM works at this point. All you need to understand is that it's common practice for Rails developers to switch between versions of Ruby and Rails. One project might use Rails 5.1 and Ruby 2.2, while another app uses Rails 6.0 and Ruby 2.7. RVM makes switching back and forth easy by managing multiple versions. It Just Works™.
Thanks to Philip Thomas, Jesse Evers, and Compound Writing for reviewing a draft of this post!