Here I have explained few steps to setup the rails on Ubuntu.
As you know RoR can be installed on windows,linux and mac. But personally I prefer to work on linux. Here I am mentioning steps for installing rails on linux (Ubuntu 10.04 box).
We are going to use 'aptitude' command to install the packages. 'aptitude' is a known command in linux to install packages. If you don't have 'aptitude' installed on your system, you can install it using the following line.
$ sudo apt-get install aptitude
Now 'aptitude' is installed and we need to make sure that we have the latest sources on our box and we don’t run into any package-related issues, such as not being able to install some packages. So now we run the below command.
$ sudo aptitude update
Next, we are going to install git , curl and build-essential which are required to install and use RVM, and compile Ruby versions. For your information git is a version control system and both git and curl is required to install RVM .To install these three packages we'll use the following command.
$ sudo apt-get install build-essential git-core curl
At this point you must be having question regarding RVM. What is RVM and why we use this ?
RVM (Ruby Version Manager ) is a command-line tool which allows you to easily install, manage and work with multiple ruby environments . Lets take one example, sometime you required to work on different projects on different ruby environment such as one on Ruby 1.8.2 and another one is on Ruby 1.9.2 . So in this case it is very important to manage the different version on the single system. This situation can be handled by RVM.
Now let's install RVM on our system using the following command.
$ bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
Once the command is executed, we need to add a line to ~/.bashrc file which will load RVM .The above file responsible for setting up our bash session.
$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"' >> ~/.bashrc
Now we are set with RVM. Next we need to know what other packages we need to install for Ruby. Now RVM will tell us, what are the required packages. Use the following command.
$ rvm notes
When we run this command it'll output a set of notes on the terminal window. Find the below note among the set of notes.
# For Ruby (MRI & ree) you should install the following OS dependencies:
ruby: aptitude install build-essential bison openssl libreadline6 libreadline6-devcurl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf
Now copy the command (which is mentioned after 'ruby:' )and execute it to install other packages.
$ sudo aptitude install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf
Please note that the above command should be in online. Otherwise it may miss some package installation.Now we are ready to install any version of ruby and set default environment.
Execute the below command to install a particular version of ruby.
$ rvm install 1.9.2
This will install the ruby1.9.2
To use the same we can use the command
$ rvm use 1.9.2
But for setting the default environment, we can use the following command.
$ rvm -–default use 1.9.2
Now you can check what is the ruby version you are using for application using the below command
$ ruby -v
At this point we are set with RVM and ruby. Now we need to install rails and go for our first application.
$ gem install rails
This will install rails and its dependencies including bundler .
We are all set with our environment. Let's move towards our first application.
Here I am creating a directory to keep all my rails applications. And then creating my first application within it.
$ mkdir rails_apps
$ cd rails_apps
$ rails new my_first_app
While execute the last command “rails new my_first_app”, you can see it'll create some files and folders. These are the standard files and directories of the application.
Now if you open the “Gemfile“ which resides inside “my_first_app” directory, you can see couple of lines (some of them are commented). Here we can include our required gems for our application. To include any gem we need to write a simple statement.
gem '
After including the required gems we can run the bundle install to install all gems for the application. In our case we don't need any additional gems as it is our first application.
Now run the below command.
$ cd my_first_app
$ bundle install
This will take few time to install all the default gems and additional gems for the application. After completion of above command execute the below code to start the server.
$ rails server
This will start the local web-server. In our case WEBRick is the default web-server which comes along with rails package.
Now we are ready to open our first application on the browser. Just type http://localhost:3000 on your browser and you can see the default rails page. 3000 is the default port used by the web-server. Remember it is one default rails page , we can add controllers,models and views to our first application to create our own application. Here I am not discussing the MVC (Model,View and Controller). Once we get the default rails page , we are assured that rails and application setup is OK and we can go ahead for more coding.
I hope the above information will help you to set-up your first application in RoR. For further assistance feel free to send your query to me (asisht@mindfiresolutions.com) or post your query in the comment section, I'll be happy to assist you solving your problem.
No comments:
Post a Comment