Setup Simple Ruby on Rails App On Ubuntu 16.04 From Scratch

Rails is one of the most popular ruby framework out there. And now, I want to try to run the simple app on Ubuntu 16.04 machine. it’s for testing purpose.

First, update the system and install essential dependencies:

$ sudo apt-get update
$ sudo apt-get build-essential curl sudo vim

Install nodejs:

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

Create a dedicated user for the app, for example, ubuntu user. And this also make the ubuntu user with sudo privilege and run the command without password. Which is useful to run command that needs sudo privilege in the next steps.

$ useradd ubuntu -m
$ echo 'ubuntu ALL=(root) NOPASSWD: ALL' >> /etc/sudoers

swith to ubuntu user and install GPG keys for install rvm:

$ su - ubuntu
ubuntu~$ gpg --keyserver hkp://keys.gnupg.net \ --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 \ 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

Download and install rvm:

ubuntu~$ \curl -sSL https://get.rvm.io | bash -s stable

Install ruby interpreter with version 2.5.1, you might wanna change it with your preferable version:

ubuntu~$ source ~/.rvm/scripts/rvm
ubuntu~$ rvm install 2.5.1
ubuntu~$ rvm use 2.5.1 --default

Install rails with gem, and create new app without writing the Gemfile. Why? because everytime I create new app, I ended up facing errors with dependencies in Gemfile. So, it safe to setup new app without the Gemfile, we’ll create it manually later.

ubuntu~$ gem install rails
ubuntu~$ rails new app --skip-gemfile

Create the Gemfile:

ubuntu~$ touch ~/app/Gemfile
ubuntu~$ vim ~/app/Gemfile

Gemfile, fill these dependencies below into the file, save and exit:

source 'https://rubygems.org'
gem 'rails', '~> 5.2.1'
gem 'bootsnap', '~> 1.3.2'
gem 'tzinfo-data', '~> 1.2018.5'
gem 'listen', '~> 3.1.5'
gem 'sqlite3'

Now, install all the gems with bundle:

ubuntu~$ cd ~/app
ubuntu app~$ bundle install

Try run the rails:

ubuntu app~$ rails server -b 0.0.0.0

 

Run Ruby on Rails App for the first time only with nginx

So I’m just lazy, I know there is better way to serve Ruby on Rails with unicorn or uWSGI, but I will try later.

This time I just want a have a quick look how’s the RoR running on the browser only with nginx, just a quick guide, no bullshit. so currently I have rails installed on my ubuntu machine 14.04 with mysql database configured and nginx. So make sure you have those things installed properly to follow this guide.

I usually create my local domain first to navigate it easier:

$ sudo echo -e "127.0.0.1\tmyapp.local" >> /etc/hosts

Create the nginx config for your web app :

$ sudo vim /etc/nginx/conf.d/myapp.conf

very simple myapp.conf looks like:

server {
 listen 80;
 server_name www.myapp.local myapp.local;
 charset utf-8;
 access_log /var/log/nginx/myapp.access.log;
 error_log /var/log/nginx/myapp.error.log;
 location / {
 proxy_pass http://127.0.0.1:3000;
 }
}

Check nginx config and restart

$ sudo nginx -t
$ sudo service nginx restart

Run the rails (I told you, I’m just lazy) :

$ cd /myapp
$ rails server &

And now, Rails is running with port 3000 which nginx already addressed to myapp.local domain. Open up the browser and navigate to http://myapp.local

Setup Ruby On Rails on Ubuntu 14.04 Trusty Tahr

I’m sick of people who write tutorials which is not clear to install the Rails, too long, too much multiple choice, rambling. This is tutorial that actually works for me. Hope it would work for you too.

Install some dependencies first

$ 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

And we install the ruby, the current new version when this post is written is ruby 2.4.1, you might want to change with the newer version if available.

$ wget http://ftp.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.gz
$ tar xzvf ruby-2.4.1.tar.gz
$ cd ruby-2.4.1
$ ./configure
$ make
$ sudo make install

Check the ruby version

$ ruby -v

Install bundler and Rails with gem

$ sudo gem install bundler
$ sudo gem install rails

Check the Rails version

$ rails -v

Create new app

$ rails new my_new_app