Tuesday 24 April 2012

Ruby on Rails environment setup on linux

I have been looking around for a quite long time to find all the Ruby on Rails environment setup instructions for linux on a single page, so finally i decided to write a blog where all the things from Netbeans to RVM gemsets are available under the same hood.

In this tutorial, we are going to setup:

  1. Appache2 server
  2. MySql
  3. Phpmyadmin
  4. RVM setup
  5. Installing Ruby
  6. Creating Gemset
  7. Installing Rails and some other gems
  8. Netbeens Installations


I have been using ubuntu 10.04, but will equally valid for higher versions up till 11.10.

Lets Start

Open the terminal, you can find it in, Applications -> Accessories -> Terminal
Copy each and every command from the rest of the page and paste it in the terminal, sequentially. In most of the commands, i have been using the word "sudo" which is require to install the desired packages with admin user rights.

Installing Appache2 server


:~$ sudo apt-get install apache2
:~$ sudo /etc/init.d/apache2 restart

The first command will install the Appache2 server and second command will restart the server again to   work it normally. This command is not essential but we should do it being on safer side.

Installing MySql


:~$ sudo apt-get install mysql-server
:~$ sudo /etc/init.d/mysql restart
:~$ sudo apt-get install libmysqlclient-dev

Now, the things are going to be self explanatory. The first command will install the server and the following one will restart it.

To check the status of the MySql, issue the following command

:~$ sudo netstat -tap | grep mysql

Installing Phpmyadmin


:~$ sudo apt-get install phpmyadmin

Now open the following file in any of your favorite text editor and put the following line. I will use vim.
Include /etc/phpmyadmin/apache.conf

:~$ vim /etc/apache2/apache2.conf

Now, open you favorite browser and access the local host by hitting the url "localhost/phpmyadmin" If you get some error then try installing the phpmyadmin by using the following command and during installations, select the "apache2" from the option list and continue installations.

:~$ sudo dpkg-reconfigure -plow phpmyadmin

Now, hit again the localhost, this time it will be working hopefully.

RVM setup

Rvm is Ruby Version Management tool which widely used to setup different RoR environments on the same machine and allowing you to switch between different environments, depending upon projects, very easily. To read more about RVM, you can visit the this site where you can find much more stuff related to rvm. 

RVM require has some dependencies issues, so you need to first install some of the supporting libraries to install rvm and work it properly.

:~$ sudo apt-get install build-essential
:~$ sudo apt-get install zlib1g-dev libreadline-dev libssl-dev libxml2-dev

There are some prerequisites which you can easily install by issuing the following commands.

:~$ sudo apt-get install curl git-core

Now install rvm:

:~$ bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

Now, close your shell/terminal and open a new one and run the following command.

:~$ source ~/.profile

Post Install Configuration:

Put the following line at the very end of your .profile file.

:~$ [[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

Executes the following command, it will read and run the each command in the script:

:~$ . "$HOME/.rvm/scripts/rvm"


Open a new shell/terminal, and play around with rvm.


Install Ruby:

:~$ rvm pkg install zlib
:~$ rvm install 1.9.2
:~$ rvm use 1.9.2

You could have install any version of the ruby by replacing the "1.9.2" from the above command with you desired version.

Creating Gemsets

Rvm provide a feature called gemset, it is a complete set of all the gems that we are using for a single application. With the help of these gemsets, we create a separate and independent set of gems for each project and can switch around very easily depending upon the project. First of all create a gemset by:

:~$ rvm gemset create rails3.1.3

Now, tells RVM to use this gemset with the mentioned version of ruby for our application by running the following command.

:~$ rvm use 1.9.2@rails3.1.3

Installing Rails and Some other Gems

:~$ gem install rails -v='3.1.3'

If you got some error like "Uninitialized constant Gem", then run the following set of commands:

If there is some error like Unintialized constant Gem, then:

:~$ rvm package install zlib
:~$ rvm remove 1.9.2
:~$ rvm install 1.9.2 --with-zlib-dir=$rvm_path/usr
:~$ rvm gemset create rails3.1.3
:~$ rvm use 1.9.2@rails3.1.3
:~$ gem install rails

If get the following error:

"ERROR:  While executing gem ... (Gem::DependencyError)
    Unable to resolve dependencies: rails requires actionpack (= 3.1.3), activerecord (= 3.1.3), activeresource (= 3.1.3), actionmailer (= 3.1.3), railties (= 3.1.3); activesupport requires multi_json (~> 1.0)"
then:

:~$ gem update
:~$ gem install rails -v='3.1.3' --include-dependencies

We have done with our installations, lets start our first app.

Netbeans Installation

To install netbeans, first you need to install JVM which you can install by issuing the following command.

:~$ sudo apt-get install openjdk-6-jdk

Then, Download the netbeans from the official site of netbeans which you can find here.

Now go to the directory where the downloaded file and run the following command.

:~$ cd path/to/netbean_downloaded_file
:~/path/to/file$ sh netbeans-6.9rc2-ml-linux.sh


Congrats...!!!
Your environment setup is complete. You can play around by creating the rails app. If you have any queries, then please let me know. All of your comments will be appreciated.


4 comments:

  1. nice work done man... really good job done!!!

    ReplyDelete
  2. You just saved my life!! This post worked like charm for me!! Thanks buddy :)

    ReplyDelete
  3. Awesome post.

    Run command "rvm autolibs enable" before "rvm pkg install zlib"

    ReplyDelete
  4. Great work.

    For latest netbeans: https://netbeans.org/downloads/
    And use "sudo apt-get install openjdk-7-jdk" for JDK 7.

    ReplyDelete