How To Install Ruby on Rails on Debian 10 / Debian 9 |

0

Ruby on Rails is a free server-side web application framework written in Ruby. It is mainly used for creating highly powerful websites and applications.

In this post, we will see how to install
Ruby on Rails on
Debian 10 /
Debian 9 using:

  1. rbenv (Recommended)
  2. RVM

Prerequisites

Install the curl and other required packages.

sudo apt update

sudo apt install -y curl gnupg2 dirmngr git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev

Install Node.js

Some functionalities of Rails like CoffeeScript and Asset Pipeline require a Javascript runtime, and for that, we will install Node.js.

Here, we will use the LTS version of Node.js for our Ruby on Rails installation.

If you want to get Node.js’s latest feature,
install Node.js v13.x.

curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -

sudo apt install -y nodejs

Install Yarn

Add the Yarn repository to install the Yarn package manager.

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -

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

Install Yarn with the below command.

sudo apt update

sudo apt install -y  yarn

Install Ruby

The rbenv lets you install and manage the versions of Ruby easily, and it is simpler than RVM.

To install rbenv on your system, run the below commands.

cd
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

The above commands will install rbenv into your home directory and set appropriate environment variables.

For this post, we will install the latest version of Ruby (v2.7.0). The installation process may take up to 20 minutes to complete, so please be patient.

rbenv install 2.7.0

You can also install or use the different versions of Ruby, run the rbenv install [version] command with Ruby version rbenv install -l.

Set Ruby v2.7.0 as the default version for all login shells.

rbenv global 2.7.0

Check the Ruby version.

ruby -v
Output:

ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux]

Install the bundler.

gem install bundler

Using RVM

RVM stands for Ruby Version Manager. It provides an easy way to install and manage ruby versions independently by automatically downloading its dependencies.

Import public key in your system.

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

Use the curl command to install RVM in your system.

curl -sSL https://get.rvm.io | bash -s stable

Load RVM environment variables using the below command.

source ~/.rvm/scripts/rvm

With RVM, you can install and manage multiple Ruby versions on the single system.

Use the below command to install Ruby 2.7.0.

rvm install 2.7.0

You can also install a different version of Ruby using the rvm install [version] command.

Set Ruby v2.7.0 as default in case your system has multiple versions of Ruby.

rvm use 2.7.0 --default
Output:

Using /home/holhol24/.rvm/gems/ruby-2.7.0

Check the Ruby version.

ruby -v
Output:

ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux]

Install the bundler.

gem install bundler

Install Rails

Use gem install rails command to install the latest release of Rails. This installation process may take a while, be patient with it.

gem install rails

You can also use gem install rails –v [version] to install a specific version of rails.

Check the Rails version.

rails -v
Output:

Rails 6.0.2.2

Create Test Application

We will now create a test application with MariaDB as a database to test it out.

Install Database

Rails uses sqlite3 as the default database for the application that requires a database, and it is not recommended to use SQLite in a production environment as SQLite can not handle high traffic. You may need to go with
MariaDB or
PostgreSQL.

For this demo, we will use MariaDB as a database server.

Here, we will install the MariaDB server (v10.3 – Debian 10, V10.2 – Debian 9) from the Debian base repository.

In case you chose to install MariaDB v10.4 from the MariaDB community, you must install libmariadb-dev package for Rails.

READ:
Install MariaDB v10.4 On Debian 10

READ:
Install MariaDB v10.4 On Debian 9

sudo apt install -y mariadb-server mariadb-client default-libmysqlclient-dev

Execute the mysql_secure_installation command to secure your MariaDB installation.

Install the MySQL2 gem extension.

gem install mysql2

Create Database User

sudo mysql -u root -p

Create a database user for your application.

CREATE USER 'holhol24'@'localhost' IDENTIFIED BY 'password';

GRANT ALL PRIVILEGES ON *.* TO 'holhol24'@'localhost';

exit

Create Rails Application

Create a Rails application with database support using the below commands.

cd ~

rails new myAapp -d mysql

cd myAapp

Create a test application as a standard user as running the Rails server as the root user is not recommended.

Update the database information.

nano config/database.yml

Enter the DB user and password details, as shown like below.

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: 
  username: holhol24 
  password: password 
  socket: /var/run/mysqld/mysqld.sock

Create the database.

rake db:create
Output:

Created database 'myAapp_development'
Created database 'myAapp_test'

Validate Test Application

Start your rails application.

rails server -b 0.0.0.0
Output:

=> Booting Puma
=> Rails 6.0.2.2 application starting in development
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 4.3.3 (ruby 2.7.0-p0), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

If everything is working correctly, your Rails application should now be running on port 3000.

Visit your Rails application by going to the below URL in a
web browser.

http://localhost:3000

OR

http://your.ip.add.ress:3000

You should get the following page.

Rails Application Running on Debian

Conclusion

That’s All. You have successfully installed install
Ruby on Rails on
Debian 10 /
Debian 9.