Just a few commands will get you started with Ruby on Rails on Ubuntu. If you want to use a different Linux distribution, most of this tutorial will still be correct. You will have to translate all of the apt-get statements and package names to your distribution of choice. Configuration file paths and layouts may also differ. If you would like more information about how to set up a Linux VM using our system, please visit our other tutorials.
Step 1: Install Ruby Version Manager
First, fire up a fresh terminal and ensure your package lists are up-to-date:
sudo apt-get update
Now we need to use curl. If you’re running one of our pre-installed images then you can skip this step, but if you don’t already have it…
sudo apt-get install curl
…which will allow us to install Ruby Version Manager – the tool we’ll use to get the latest version of Ruby.
curl -L https://get.rvm.io | bash -s stable
Now we need to load RVM and make sure it’s added to .bashrc. The filepath will change according to user, but if you’re logged in as root, run these two commands:
echo '[[ -s "/usr/local/rvm/scripts/rvm" ]] && source "/usr/local/rvm/scripts/rvm"' >> ~/.bashrc source ~/.bashrc
RVM’s dependencies can then be installed with one command.
Step 2: Install Ruby
Now we use RVM to install Ruby itself…
rvm install ruby
…and tell it to use the latest version (the one you’ve just downloaded) by default:
rvm use ruby --default
Step 3: Install RubyGems
rvm rubygems current
RubyGems is a package manager for Ruby, similar to EasyInstall for Python.
Step 4: Install Rails
gem install rails
Step 5: Create your application
rails new blog
This will create the skeleton of a new application called “blog”, with all the necessary files and directories. It then runs the “bundle install” command automatically upon completion, which might take some time to execute.
Step 6: Start your server
This will start a server listening on port 3000.
To revel in your creation, go to http://your-IP-address:3000 and you’ll see:
To get Rails saying “Hello”, you need to create at minimum a controller and a view.
Step 7: Hello World
rails generate controller welcome index
This creates an index.html.erb file and route to it (This is the view), and a controller (called welcome_controller.rb). It also installs, amongst other things, a helper, SCSS and CoffeeScript files, but if we open the index file in a text editor:
and write something appropriate like:
we are almost there. All we need to do is make sure this file will be pointed to when users navigate to your root URL, in our case, http://your-IP-address:3000, instead of the “Welcome aboard” page. We do this by changing the config/routes.rb
Uncomment the line containing
What this does is well explained by the Rubyonrails.org guide – “The root “welcome#index” tells Rails to map requests to the root of the application to the welcome controller’s index action and get “welcome/index” tells Rails to map requests to http://your-ip:3000/welcome/index to the welcome controller’s index action. This was created earlier when you ran the controller generator (rails generate controller welcome index).”
Step 8: Bask in the glory of your own awesomeness
Now just navigate to http://your-IP-address:3000 once again to see your Ruby on Rails application in action!