How to install Ruby on Rails on Ubuntu Server
The Issue
If you seeing this then you are probably looking for guidance on how to install ruby on rails on Ubuntu server for the production environment.
If so then keep reading because the solution would be here
I would also cover installing the Elasticsearch server, integrate it with the rails app so you could add search functionality to your app easily
You should already know what is Ubuntu server and what is rails application so I’ll try to be concise as much as I can.
Important Notes
- The first important note about this integration is the version number, You should stick with it exactly as it is, any change would make a conflict later which would cause a severe impact on the installation.
- In this code you would get to know how to install ruby gems on ubuntu, I would use rvm to install rails on the ubuntu server and the ubuntu server version 18.04 LTS.
- Please don’t forget to stick with the rvm versions, any gems versions, and Elasticsearch version so you can integrate them successfully.
- I would use Nginx as a server because it’s easier to debug.
- Some of this code is related to some specific gems that related to my app like gem ‘acts-as-taggable-on’
- Feel free to exclude it, also I would like to point at something in this command RAILS_ENV=production rake searchkick:reindex CLASS=Element.
- Element in my application is the model that I would like to use the search function. If you want to use it for User, just change it. You can index as many models as you want.
- Finally, I would like to point a very important thing which is the server requirement. You need at least 2GB RAM to make this work, 1 GB for the rails application and the other for the Elasticsearch engine.
Code steps to install Ruby on Rails on Ubuntu Server
-
Add User
adduser amre usermod -aG sudo amre sudo visudo root ALL=(ALL:ALL) ALL amre ALL=(ALL:ALL) ALL sudo apt-get update sudo apt-get upgrade
-
Install Curl
sudo apt install curl curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash - curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list sudo apt-get update sudo apt-get install git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev nodejs yarn sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev
-
Install rvm Ubuntu
sudo reboot gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB curl -sSL https://get.rvm.io | bash -s stable source ~/.rvm/scripts/rvm rvm install 2.5.8 rvm use 2.5.8 --default ruby -v
-
Install Gem Ubuntu
gem install sprockets -v 3.7.2 gem install bundler -v 1.17.3 gem install rails -v 4.2.9
-
Install Git Ubuntu
git config --global color.ui true git config --global user.name "Amreos" git config --global user.email "[email protected]" ssh-keygen -t rsa -b 4096 -C "[email protected]" cat ~/.ssh/id_rsa.pub ssh -T [email protected]
-
Instal mysql Ubuntu
sudo apt-get install mysql-server mysql-client libmysqlclient-dev sudo mysql CREATE USER 'amreos'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON * . * TO 'amreos'@'localhost'; FLUSH PRIVILEGES; \q mysql -u amreos-p CREATE DATABASE amreos; USE amreos; \q
-
Install Nginx Ubuntu
sudo apt-get update sudo apt-get install -y nginx-extras git-core nginx -y
-
Clone Rails
git clone [email protected]:username/in3case.git cd in3case //change your server IP sudo nano config/deploy.rb git add . git commit -m "change IP" git push bundle rake acts_as_taggable_on_engine:install:migrations
-
Deploy rails “initial”
cap production deploy:initial sudo rm /etc/nginx/sites-enabled/default sudo ln -nfs "/home/amre/apps/in3case/current/config/nginx.conf" "/etc/nginx/sites-enabled/in3case" sudo service nginx restart
-
Install Java Ubuntu
sudo apt-get install default-jdk sudo nano /etc/environment JAVA_HOME="/usr/lib/jvm/openjdk-11" source /etc/environment echo $JAVA_HOME
-
Install Elasticsearch Ubuntu
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add - sudo apt-get install apt-transport-https #echo "deb https://artifacts.elastic.co/packages/6.8.6/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-6.8.6.list sudo apt-get update wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.0.deb sudo apt install ./elasticsearch-6.8.6.deb sudo nano /etc/elasticsearch/elasticsearch.yml change network.host 0.0.0.0 sudo /bin/systemctl enable elasticsearch.service sudo /bin/systemctl start elasticsearch.service sudo reboot RAILS_ENV=production rake searchkick:reindex CLASS=Element RAILS_ENV=production bundle exec rails c Element.reindex sudo service nginx restart
-
Final deployment
cap production deploy
My gem file
source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.2.9' # Use mysql as the database for Active Record # Use SCSS for stylesheets gem 'sass-rails' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # Use CoffeeScript for .coffee assets and views gem 'coffee-rails', '~> 4.1.0' # See https://github.com/rails/execjs#readme for more supported runtimes # gem 'therubyracer', platforms: :ruby # Use jquery as the JavaScript library gem 'jquery-rails' # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks gem 'turbolinks' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', '~> 0.4.0', group: :doc gem 'devise' gem 'cancan' gem 'carrierwave' gem 'cloudinary' gem 'simple_form' gem 'bootstrap' gem 'mini_magick' gem 'item' gem 'acts_as_votable' gem "punching_bag" #gem 'bootstrap-glyphicons' gem 'glyphicons' gem 'searchkick' gem 'will_paginate', '~> 3.1.0' gem 'ancestry' gem 'friendly_id' gem 'acts-as-taggable-on' gem 'omniauth-facebook' gem 'omniauth-twitter' gem 'omniauth-google' gem 'mysql2', '>= 0.3.13', '< 0.5' gem 'puma' #gem 'pg', '~> 0.21.0' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' #gem 'capistrano', '~> 3.7', '>= 3.7.1' #gem 'capistrano-rails', '~> 1.2' #gem 'capistrano-passenger', '~> 0.2.0' # Add this if you're using rbenv #gem 'capistrano-rbenv', '~> 2.1' # Use Unicorn as the app server # gem 'unicorn' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development group :development do # Access an interactive console on exception pages or by calling 'console' anywhere in the code. gem 'capistrano', require: false gem 'capistrano-rvm', require: false gem 'capistrano-rails', require: false gem 'capistrano-bundler', require: false gem 'capistrano3-puma', require: false gem 'web-console', '>= 3.3.0' gem 'listen', '>= 3.0.5', '< 3.2' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring # gem 'spring' # gem 'spring-watcher-listen', '~> 2.0.0' end group :test do # Adds support for Capybara system testing and selenium driver gem 'capybara', '>= 2.15' gem 'selenium-webdriver' # Easy installation and use of web drivers to run system tests with browsers gem 'webdrivers' end
Hooray! That is it, Folks.
Final Words
This is a working code, did it myself after a lot of hard work. I hope this code helps you through your technical issues and answers the big question, How to install ruby on rails on the Ubuntu server?
Please feel free to contact us or leave a comment if you have any questions or problems with this deployment.
Don’t forget to like and share if it works for you!
Articles could be useful