Installation and configuration of Ruby and Ruby On Rails in Ubuntu (16.04) - Run With Code

Latest

Learn Ruby On Rails , Linux and IoT(Internet Of Things).

Amazon

Saturday 17 February 2018

Installation and configuration of Ruby and Ruby On Rails in Ubuntu (16.04)

What we are going to configure?

1) Ruby (rbenv).
2) Ruby On Rails.



Ruby on Rails is one of the most popular and fast application stacks for developers looking to create sites and web apps. The Ruby programming language, combined with the Rails development framework, makes app development simple.

Managing the multiple projects which are developed in different ruby versions,  is a difficult task for a developer.

To overcome the difficulty we use rbenv, which is a command-line tool.

rbenv will provide you with a solid environment for developing your Ruby on Rails applications as it will let you easily switch Ruby versions.

You can find it on Github.  https://github.com/rbenv/rbenv

Let’s install the dependencies

*First of all, you have to update your ubuntu :

$sudo apt-get update 




* Install the dependencies required for rbenv and Ruby for ubuntu :

      $sudo apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev


*Once you have done the above steps. Means you are ready to install rbenv.

* Let’s install rbenv :

  Now you are ready to install rbenv .

  Let's clone the rbenv repository from git. By using 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 

    Now you are ready to install ruby.

* Let’s Install ruby :

Now you are able to install ruby. But before that, you have to know, which version you have to install and how many version are available. So don’t worry, use the below command.
       $rbenv install -l 

    The output of above command is a long list of versions. Which is helps you to find, what you need?
    
   Once you select your version use below command.

    $rbenv install 2.3.1

    $rbenv global 2.3.1

For example, i am using ruby 2.3.1 that’s why I am using 2.3.1 If you need other versions like  2.3.0 you can use $rbenv install 2.3.0  etc.


*You have done your installation.

*check you did correctly or not.

 Verify that Ruby was properly installed or not, check the current ruby version.
$ruby -v 

Output : current version of ruby .



If the output version is same as you select and use above command. Means you did great:).


Okay, we complete one step Let’s move to next and install Rails.


*Let’s install and create a Rails environment.  

You can install the most recent version of Rails with the gem install command.

$gem install rails   

OR

if you would like to install the different version. You can specify the version.

$gem install rails  -v 5.1.5


After the installation checks the version.
  
$rails -v


Now we are ready to create a Rails application.

* Create a Rails Application :

Move to your working directory and then use the below command. 

  $rails new <application name >

Example : $rails new blogging 




This command generates a new rails application with default sqlite3 database 
if you would like to use other databases you use below command for generating rails application.

$rails new <application name > -d  postgresql 
OR
$rails new <application name > -d  mysql

I am using sqlite3 for this application.   



After using above command it creates a list of directories and files.



Move to your application directory 

$cd <name of your application >

Example : $cd blogging

Now you're in your application directory.

You have to install bundle first.

$gem install bundle 



Then 


$bundle install 



once the bundle installation is finished.

Let’s test the application is working or not use below command to run the server.

$rails s 




Open your Browser and type the below URL.




Let’s Create a new model controller and view for blogging.

$rails g scaffold Blog title:string body:text 

 This command generates a list of directories and files. 



Use this command for migrate database 

        $rake db:migrate 




After that open the routes file.

<project locatio> $ vi config/routes.rb

You can open it in any text editor like sublime,gedit etc.

And add this line before resources :blogs

root 'blogs#index'



Then start your application using 
      $rails s 




 Refresh your web page  or open this URL http://localhost:3000



 Yeeeeeeeeeee it’s working .. :) .

2 comments:

Please do not enter any spam link in the comment box.