Up and running your first Ruby on Rails app [with troubleshooting]
Ruby on Rails are build on top of many existing language and frameworks and running it is not as simple in first go. This might cause to loose interest in new developer like me(in Ruby on Rails). I am writing quick way to install and fix any coming issue to run your first ruby on rail application.
For this demonstration, i will be using MacOS Mojave. However steps will be quite universal and you can run it with your corresponding OS once its clear to you.
Step 1 — Installing rbenv
To start, rbenv
is package which will make sure it starts automatically on boot of your system. Your Homebrew
can be used to install.
brew install rbenv
Now to make it load automatically in terminal, you need to update your bash configuration file by editing .bash_profile
in your home directory
nano .bash_profile
and add following line at bottom of file.
eval "$(rbenv init -)"
Save the file and quit.
Now apply changes by reloading your bash profile. To do so, run
source ~/.bash_profile
Step 2 — Ruby installation
Now you can use rbenv
to install Ruby. To do so, run following command, this will install latest version of Ruby.
rbenv install
to set the specific ruby version to global, you can run command
rbenv global 2.6.3
Now you can verify installation using
ruby -v
Step 3 — Setting up gems
At the time of gem installation, it would generate local documentation thus consuming more time and space, to turn this off, first run following command.
echo "gem: --no-document" > ~/.gemrc
Now go ahead and install bundler
gem install bundler
Step 4 — Installing Rails
Rails can be installed using gem install rail command and if you want to specify version, you can do so by passing -v
flag.
gem install rails -v 6.0.0
to activate new changes in directory, you need to rehash. to do so
rbenv rehash
Now you can verify Rails installation by command
rails -v
Step 5 — Creating first Application
lets create first application by creating directory first say projects directory and application name will be blog
~/projects$ rails new blog
this will create new application named blog which will have all Ruby on rail structure along with all required gems installed.
Step 6 — Running your application
To run your application in development environment, you need to go inside your project directory using command line.
Once inside, you need to run
rails server
and this should output
=> Booting Puma
=> Rails 6.1.4 application starting in development
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 5.3.2 (ruby 2.6.3-p62) ("Sweetnighter")
* Min threads: 5
* Max threads: 5
* Environment: development
* PID: 64574
* Listening on http://127.0.0.1:3000
* Listening on http://[::1]:3000
Use Ctrl-C to stop
Started GET "/" for 127.0.0.1 at 2021-07-26 22:30:29 +0530
(5.4ms) SELECT sqlite_version(*)
Processing by Rails::WelcomeController#index as HTML
Rendering /Users/userName/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-6.1.4/lib/rails/templates/rails/welcome/index.html.erb
Rendered /Users/userName/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-6.1.4/lib/rails/templates/rails/welcome/index.html.erb (Duration: 9.6ms | Allocations: 532)
Completed 200 OK in 49ms (Views: 18.1ms | ActiveRecord: 0.0ms | Allocations: 4409)
Now you can visit http://127.0.0.1:3000
and see your first Rails application running.
Debugging
While you ran rails server
command, you might face issue like Webpacker configuration file not found
. To resolve this issue. you have to follow instruction below.
bundle exec rails webpacker:install
This command itself require yarn
to complete, and yarn
require nodejs
to be installed.
To install nodejs: https://nodejs.org/en/download/
Once nodejs
is installed, run
npm install --global yarn
After both of above done setup, you can re-run `bundle exec rails webpacker:install
and you setup is completed.
You can now run rails server
command and open your rails application in browser
If you find this helpful, you can share/tweet so devs can find easily.
Troubleshooting step i have separately mentioned in my stackoverflow answer
as well.