Archive for the ‘RVM’ Category

A Step by Step Guide to Setup Rails application on Ec2 instance (Ubuntu Server)

Sometimes the Bitnami or other Rails AMIs doesn’t fit your needs directly and you will feel the need of building the Server yourself.Here I go step by step in building such a stack on top of Amaon EC2 Ubuntu Server.
  • Rails applications are a little bit different to install on servers but the process is very easy.Rails application needs a web server and an application server to run with. For development, it comes with default Webrick server that serve as application server on local machine. For setting it up on production server, we have the following choices on Web and application servers :-

    • Web servers

      1. Apache

      2. Nginx

    • Application Servers

      1. Passenger

      2. Thin

      3. Puma

      4. Unicorn

  • The simplest and best combination consists of Nginx + Passenger. It allows greater flexibility for configuration and also allows good speed over other combinations. So we are going to setup an Rails application using Nginx + passenger configuration on a bare Ubuntu server. Here are the steps :-

  1. Launch an Ec2 instance with ubuntu AMI. Make sure you have HTTP and SSH access to the server.

  2. SSH into the server by using private key (.pem) used while launching the instance and install the available updates by running :-

    sudo apt-get install updates
  3. Now you need to setup ruby on your server, so install the single user rvm ruby by following this blog.
  4. Load the rvm and make the installed ruby as default by running the following commands :-

    source ~/.rvm/scripts/rvm
    rvm use 2.1.0 –default
  5. Install the version control to clone your rails application to server. We generally use Git with rails application which can be installed by running the following command :-

    sudo apt-get install git
  6. Now clone your application on the server :-

    git clone yourepo.git

    Note:- In case of private git repository, you need to add public key of server to deploy keys of your repository, otherwise you will be promped with an permission denied error.


    Deploy using application to this server using Capistrano script. Please read this blog for more details on deploying your application using Capistrano.

  7. Now go to your application and install the gems by running bundle install command. If you want to setup your database on the same server, you can do the same by using the following commands 😐

    • In case of MYSQL

      sudo apt-get install mysql-server mysql-client
      sudo apt-get install libmysql++-dev
    • In case of POSTGRESQL, follow this blog for installation and then install the development headers

      sudo apt-get install libpq-dev

      After setting this up, migrate your databases in whichever environment you want to launch the server.

  8. Now install the Passenger gem by running :-

    gem install passenger

  9. Next step is to install the Nginx server, but we have some pre-requisits for this.

      1. It needs curl development headers which can be installed by :-

        sudo apt-get install libcurl4-openssl-dev

      2. It will be installed under /opt directory and your user should have permissions to that folder, so make your user as user owner for /opt directory by :-

        sudo chown -R ubuntu /opt

  10. Now install the Nginx server with passenger extension by running the following command :-


  11. Set your Nginx server as service in init script by using the following commands :-

    wget -O
    sudo mv /etc/init.d/nginx
    sudo chmod  +x /etc/init.d/nginx
    sudo /usr/sbin/update-rc.d -f nginx defaults
  12. Setup your application path in the nginx configuration file i.e. /opt/nginx/conf/nginx.conf.

    server {

    listen 80;

    server_name localhost;

    root /home/ubuntu/my_application/public #<-- be sure to point to 'public'

    passenger_enabled on;

    rails_env production;


  13. Lastly start your server by running the following command :-

    sudo service nginx start


Setting up single user rvm on ubuntu

RVM is a great tool for Ruby and Ruby on Rails(RoR) developers managing multiple RoR Projects with different ruby and gem configurations. Ubuntu has been growing as a choice for popular development and server Operating system. Lets hop onto our famous 1,2,3 howto format on how to install RVM on an ubuntu machine:
  1. Please ensure that you have curl installed on your system. If not, then its as simple as:
    sudo apt-get install curl
  2. Follow the single user installation instructions given on the rvm site to install rvm onto your system. You will find the instructions here.
  3. After you are done with the single user installation,  on your shell prompt, type
    type rvm | head -1
  4. If you get the following output, then rvm is correctly installed on your system. You can stop reading here and enjoy the magic of rvm on your ubuntu box .. 🙂
    rvm is a function
  5. If you dont get the above output, then we need to perform following additional tasks on your ubuntu box to configure and use rvm properly on your system.
  6. Go to your user’s home directory and open up .profile in your favorite editor such as vi/gedit and add the following lines to the end of file(This basically tells the shell to check if the rvm script exists at the default rvm script location($HOME/.rvm/scripts), and if it exists then executes all the commands in that rvm script files):
     [[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function 
  7. The next step is to load the rvm for both interactive and non-interactive shells. This is the step which is missed by most of the beginners and major cause of lot of frustration around troubleshooting the rvm install. Find the following lines in ~/.bashrc file of yours:
  8. # if you see '[ -z "$PS1" ] && return' then you must change this line to:
    if [[ -n "$PS1" ]] ; then ... original content that was below the && return line ...
    fi # <= be sure to close the if. #EOF .bashrc Even if you use zsh you should still adjust the .bashrc as above.
  9. And that should be it. The command type rvm | head -l should work perfectly fine now.
Enjoy Multiple ruby and gem versions !!