Installing Ruby on Rails on linux

- 5 mins

Summary:

We are going to install ruby on rails on Linux. For this tutorial I am using Xubuntu 16.04 LTS.

Index


What is Xubuntu and why Xubuntu

Xubuntu is Linux based distro (distro=a Linux distribution). It is inherited from popular a Linux distro Ubuntu. It’s all about personal preference of which distro you want to use. Difference between Ubuntu and Xubuntu is in their user interface. Whether Ubuntu has Unity interface, Xubuntu use XFCE which is much lighter comparing to former one. All of the command used here will work on ubuntu and other distros based on ubuntu(Lubuntu, Kubuntu, Edubuntu and obviously Xubuntu).


Installing ruby

First we have to install some dependencies for ruby. Open terminal (CTRL+ALT+T) and run following commands one by one

sudo apt-get update
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 nodejs

There are three methods to install ruby. Popular one is using rbenv which I am going to use here. But there are two different ways you can use- using rvm or from source. Both of them are little bit of complex than rbenv method. From command line go to root path

cd ~

Install rbenv

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 install ruby version 2.4.0

rbenv install 2.4.0
rbenv global 2.4.0

Check if it is installed correctly.

ruby -v

If everything is ok you will see ruby version like this

Ruby 2.4.0

Now we have to install bundler.


What is bundler?

According to the Official site Bundler provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that are needed.

So to maintain gem in ruby project bundler keep us out from several harsh trap. It is a must have component for any rails project. Run this command to install bundler

gem install bundler

And run this after bundler has installed.

rbenv rehash

Git configuration

Check this article for in depth git setup.


Installing Rails

New version of rails comes up with several dependencies. We need to install JavaScript runtime NodeJS so that we can use coffeescript and asset pipeline. To install NodeJS run (check official documentation for more details.

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs

Now install rails. (Current version is 5.0.1)

gem install rails -v 5.0.1

And again

rbenv rehash

voila. It’s done. Now check out our rails version using

rails -v

Installing database

Rails comes up with default SQLite database. Though this is OK for little projects. But more robust database like MySQL or PostgreSQL would be a good choice to use. Here I am going to show you the process of installing PostgreSQL and integrate it with rails application.

Install PostgreSQL with required dependencies

sudo apt-get install postgresql postgresql-contrib libpq-dev

Now we have to create database user and password.

Choose an username that is exactly similar to your system user name. If you are login into xubuntu with ‘xyz’ username then create database user with ‘xyz’ name. In some cases if database username doesn’t match with system it shows fatal error. Warning: ERROR(fatal: role does not exist)

Go to command line. Replace ‘xyz’ with your username

sudo -u postgres createuser -s -r xyz

Enter PostgreSQL console to set password

sudo -u postgres psql

Select user to set password

\password xyz

Set password for xyz username and confirm it. Exist PostgreSQL console using

\q

The end

So far so good. Let’s create our first application. Give any name as you like to ‘appname’

rails new appname -d postgresql

-d for database. Default database is SQLite. But we want to use PostgreSQL.

or if you want to use specific version of rails (like 4.2.0) run like this command.

rails _4.2.0_ new appname -d postgresql

Note: Remember, to install a specific version of rails application you must have that version installed in your system. Running multiple version of rails is a painful issue. So if you do not have any specific reason to use different version stick up with latest version.

Configure database information:

Go to rails project folder and navigate to config/database.yml

Remove comment from following lines (by removing #) and set previously create PostgreSQL username and password. If these lines are not included add them.

host: localhost

username: xyz

password: given password for xyz

Now save and return to console.

Go into application directory

cd appname

Run command to create database

rake db:create

Fire up rails server

rails server

or

rails s

Now from your browser visit http://localhost:3000 to view your application!

If you see the Yay message you are officially running your first rails application.

If you received an error that said Access denied for user ‘root’@’localhost’ (using password: NO) then you need to update your config/database.yml file to match the database username and password.

comments powered by Disqus
rss facebook twitter github youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora