Linux, Windows, Virtualization, OpenSource & Blogging

Install Ruby On Rails On Debian 9 / Ubuntu 16.04

0

Ruby is an open-source programming language and more popular these days. Ruby language was designed and developed by Yukihiro “Matz” Matsumoto in mid-1995.

This post will help you to install Ruby on Rails on Debian 9 / Ubuntu 16.04 using Ruby Version Manager. RVM offers an easy set of commands to install multiple versions of Ruby on a single server.

Prerequisites

Switch to the root user.

su -

OR

sudo su -

Install the curl and other required packages.

apt-get install -y curl gnupg2 dirmngr

Install RVM

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

Import public key in your system.

### Debian ###

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

### Ubuntu ###

gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

Use curl command to install RVM in your system.

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

Output:

Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc
gpg: Signature made Sun 10 Sep 2017 03:59:21 PM CDT
gpg:                using RSA key E206C29FBF04FF17
gpg: Good signature from "Michal Papis (RVM signing) <mpapis@gmail.com>" [unknown]
gpg:                 aka "Michal Papis <michal.papis@toptal.com>" [unknown]
gpg:                 aka "[jpeg image of size 5015]" [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: 409B 6B17 96C2 7546 2A17  0311 3804 BB82 D39D C0E3
     Subkey fingerprint: 62C9 E5F4 DA30 0D94 AC36  166B E206 C29F BF04 FF17
GPG verified '/usr/local/rvm/archives/rvm-1.29.3.tgz'
Creating group 'rvm'

Installing RVM to /usr/local/rvm/
Installation of RVM in /usr/local/rvm/ is almost complete:

  * First you need to add all users that will be using rvm to 'rvm' group,
    and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.

  * To start using RVM you need to run `source /etc/profile.d/rvm.sh`
    in all your open shell windows, in rare cases you need to reopen all shell windows.

Load RVM environment variables using below command.

source /etc/profile.d/rvm.sh

Install Ruby

Use the below command to install dependencies for ruby.

rvm requirements

Run the following command to get a list of ruby versions available for your system.

rvm list known

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[.7]
[ruby-]2.3[.4]
[ruby-]2.4[.1]
ruby-head

# for forks use: rvm install ruby-head- --url https://github.com/github/ruby.git --branch 2.2

# JRuby
jruby-1.6[.8]
.   .   .

With RVM, you can manage multiple ruby versions on the single system. Use the following command to install Ruby 2.4.1.

rvm install 2.4.1
Searching for binary rubies, this might take some time.
No binary rubies available for: debian/9/x86_64/ruby-2.4.1.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for debian.
Requirements installation successful.
Installing Ruby from source to: /usr/local/rvm/rubies/ruby-2.4.1, this may take a while depending on your cpu(s)...
ruby-2.4.1 - #downloading ruby-2.4.1, this may take a while depending on your connection...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 11.9M  100 11.9M    0     0  2697k      0  0:00:04  0:00:04 --:--:-- 2705k
ruby-2.4.1 - #extracting ruby-2.4.1 to /usr/local/rvm/src/ruby-2.4.1....
ruby-2.4.1 - #applying patch /usr/local/rvm/patches/ruby/2.4.1/random_c_using_NR_prefix.patch.
ruby-2.4.1 - #configuring..................................................................
ruby-2.4.1 - #post-configuration..
ruby-2.4.1 - #compiling................................................................................
ruby-2.4.1 - #installing...........
ruby-2.4.1 - #making binaries executable..
ruby-2.4.1 - #downloading rubygems-2.6.13
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  750k  100  750k    0     0  1584k      0 --:--:-- --:--:-- --:--:-- 1587k
ruby-2.4.1 - #extracting rubygems-2.6.13....
ruby-2.4.1 - #removing old rubygems.........
ruby-2.4.1 - #installing rubygems-2.6.13..........................
ruby-2.4.1 - #gemset created /usr/local/rvm/gems/ruby-2.4.1@global
ruby-2.4.1 - #importing gemset /usr/local/rvm/gemsets/global.gems...............................................
ruby-2.4.1 - #generating global wrappers........
ruby-2.4.1 - #gemset created /usr/local/rvm/gems/ruby-2.4.1
ruby-2.4.1 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.4.1 - #generating default wrappers........
ruby-2.4.1 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-2.4.1 - #complete
Ruby was built without documentation, to build it run: rvm docs generate-ri

You can also install other versions of ruby using the rvm install <version> command.

Set default ruby version to 2.4.1 in case your system has multiple versions of ruby.

rvm use 2.4.1 --default

Output:

Using /usr/local/rvm/gems/ruby-2.4.1

Check the Ruby version.

ruby -v

Output:

ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]

Install Rails

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

gem install rails

Check rails version.

rails -v

Output:

Rails 5.1.4

You can also use gem install rails –version=<version> to install a specific version of rails.

Install Node.js

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

apt-get -y install nodejs

Create a Test Application

We will now create a test application your home directory and test it out.

Install MariaDB Database

Rails uses sqlite3 as the default database, and it is not recommended to use in a production environment where you get high traffic. You may probably want to go with MySQL or PostgreSQL.

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

### Debian 9 ###

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

### Ubuntu 16.04 ###

apt-get install -y mariadb-server mariadb-client libmysqlclient-dev

# Secure MySQL installation - Optional

mysql_secure_installation

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

Add your user to rvm group to resolve permission issues while installing gems.

usermod -aG rvm <user>

# switch to normal user

su - <user>

Create a Rails application with database support using below commands.

cd ~

rails new itzgeekapp -d mysql

cd itzgeekapp

Update the database information.

nano  config/database.yml

Enter the DB user details shown lie below.

default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: pass ## replace pass with MySQL root password.
socket: /var/run/mysqld/mysqld.sock

Create the database.

rake db:create

Output:

Created database 'itzgeekapp_development'
Created database 'itzgeekapp_test'

Start your rails application.

rails server

OR

# Set a binding IP address

rails server --binding=IP

Output:

=> Booting Puma
=> Rails 5.1.4 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.10.0 (ruby 2.4.1-p111), codename: Russell's Teapot
* 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 be running on port 3000 of the IP address of your server.

Visit your Rails application by going to 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 9:

Install Ruby On Rails On Debian 9 - Rails application Running on Debinan 9
Install Ruby On Rails On Debian 9 – Rails application Running on Debian 9

Rails application running on Ubuntu 16.04:

Install Ruby On Rails On Debian 9 - Rails Application running on Ubuntu 16.04
Install Ruby On Rails On Debian 9 – Rails Application running on Ubuntu 16.04

That’s All.

You might also like

Install Ruby On Rails On Debian 9 / Ubuntu 16.04

0