How to setup Ruby on Rails on Ubuntu 16.04 Xenial Xerus - OtagoPolytechnic/opp-cico GitHub Wiki

Overview

This tutorial will explain the steps you need to take to install a Ruby on Rails development environment on an Ubuntu 16.04 Xenial Xerus machine. These commands may also work on other versions of Linux but its not guaranteed.

The reason I recommend you develop on a Linux machine is because when you are ready to deploy your app to the internet, 99.9% of the time you will be running it on a Linux server. So it helps if you develop on Linux, that way it will be guaranteed to work on a Linux server.

Installing Ruby

First we need to install some dependencies for Ruby.

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

Next we will install Ruby using rbenv. Most people prefer using rbenv these days as it has the least issuses.

Installing with rbenv is a simple two step process. First you install rbenv, and then ruby-build.

cd
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

rbenv install 2.3.1
rbenv global 2.3.1
ruby -v

The last step is to install Bundler.

gem install bundler

rbenv users need to run the rehash command after installing bundler.

rbenv rehash

Installing Rails

The next step is to install a JavaScript runtime like NodeJS that will allow us to use Coffeescript and the Asset Pipeline in Rails.

To install NodeJS, we’re going to add it using the official repository.

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

Install Rails

gem install rails -v 4.2.6

rbenv users need to run the rehash command again to make the rails executable avaliable.

rbenv rehash

We can easily check everything has been installed correctly by running this command.

rails -v
# Rails 4.2.6

If you get a different result for some reason, it means your environment may not be setup properly.

Setting Up PostgreSQL

We are going to use a PostgreSQL database with our Rails app as it is a solid database to use and has some better features when we want to move our app to production. We will install PostgreSQL using an apt-key.

sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-common
sudo apt-get install postgresql-9.5 libpq-dev

It is good practice to make our own user for PostgreSQL.

sudo -u postgres createuser "YOUR USERNAME HERE" -s

Now we will login in with the default PostgreSQL user and set the password of the new account we just created.

sudo -u postgres psql
postgres=# \password "YOUR USERNAME HERE"

Now you will want to locate your pg_hba.conf at /etc/postgresql/9.5/main/ and use nano to edit it. In the conf file you will want to change all lines that say peer to trust. This allows the rails app to access your database.

Finishing up

We are now ready to see if all this setting up is going to work! We can test this by creating your first rails app!

# Using PostgreSQL
rails new myapp -d postgresql

Rails will automatically create everything you need to get started. Give it some time.

Next you will want to move into the root of your rails app.

# Move into the application directory
cd myapp

Move into the config folder. Here you will find a database.yml file that will need to be configured with the PostgreSQL user we created and the password associated to this user.

Now move back into the root of your rails app and run this command to create the database.

# Create the database
rake db:create

We are now ready to launch your rails app.

rails server

If everything has gone to plan so far you should be able to find your app at localhost:3000.

Next Steps

Congratulations for setting up your Rails environment. The next step is to configure Git and clone the current code base of Cico and start developing! You can find a tutorial for this here.

⚠️ **GitHub.com Fallback** ⚠️