{"id":5895,"date":"2018-08-03T15:42:36","date_gmt":"2018-08-03T11:42:36","guid":{"rendered":"https:\/\/www.howtoforge.com\/tutorial\/ubuntu-ruby-on-rails\/"},"modified":"2018-08-03T15:42:36","modified_gmt":"2018-08-03T11:42:36","slug":"how-to-install-ruby-on-rails-on-ubuntu-18-04-lts","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/how-to-install-ruby-on-rails-on-ubuntu-18-04-lts\/","title":{"rendered":"How to Install Ruby on Rails on Ubuntu 18.04 LTS"},"content":{"rendered":"<p>Ruby on Rails (RoR) is an open source web application framework, published under the MIT License. It is a server-side web application framework that follows the MVC (Model-View-Controller) concept.<\/p>\n<p>Rails are providing default structures for the database, web service, and web pages. More than 3000 developers have contributed code to the Rails framework and there are many well-known applications based on Rails, such as Github, Airbnb, Soundcloud etc.<\/p>\n<p>In this tutorial, I will show you\u00a0the steps to install Ruby on Rails on Ubuntu 18.04 LTS. We will show you how to install and configure Rails with a PostgreSQL database, and how to create a new first project with Rails.<\/p>\n<h2 id=\"prerequisites\">Prerequisites<\/h2>\n<ul>\n<li>Ubuntu 18.04 LTS<\/li>\n<li>Root privileges<\/li>\n<\/ul>\n<h2 id=\"what-we-will-do\">What we will do?<\/h2>\n<ol>\n<li>Install RVM (Ruby Version Manager)<\/li>\n<li>Setup Ruby<\/li>\n<li>Install Nodejs<\/li>\n<li>Configure Ruby Gem<\/li>\n<li>Install Ruby on Rails<\/li>\n<li>Setup PostgreSQL Database for Rails Development<\/li>\n<li>Create Your First App with Rails and PostgreSQL<\/li>\n<\/ol>\n<h2 id=\"step-install-rvm-ruby-version-manager\">Step 1 &#8211; Install RVM (Ruby Version Manager)<\/h2>\n<p>RVM (Ruby Version Manager) is a command-line tool based on Bash and Ruby to manage the ruby installation. RVM allows you to install and configure multiple ruby versions on one system.<\/p>\n<p>The first step we will do is to install the rvm packages using the installer script.<\/p>\n<p>Add the rvm key to the server.<\/p>\n<p class=\"command\">gpg &#8211;keyserver hkp:\/\/keys.gnupg.net &#8211;recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 \\<br \/>7D2BAF1CF37B13E2069D6956105BD0E739499BDB<\/p>\n<p>Install the rvm stable version by running the command below.<\/p>\n<p class=\"command\">curl -sSL https:\/\/get.rvm.io | bash -s stable &#8211;ruby<\/p>\n<p>The command will automatically install packages required, and install the latest stable rvm version.<\/p>\n<p>After the installation is complete, run the following command.<\/p>\n<p class=\"command\">source \/usr\/local\/rvm\/scripts\/rvm<\/p>\n<p><a class=\"fancybox\" id=\"img-1\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_ruby_on_rails_on_ubuntu_1804_lts\/big\/1.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/08\/how-to-install-ruby-on-rails-on-ubuntu-18-04-lts.png\" alt=\"Install RVM\" width=\"500\" height=\"347\" title=\"\"><\/a><\/p>\n<p>Now you can use the rvm command to manage the ruby version.<\/p>\n<p class=\"command\">rvm version<\/p>\n<h2 id=\"step-setup-ruby-latest-version\">Step 2 &#8211; Setup Ruby Latest Version<\/h2>\n<p>The latest version of ruby at this day is Ruby 2.5.1, and it will be automatically installed during the rvm installation, when there is no ruby package on the system.<\/p>\n<p>In this step, we will set up the default ruby version on the Ubuntu system.<\/p>\n<p>Update the rvm to the latest stable version.<\/p>\n<p class=\"command\">rvm get stable &#8211;autolibs=enable<br \/>usermod -a -G rvm root<\/p>\n<p>Now check all available ruby versions.<\/p>\n<p class=\"command\">rvm list known<\/p>\n<p>And you will get a lot of available versions of ruby &#8211; install the latest stable version Ruby 2.5.1 using the rvm command as shown below.<\/p>\n<p class=\"command\">rvm install ruby-2.5.1<\/p>\n<p>After all installation is complete, make the ruby 2.5.1 as a default version on the Ubuntu system.<\/p>\n<p class=\"command\">rvm &#8211;default use ruby-2.5.1<\/p>\n<p>Check the Ruby version.<\/p>\n<p class=\"command\">ruby -v<\/p>\n<p>Now you will see ruby 2.5.1 is default ruby version on the Ubuntu 18.04 system.<\/p>\n<p><a class=\"fancybox\" id=\"img-2\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_ruby_on_rails_on_ubuntu_1804_lts\/big\/2.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/08\/how-to-install-ruby-on-rails-on-ubuntu-18-04-lts-1.png\" alt=\"Setup Ruby Latest Version\" width=\"500\" height=\"192\" title=\"\"><\/a><\/p>\n<h2 id=\"step-install-nodejs\">Step 3 &#8211; Install Nodejs<\/h2>\n<p>Ruby on Rails requires a JavaScript runtime to compile the Rails asset pipeline. And for the Rails development on Ubuntu Linux, it&#8217;s best to install and using Nodejs as the Javascript runtime.<\/p>\n<p>Add the nodejs nodesource repository to the system.<\/p>\n<p class=\"command\">curl -sL https:\/\/deb.nodesource.com\/setup_10.x | sudo -E bash &#8211;<\/p>\n<p>Install the latest version nodejs 10 and some additional packages using the apt command below.<\/p>\n<p class=\"command\">sudo apt install -y nodejs<br \/>sudo apt install gcc g++ make<\/p>\n<p>The nodejs 10 has been installed on Ubuntu 18.04 system.<\/p>\n<h2 id=\"step-configure-ruby-gem\">Step 4 &#8211; Configure Ruby Gem<\/h2>\n<p>RubyGems is a Ruby Package Manager, coming with the gem command-line tool. It&#8217;s automatically installed when we install Ruby on the system.<\/p>\n<p>Update gem to the latest version and check it.<\/p>\n<p class=\"command\">gem update &#8211;system<br \/>gem -v<\/p>\n<p><strong>Note:<\/strong><\/p>\n<p>This is optional, we can disable gem to install documentation on every ruby package installation. Simply by adding the configuration to the &#8216;.gemrc&#8217; configuration file.<\/p>\n<p class=\"command\">echo &#8220;gem: &#8211;no-document&#8221; &gt;&gt; ~\/.gemrc<\/p>\n<h2 id=\"step-install-ruby-on-rails\">Step 5 &#8211; Install Ruby on Rails<\/h2>\n<p>In this tutorial, we will be using the latest stable Ruby on Rails 5.2.0. We will install Rails using the gem ruby package manager.<\/p>\n<p>Install Ruby on Rails 5.2.0 using the command below.<\/p>\n<p class=\"command\">gem install rails -v 5.2.0<\/p>\n<p>After the installation is complete, check the rails version.<\/p>\n<p class=\"command\">rails -v<\/p>\n<p>And following should the result.<\/p>\n<p><a class=\"fancybox\" id=\"img-3\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_ruby_on_rails_on_ubuntu_1804_lts\/big\/3.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/08\/how-to-install-ruby-on-rails-on-ubuntu-18-04-lts-2.png\" alt=\"Install Ruby on Rails\" width=\"478\" height=\"153\" title=\"\"><\/a><\/p>\n<p>Ruby on Rails 5.2.0 has been installed on Ubuntu 18.04 LTS.<\/p>\n<h2 id=\"step-setup-postgresql-database-for-rails-development\">Step 6 &#8211; Setup PostgreSQL Database for Rails Development<\/h2>\n<p>By default, Ruby on Rails is using the SQLite database. It supports many databases system, including MySQL, SQLite, and PostgreSQL. And for this guide, we will be using PostgreSQL.<\/p>\n<p>Install the PostgreSQL database using the apt command below.<\/p>\n<p class=\"command\">sudo apt install postgresql postgresql-contrib libpq-dev -y<\/p>\n<p>After all installation is complete, start the Postgres service and enable it to launch everytime at system boot.<\/p>\n<p class=\"command\">systemctl start postgresql<br \/>systemctl enable postgresql<\/p>\n<p>Next, we will configure a password for the Postgres user, and create a new user for the Rails installation.<\/p>\n<p>Login to the &#8216;postgres&#8217; user and run the Postgres shell.<\/p>\n<p class=\"command\">su &#8211; postgres<br \/>psql<\/p>\n<p>Change the Postgres password using the query below.<\/p>\n<p class=\"command\">\\password postgres<\/p>\n<p>Type your password and the password for postgres user has been added.<\/p>\n<p>Now we will create a new role for our rails installation. We will create a new role named &#8216;rails_dev&#8217; with the privilege of creating the database and with the password &#8216;aqwe123&#8217;.<\/p>\n<p>Run the Postgres query below.<\/p>\n<p class=\"command\">create role rails_dev with createdb login password &#8216;aqwe123&#8217;;<\/p>\n<p>Now check all available roles on the system.<\/p>\n<p class=\"command\">\\du<\/p>\n<p>And you will get the &#8216;rails_dev&#8217; role on the list.<\/p>\n<p><a class=\"fancybox\" id=\"img-4\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_ruby_on_rails_on_ubuntu_1804_lts\/big\/4.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/08\/how-to-install-ruby-on-rails-on-ubuntu-18-04-lts-3.png\" alt=\"Setup PostGres\" width=\"500\" height=\"351\" title=\"\"><\/a><\/p>\n<p>PostgreSQL installation and configuration for Rails Development has been completed.<\/p>\n<h2 id=\"step-create-your-first-app-with-rails-and-postgresql\">Step 7 &#8211; Create Your First App with Rails and PostgreSQL<\/h2>\n<p>Ruby on Rails provides a command-line &#8216;rails&#8217; for bootstrapping our first rails application.<\/p>\n<p>Create a new project &#8216;myapp&#8217; with default database &#8216;PostgreSQL&#8217; by running rails command below.<\/p>\n<p class=\"command\">rails new myapp -d postgresql<\/p>\n<p>Now you will see the &#8216;myapp&#8217; directory, go to that directory and edit the database configuration file &#8216;database.yml&#8217; using <a href=\"https:\/\/www.howtoforge.com\/vim-basics\" target=\"_blank\" rel=\"noopener noreferrer\">vim <\/a>editor.<\/p>\n<p class=\"command\">cd myapp\/<br \/>vim config\/database.yml<\/p>\n<p>There are different configuration sections for each setup &#8211; Development, Testing, and Production.<\/p>\n<p>In the development section, uncomment those line and change the value as below.<\/p>\n<pre>username: rails_dev&#13;\npassword: aqwe123&#13;\nhost: localhost&#13;\nport 5423<\/pre>\n<p>For the testing section, paste those configurations under the testing section.<\/p>\n<pre> host: localhost&#13; port: 5432&#13; username: rails_dev&#13; password: aqwe123<\/pre>\n<p>Save and exit.<\/p>\n<p>Now generate the database and make sure there is no error.<\/p>\n<p class=\"command\">rails db:setup<br \/>rails db:migrate<\/p>\n<p>When all setup is complete, start the default puma rails web server using the command below.<\/p>\n<p class=\"command\">rails s -b 192.168.1.10 -p 8080<\/p>\n<p>The first rails project will be running on the IP address &#8216;192.168.1.10&#8217; with port 8080.<\/p>\n<p><a class=\"fancybox\" id=\"img-5\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_ruby_on_rails_on_ubuntu_1804_lts\/big\/5.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/08\/how-to-install-ruby-on-rails-on-ubuntu-18-04-lts-4.png\" alt=\"Create Ruby App\" width=\"500\" height=\"200\" title=\"\"><\/a><\/p>\n<p>Open your web browser and type the server IP address on the address bar.<\/p>\n<p><a href=\"http:\/\/192.168.1.10:8080\/\">http:\/\/192.168.1.10:8080\/<\/a><\/p>\n<p>You will get the default rails project homepage as below.<\/p>\n<p><a class=\"fancybox\" id=\"img-6\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_ruby_on_rails_on_ubuntu_1804_lts\/big\/6.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/08\/how-to-install-ruby-on-rails-on-ubuntu-18-04-lts-5.png\" alt=\"Ruby on Rails App is working\" width=\"500\" height=\"285\" title=\"\"><\/a><\/p>\n<p>Next, we will test to create simple CRUD with PostgreSQL database on rails.<\/p>\n<p>Run the rails command below.<\/p>\n<p class=\"command\">rails g scaffold Post title:string body:text<br \/>rake db:migrate<\/p>\n<p>Run the puma web server again.<\/p>\n<p class=\"command\">rails s -b 192.168.1.10 -p 8080<\/p>\n<p>And open the web browser with the URL below.<\/p>\n<p><a href=\"http:\/\/192.168.1.10:8080\/posts\/\">http:\/\/192.168.1.10:8080\/posts\/<\/a><\/p>\n<p>Now you will get the simple CRUD form.<\/p>\n<p><a class=\"fancybox\" id=\"img-7\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_ruby_on_rails_on_ubuntu_1804_lts\/big\/7.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/08\/how-to-install-ruby-on-rails-on-ubuntu-18-04-lts-6.png\" alt=\"Test App written in RoR\" width=\"500\" height=\"117\" title=\"\"><\/a><\/p>\n<p>And following is my result after creating a simple post.<\/p>\n<p><a class=\"fancybox\" id=\"img-8\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_ruby_on_rails_on_ubuntu_1804_lts\/big\/8.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/08\/how-to-install-ruby-on-rails-on-ubuntu-18-04-lts-7.png\" alt=\"\" title=\"\"><\/a><\/p>\n<p>Ruby on Rails installation with PostgreSQL database on Ubuntu 18.04 LTS has been completed successfully.<\/p>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Fubuntu-ruby-on-rails%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/08\/how-to-install-ruby-on-rails-on-ubuntu-18-04-lts-8.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Fubuntu-ruby-on-rails%2F&amp;text=How+to+Install+Ruby+on+Rails+on+Ubuntu+18.04+LTS&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/08\/how-to-install-ruby-on-rails-on-ubuntu-18-04-lts-9.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/08\/how-to-install-ruby-on-rails-on-ubuntu-18-04-lts-10.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Fubuntu-ruby-on-rails%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/08\/how-to-install-ruby-on-rails-on-ubuntu-18-04-lts-11.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Ruby on Rails (RoR) is an open source web application framework, published under the MIT License. It is a server-side web application framework that follows the MVC (Model-View-Controller) concept. Rails are providing default structures for the database, web service, and web pages. More than 3000 developers have contributed code to the Rails framework and there [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-5895","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/5895","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=5895"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/5895\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=5895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=5895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=5895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}