Before we start building our Rails application, we need to set up the development environment. This post covers everything you need to know to get started with Ruby on Rails.
To learn why you should build for the web and use Rails, check out the first article in the series.
I use a MacBook Pro, so these are Mac-specific instructions. It's not that difficult to set up Rails on Windows, but I recommend using a MacBook (Air or Pro, it doesn't matter) for Rails development. You will be more productive and enjoy programming more than before. Trust me; I've been programming .NET on Windows for over five years before switching to Rails.
Let's get started.
Before You Install Rails ...
Here are some of my favorite tools to increase your productivity and make the development experience more pleasant.
They are not a requirement for Rails, but try them anyway.
- iTerm: a replacement for the native Terminal app on OSX.
- zsh: z-shell is a powerful alternative to the default bourne-again shell (bash)
- Oh-my-zsh: a delightful, open-source, community-driven framework for managing your Zsh configuration.
- Git: I don't have to explain this, do I? You need version control.
- Bat: It's an alternative to the
catcommand and supports syntax highlighting for a large number of programming and markup languages.
- VS Code: a lightweight editor + powerful IDE for everyday programming.
If you have other favorite tools, let me know in the comments.
With the nice-to-haves out of the way, here's the essential software you will need for building awesome Rails applications.
If you are developing on a Mac, you will need Homebrew.
Though it's not a Rails-specific tool, it lets you install most of the tools and libraries you'll need when programming with Rails, such as Redis or Sqlite. So this is the first tool we will install.
Get the most up-to-date installation instructions on its website.
Ruby is a beautiful programming language that combines the best of both functional and object-oriented programming (don't worry if you don't know what these terms mean).
Ruby will make you more productive when programming, and you will be spewing beautiful code that's aesthetically pleasing in no time.
Install Ruby using `rbenv`, which allows you to easily install and manage multiple versions of Ruby. Install it using Homebrew.
brew install rbenv ruby-build
ruby-buildallows you to install any Ruby version,
rbenvmakes it easy to switch between them on a per-project basis.
Once it finishes, run the following command:
It will give you some instructions depending on the system. Follow them.
Read the rbenv documentation to learn how to install a specific Ruby version. To ensure you have Ruby installed on your machine, run the following command in the terminal.
➜ ruby --version ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-darwin21]
It should print the Ruby version you installed.
We will mainly use SQLite because it's very easy to use. It's a simple file. That makes backups and data transfer really easy (just copy+paste). But don't let that simplicity fool you. It's a powerful tool that deserves our respect. It works fine as the database engine for most low to medium-traffic websites (that is, most websites).
We are going to use SQLite for our application. Most likely, you don't need to do anything to install SQLite. It's preinstalled in all modern versions of macOS.
Ensure that you have it by running the following command:
➜ sqlite3 --version 3.37.0 2021-12-09 01:34:53 9ff244..
If you don't have it installed, then use Homebrew to install it.
brew install sqlite
Although you can query the data in the terminal, I highly recommend using TablePlus. It's a fantastic database browser with a polished UI that will make you more productive. I've been using the paid version for a few months now, and I don't want to go back. Don't worry; it also has a free version that you can use.
Ruby on Rails
Finally, we are ready to install Ruby on Rails, which is a gem, and we will install it using RubyGems.
What's a gem?
A gem is a software package containing a ruby application or library. Gems give you a structured and reliable way to install, use, package, and distribute ruby code.
You can use gems written by other ruby programmers in your applications and write and publish your gems that others can use.
What is RubyGems?
RubyGems is a package manager for ruby, providing a standard format for distributing ruby gems. It makes it easy to locate, install, upgrade, and uninstall gems. You can use the
gem command that ships with ruby to use RubyGems.
Before RubyGems, if you wanted to install a new library, you had to search the Web, download a package, and attempt to install it—only to find that you had to install its dependencies, too.
Using RubyGems, you can bundle your code into single files called gems. These files conform to a standardized format and typically are stored in repositories on the Internet (but you can also create your own repositories if you want). RubyGems can also install utility programs that you can invoke from the command line.
gem, a command-line tool for working with Ruby gems. It also includes integration into Ruby so that your programs can access gems as libraries.
To install Ruby on Rails, use the
gem install command provided by RubyGems:
gem install rails
Once it finishes, ensure that it was installed successfully by running the following command:
➜ rails version Rails 188.8.131.52
If you see a version greater than 7, Rails was installed successfully.
Congratulations, you have successfully set up your laptop for Rails development!!
Pat yourself on the back and relax. In the following article in this series, we will try something totally different.
We will be building a web application without Rails.