This step by step guide gives you a brief introduction to Ruby on Rails and then walks you through the steps to install Ruby on Rails in Linux operating systems. At the end, you will learn how to create a new Rails application and test it via the web browser.
Table of Contents
Introduction to Ruby on Rails
Ruby on Rails, or Rails, is an open source web application development framework written in Ruby programming language. Rails is a server-side web application framework that facilitates the use of web standards such as JSON
or XML
for data transfer and HTML
, CSS
and JavaScript
for user interfacing.
Using Rails, we can build modern web applications with less code while accomplishing more than other languages and frameworks. Hundreds of thousands of applications are built using Rails. Some of the popular applications are Basecamp, GitHub, HEY, Shopify, Airbnb, Twitch, SoundCloud, Hulu, Zendesk and many.
Ruby on Rails is created by David Heinemeier Hansson, a Danish programmer, racing driver and a best-selling author. Hansson first released Rails as open source in July 2004. Rails became popular when Apple announced that it would ship Ruby on Rails with Mac OS X v10.5.
1. Install Ruby on Rails in Linux
First of all, update your Linux system.
If you're on Debian and its derivatives like Ubuntu, you can update your system using apt
like below:
$ sudo apt update
$ sudo apt upgrade
On Fedora, RHEL and its clones like CentOS, AlmaLinux:
$ sudo dnf --refresh upgrade
To update openSUSE, run:
$ sudo zypper update
$ sudo zypper upgrade
Reboot your system to apply and complete the updates:
$ sudo reboot
Before installing Rails, make sure you have installed the following prerequisites.
- Ruby - A dynamic, open source programming language with a focus on simplicity and productivity.
- SQLite3 - It is the default database backend for Rails. It will be installed as a requirement when we install Ruby.
- Node.js - An open source, cross-platform, JavaScript run-time environment to build scalable network applications.
- Yarn - A modern package manager to install and manage JavaScript programs.
1.1. Install Ruby in Linux
Ruby is packaged for many Linux distributions and available in their default repositories. So you can install using your distribution's default package manager. However, the available Ruby version in the repositories might be bit old. So you can use any third-party installers and Ruby managers.
Many Ruby users use Ruby managers to manage multiple Rubies. They allow easy or even automatic switching between Ruby versions. For the purpose of this guide, we will be using RVM.
RVM, stands for Ruby Version Manager, allows you to install and manage multiple installations of Ruby on your Linux system. It can also manage different gemsets. It is available for macOS, Linux, or other UNIX-like operating systems.
Install GPG2 package which is needed to add RVM GPG keys:
$ sudo apt install gnupg2
And then import RVM GPG keys by entering the following command in your Terminal:
$ gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
After adding the key, run the following command to install RVM in Linux:
$ curl -sSL https://get.rvm.io | bash -s stable
Sample output:
Downloading https://github.com/rvm/rvm/archive/1.29.12.tar.gz Downloading https://github.com/rvm/rvm/releases/download/1.29.12/1.29.12.tar.gz.asc gpg: Signature made Fri Jan 15 18:46:22 2021 UTC gpg: using RSA key 7D2BAF1CF37B13E2069D6956105BD0E739499BDB gpg: Good signature from "Piotr Kuczynski <piotr.kuczynski@gmail.com>" [unknown] gpg: WARNING: This key is not certified with a trusted signature! gpg: There is no indication that the signature belongs to the owner. Primary key fingerprint: 7D2B AF1C F37B 13E2 069D 6956 105B D0E7 3949 9BDB GPG verified '/home/ostechnix/.rvm/archives/rvm-1.29.12.tgz' Installing RVM to /home/ostechnix/.rvm/ Adding rvm PATH line to /home/ostechnix/.profile /home/ostechnix/.mkshrc /home/ostechnix/.bashrc /home/ostechnix/.zshrc. Adding rvm loading line to /home/ostechnix/.profile /home/ostechnix/.bash_profile /home/ostechnix/.zlogin. Installation of RVM in /home/ostechnix/.rvm/ is almost complete: * To start using RVM you need to run `source /home/ostechnix/.rvm/scripts/rvm` in all your open shell windows, in rare cases you need to reopen all shell windows. Thanks for installing RVM ? Please consider donating to our open collective to help us maintain RVM. ? Donate: https://opencollective.com/rvm/donate
RVM installation is almost complete. You need to run one last step before installing Ruby!
As you might have noticed in the above output, there is a line that says - To start using RVM you need to run `source /home/ostechnix/.rvm/scripts/rvm`
.
So let me run the following command to start RVM:
$ source /home/ostechnix/.rvm/scripts/rvm
Replace /home/ostechnix
with your own username. The above command will set the RVM environment path.
If you're on RHEL-based systems (Eg. CentOS), you may need to run the following command to add RVM to the $PATH
.
# source /etc/profile.d/rvm.sh
We have just installed RVM and add it to the $PATH
. Now let us install necessary requirements for RVM.
To install RVM requirements, simply run:
$ rvm requirements
Sample output:
Checking requirements for ubuntu. Installing requirements for ubuntu. Updating system… Installing required packages: g++, gcc, autoconf, automake, bison, libc6-dev, libffi-dev, libgdbm-dev, libncurses5-dev, libsqlite3-dev, libtool, libyaml-dev, make, pkg-config, sqlite3, zlib1g-dev, libgmp-dev, libreadline-dev, libssl-dev………………………….. Requirements installation successful.
The above command installs all required packages including SQLite. It is time to install Ruby using RVM.
First, let us check which version of Ruby is available using the following command:
$ rvm list known
Sample output:
MRI Rubies [ruby-]1.8.6[-p420] [ruby-]1.8.7[-head] # security released on head [ruby-]1.9.1[-p431] [ruby-]1.9.2[-p330] [ruby-]1.9.3[-p551] [ruby-]2.0.0[-p648] [ruby-]2.1[.10] [ruby-]2.2[.10] [ruby-]2.3[.8] [ruby-]2.4[.10] [ruby-]2.5[.8] [ruby-]2.6[.6] [ruby-]2.7[.2] [ruby-]3[.0.0] ruby-head [...]
As of writing this guide, the latest available Ruby version was 3.0.0.
To install the latest Ruby using RVM on Linux, run:
$ rvm install ruby
Sample output:
Searching for binary rubies, this might take some time. Found remote file https://rvm_io.global.ssl.fastly.net/binaries/ubuntu/20.04/x86_64/ruby-3.0.0.tar.bz2 Checking requirements for ubuntu. Requirements installation successful. ruby-3.0.0 - #configure ruby-3.0.0 - #download % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 26.7M 100 26.7M 0 0 1186k 0 0:00:23 0:00:23 --:--:-- 1307k ruby-3.0.0 - #validate archive ruby-3.0.0 - #extract ruby-3.0.0 - #validate binary ruby-3.0.0 - #setup ruby-3.0.0 - #gemset created /home/ostechnix/.rvm/gems/ruby-3.0.0@global ruby-3.0.0 - #importing gemset /home/ostechnix/.rvm/gemsets/global.gems……………………………. ruby-3.0.0 - #generating global wrappers…….. ruby-3.0.0 - #gemset created /home/ostechnix/.rvm/gems/ruby-3.0.0 ruby-3.0.0 - #importing gemsetfile /home/ostechnix/.rvm/gemsets/default.gems evaluated to empty gem list ruby-3.0.0 - #generating default wrappers……..
You can also install a specific version of Ruby by explicitly mentioning its version. For example, the following command will install Ruby 2.7.2 version.
$ rvm install ruby-2.7.2
Check the installed Ruby version:
$ ruby --version ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]
List all installed Ruby versions:
$ rvm list
Sample output:
=* ruby-3.0.0 [ x86_64 ] => - current =* - current && default * - default
If you have installed multiple Ruby versions, you can then set default Ruby version like below:
$ rvm use 3.0.0 --default
1.2. Install SQLite
SQLite is a relational database management system contained in a C library. SQLite is the most widely deployed database engine. It is used almost everywhere including web browsers, operating systems and embedded systems. Rails uses SQLite3 as the default database.
SQLite is automatically installed when we install RVM requirements in the previous step. So let us not bother with SQLite installation.
1.3. Install Nodejs
A few Rails features, such as the Asset Pipeline, depend on a JavaScript Runtime. Since Nodejs provides this functionality, install it as described in the following link.
1.4. Install Yarn
Yarn is a JavaScript package manager, and a faster alternative to NPM. The advantage of using Yarn with Rails is that you facilitate the management of CSS and JavaScript libraries in your project. Its behavior is similar to Ruby gems, but in a front-end universe. Rails supports Yarn out of the box.
If you haven't installed Yarn yet, refer the following guide.
1.5. Install Rails
After installing all aforementioned prerequisites, run the following command to install Ruby on Rails in your Linux system:
$ gem install rails
Check the installed Rails version using command:
$ rails --version Rails 6.1.3.2
If you want to install a specific Rails version, for example 5.2.0, the command would be:
$ gem install rails -v 5.2.0
You can list all available Rails versions by searching their versions using the following command:
$ gem search '^rails$' --all
As of writing this guide, these are the available remote gems:
*** REMOTE GEMS ***
rails (6.1.3.2, 6.1.3.1, 6.1.3, 6.1.2.1, 6.1.2, 6.1.1, 6.1.0, 6.0.3.7, 6.0.3.6, 6.0.3.5, 6.0.3.4, 6.0.3.3, 6.0.3.2, 6.0.3.1, 6.0.3, 6.0.2.2, 6.0.2.1, 6.0.2, 6.0.1, 6.0.0, 5.2.6, 5.2.5, 5.2.4.6, 5.2.4.5, 5.2.4.4, 5.2.4.3, 5.2.4.2, 5.2.4.1, 5.2.4, 5.2.3, 5.2.2.1, 5.2.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 2.3.17, 2.3.16, 2.3.15, 2.3.14, 2.3.12, 2.3.11, 2.3.10, 2.3.9, 2.3.8, 2.3.7, 2.3.6, 2.3.5, 2.3.4, 2.3.3, 2.3.2, 2.2.3, 2.2.2, 2.1.2, 2.1.1, 2.1.0, 2.0.5, 2.0.4, 2.0.2, 2.0.1, 2.0.0, 1.2.6, 1.2.5, 1.2.4, 1.2.3, 1.2.2, 1.2.1, 1.2.0, 1.1.6, 1.1.5, 1.1.4, 1.1.3, 1.1.2, 1.1.1, 1.1.0, 1.0.0, 0.14.4, 0.14.3, 0.14.2, 0.14.1, 0.13.1, 0.13.0, 0.12.1, 0.12.0, 0.11.1, 0.11.0, 0.10.1, 0.10.0, 0.9.5, 0.9.4.1, 0.9.4, 0.9.3, 0.9.2, 0.9.1, 0.9.0, 0.8.5, 0.8.0)
The most recent version will be listed first.
2. Create a new Rails application
Rails consists of many scripts named generators which makes your application development easier! One of the generator is the new application generator that is used to create a fresh Rails application.
To create a new Rails application named ostechnixapp
, run:
$ rails new ostechnixapp
This will create a Rails application called ostechnixapp in a directory named ostechnixapp
and install the gem dependencies that are already mentioned in Gemfile
using bundle install
.
After creating the ostechnixapp
application, switch to its directory:
$ cd ostechnixapp/
This directory contains several files and sub-directories. The main sub-directory is app
, which contains the controllers, models, views, helpers, mailers, channels, jobs, and assets for your application.
To see our newly created application, let us start a web server using command:
$ bin/rails server
Or,
$ rails server
This will start up Puma, a web server distributed with Rails by default.
To see your application in action, open your web browser and navigate to http://localhost:3000. You should see the Rails default information page:
If you see "Yay! You're on Rails!" page, congratulation! You're all set!! Ruby on Rails is correctly installed and configured on your Linux system. To exit the web server, simply press CTRL+C
.
By default, you can access the Rails test page from your local machine only. If you want to access the Rails default test page from other machine on the network, run:
$ rails server -b 0.0.0.0
Now you can access the Rails test page from any system on the network by navigating to http://ip-address:3000
from the browser window.
You may also need to allow the port 3000
in your firewall/router.
You can also use different port by pass the -p flag along with the port number like below:
$ rails server -b 0.0.0.0 -p 8080
Now, you can access your Rails application using this URL: http://ip-address:8080
3. Use MySQL With Rails Application
While Sqlite works great with Rails, some times it may not be sufficient for your Rails application. If you want scalability, concurrency, centralization, and control, you may want to try more robust databases like MySQL or PostgreSQL. The following guide explains how to use MySQL with Ruby on Rails application.
4. Use PostgreSQL with Rails Application
PostgreSQL is yet another recommended database to use with Rails applications. If you want to use PostgreSQL instead of MySQL for your Rail projects, the following guide will help.
5. Remove RVM
After installing Ruby, you probably not going to need RVM. So you can uninstall RVM if you no longer need it.
To do so, edit your ~/.bashrc
file:
$ nano ~/.bashrc
Scroll down to the end and remove the following lines:
[...] Add RVM to PATH for scripting. Make sure this is the last PATH variable chang> export PATH="$PATH:$HOME/.rvm/bin"
Press CTRL+W
followed by CTRL+X
to save and close the file.
Source the ~/.bashrc
file to take effect the changes immediately.
$ source ~/.bashrc
Finally, remove the RVM directory:
$ rm -r ~/.rvm/
Conclusion
At this stage, you should learned what is Ruby on Rails, how to install Ruby on Rails in Linux using RVM and finally how to create a sample Rails application and test it via web browser. Start developing your Rails applications.
Resources: