Friday 20 May 2016

Rails on Ubuntu 16.04

Initialization



Update your system.


sudo apt get update


Install all ruby and rails related dependencies.


sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev


Installing ruby



There are various ways you can install ruby. We are going to install ruby with rvm. Rvm lets us manage various versions of ruby. It also provides container for various gem bundles for different applications. This is very convenient for multiple applications development.


Install rvm related dependencies.


sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev


Get latest stable rvm version.


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


You may encounter an error regarding signature verification. Just follow the instructions on screen and try above command again.


Execute rvm file to load on terminal.


source ~/.rvm/scripts/rvm


Check rvm version.


rvm -v


Install ruby version.


rvm install 2.3.1


This was the latest ruby version while writing this blog. This might take some time, have a coffee or a tea if that’s your thing. :)


Check the ruby version.


ruby -v


Make installed ruby version default.


rvm use 2.3.1 --default


Configuring git



Git is a great version management for your applications. Head towards GitHub and register your account if you haven’t already.


Make color code for git information true.


git config --global color.ui true


Enter your GitHub name and email.


git config --global user.name "YOUR NAME"
git config --global user.email "YOUR@EMAIL.com"


This is important because every Git commit uses this information, and its immutably baked into the commits you start creating.


Generate ssh key. (more information)


ssh-keygen -t rsa -b 4096 -C "YOUR@EMAIL.com"


Just keep pressing enter and ssh key will be generated.


Display the content of generated key file with following command.


cat ~/.ssh/id_rsa.pub


Copy the content and paste it in your GitHub ssh setting.


Enter following command to verify connection.




Installing Rails



Rails requires JavaScript run-time so we will install Node.js. This lets us use CoffeScript and asset pipeline.


curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -


sudo apt-get install -y nodejs


Remember the container I talked about earlier, we will create one for rails installation.


rvm gemset create rails-4.2.6


At the time of writing this blog latest stable version of rails was 4.2.6, so we created gemset related to that.


List gemsets.


rvm gemset list


Use created gemset.


rvm gemset use rails-4.2.6


This will select rails-4.2.6 gemset. You can verify with list command.


gem install rails -v 4.2.6


This will install rails and its dependent gems. Dude that’s going to take some time play some game in your phone, also let me know if really good.


All done ! Check the version just to make sure.


rails -v


Setup MySQL



Rails by default uses SQLite database, but we will install MySQL as its more robust.


sudo apt-get install mysql-server mysql-client libmysqlclient-dev


While installation you will be asked in enter password for MySQL root user, keep it secure one.


Creating first app



rails new myapp -d mysql

This will create rails app skeleton.


Move into application directory.


cd myapp


Change the config/database.yml to set username and password for MySQL setting.


nano config/database.yml


Create database and run application.


rake db:create


rails s


Navigate to http://localhost:3000 to check you application.


Congratulation now your Ubuntu is rocking with rails.

Conclusion


  We have come to an end of installation guide. There are many other packages and awesome gems to be installed but that depends on application requirements. Anyways this is just a beginning. Happy coding !!!



No comments:

Post a Comment