Last updated 6 December 2016
Install Ruby on Rails 5.2 on Ubuntu Linux. Up-to-date, detailed instructions for the Rails newest release. How to install Rails 5.2, the newest version of Rails, on Ubuntu.
This in-depth installation guide is used by developers to configure their working environment for real-world Rails development. This guide doesn’t cover installation of Ruby on Rails for a production server.
To develop with Rails on Ubuntu, you’ll need Ruby (an interpreter for the Ruby programming language) plus gems (software libraries) containing the Rails web application development framework.
For an overview of what’s changed in each Rails release, see a Ruby on Rails Release History.
If you’re new to Rails, see What is Ruby on Rails?, the book Learn Ruby on Rails, and recommendations for a Rails tutorial.
This is an article from the RailsApps project. The RailsApps project provides example applications that developers use as starter apps. Hundreds of developers use the apps, report problems as they arise, and propose solutions. Rails changes frequently; each application is known to work and serves as your personal “reference implementation.” Support for the project comes from subscribers. If this article is useful, please support us and join the RailsApps project.
Ubuntu is a popular platform for Rails development, as are other Unix-based operating systems such as Mac OS X. Installation is relatively easy and widespread help is available in the Rails developer community.
You’ll need an easy way to switch between Ruby versions. Just as important, you’ll have a dependency mess if you install gems into the system environment. I recommend RVM to manage Ruby versions and gems because it is popular, well-supported, and full-featured. If you are an experienced Unix administrator, you can consider alternatives such as Chruby or Sam Stephenson’s rbenv.
Conveniently, you can use RVM to install Ruby.
Ubuntu provides a package manager system for installing system software. You’ll use this to prepare your computer before installing Ruby. However, don’t use apt-get
to install Ruby. The package manager will install an outdated version of Ruby. And it will install Ruby at the system level (for all users). It’s better to use RVM to install Ruby within your user environment.
You can use Ruby on Rails without actually installing it on your computer. Hosted development, using a service such as Cloud9, means you get a computer “in the cloud” that you use from your web browser. Any computer can access the hosted development environment, though you’ll need a broadband connection. Cloud9 is free for small projects.
Using a hosted environment means you are no longer dependent on the physical presence of a computer that stores all your files. If your computer crashes or is stolen, you can continue to use your hosted environment from any other computer. Likewise, if you frequently work on more than one computer, a hosted environment eliminates the difficulty of maintaining duplicate development environments. For these reasons some developers prefer to “work in the cloud” using Cloud9. Cloud9 is a good option if you have trouble installing Ruby on Rails on your computer.
You’ll need to prepare your computer with the required system software before installing Ruby on Rails.
You’ll need superuser (root) access to update the system software.
Update your package manager first:
$ sudo apt-get update
This must finish without error or the following step will fail.
Install Curl:
$ sudo apt-get install curl
You’ll use Curl for installing RVM.
Use RVM, the Ruby Version Manager, to install Ruby and manage your Rails versions.
If you have an older version of Ruby installed on your computer, there’s no need to remove it. RVM will leave your “system Ruby” untouched and use your shell to intercept any calls to Ruby. Any older Ruby versions will remain on your system and the RVM version will take precedence.
Ruby 2.4.2 was current when this was written. You can check for the current recommended version of Ruby. RVM will install the newest stable Ruby version.
The RVM website explains how to install RVM. Here’s the simplest way:
$ \curl -L https://get.rvm.io | bash -s stable --ruby
Note the backslash before “curl” (this avoids potential version conflicts).
The “—ruby” flag will install the newest version of Ruby.
RVM includes an “autolibs” option to identify and install system software needed for your operating system. See the article RVM Autolibs: Automatic Dependency Handling and Ruby 2.0 for more information.
If you already have RVM installed, update it to the latest version and install Ruby:
$ rvm get stable --autolibs=enable $ rvm install ruby $ rvm --default use ruby-2.4.2
If you have trouble installing Ruby with RVM, you can get help directly from the RVM team using the IRC (Internet Relay Chat) channel #rvm on irc.freenode.net:
http://webchat.freenode.net/?channels=rvm
If you’ve never used IRC, it’s worthwhile to figure out how to use IRC because the RVM team is helpful and friendly. IRC on freenode requires registration (see how to register).
Since Rails 3.1, a JavaScript runtime has been needed for development on Ubuntu Linux. The JavaScript runtime is required to compile code for the Rails asset pipeline. For development on Ubuntu Linux it is best to install the Node.js server-side JavaScript environment.
$ sudo apt-get install nodejs
and set it in your $PATH
.
If you don’t install Node.js, you’ll need to add this to the Gemfile for each Rails application you build:
gem 'therubyracer'
RubyGems is the gem manager in Ruby.
Check the installed gem manager version. You may see:
$ gem -v 2.4.2
At the time this was written, a newer RubyGems version was available. Use gem update --system
to upgrade the Ruby gem manager:
$ gem update --system
Not all Rails developers use RVM to manage gems, but many recommend it.
Display a list of gemsets:
$ rvm gemset list gemsets for ruby-2.4.2 => (default) global
Only the “default” and “global” gemsets are pre-installed.
If you get an error “rvm is not a function,” close your console and open it again.
See what gems are installed in the “global” gemset:
$ rvm gemset use global $ gem list
A trouble-free development environment requires the newest versions of the default gems.
Several gems are installed with Ruby or the RVM default gemset:
To get a list of gems that are outdated:
$ gem outdated ### list not shown for brevity
To update all stale gems:
$ gem update ### list not shown for brevity
In particular, rake should be updated to version 10.2.1 or newer.
You can track updates to gems at the RubyGems.org site by creating an account and visiting your dashboard. Search for each gem you use and “subscribe” to see a feed of updates in the dashboard (an RSS feed is available from the dashboard). After you’ve built an application and set up a GitHub repository, you can stay informed with Gemnasium or VersionEye. These services survey your GitHub repo and send email notifications when gem versions change. Gemnasium and VersionEye are free for public repositories with a premium plan for private repositories.
By default, when you install gems, documentation files will be installed. Developers seldom use gem documentation files (they’ll browse the web instead). Installing gem documentation files takes time, so many developers like to toggle the default so no documentation is installed.
Here’s how to speed up gem installation by disabling the documentation step:
$ echo "gem: --no-document" >> ~/.gemrc
This adds the line gem: --no-document
to the hidden .gemrc file in your home directory.
The Bundler gem is an essential tool for managing gems when developing and running Rails applications. RVM used to install Bundler automatically. As of version 1.26.11 (released March 31, 2015), RVM no longer installs Bundler automatically. You must install Bundler:
$ gem install bundler
Nokogiri is a gem that is a dependency for many other gems. Nokogiri is a gem that requires compilation for your specific operating system. As such, if your system environment doesn’t match Nokogiri’s requirements, compilation of Nokogiri will fail. If your system is configured properly, you’ll be able to compile Nokogiri. However, compilation takes time. Every time you install the Nokogiri gem, you’ll wait (as long as five minutes).
To save time, install the Nokogiri gem in the RVM global gemset:
$ gem install nokogiri
During installation, Nokogiri will display two lengthy messages in the console. It will also pause without displaying any progress for as long as five minutes. Don’t assume installation has failed unless you see an error message or you’ve waited more than ten minutes.
If installation fails, make sure your system is configured properly (look for help on Stack Overflow).
Check for the current version of Rails. Rails 5.1 was current and Rails 5.2 was available in beta release when this was written.
You can install Rails directly into the global gemset. However, many developers prefer to keep the global gemset sparse and install Rails into project-specific gemsets, so each project has the appropriate version of Rails.
If you install Rails at this point, you will install it into the global gemset.
I recommend to make a gemset for the beta release:
$ rvm use ruby-2.4.2@rails5.2 --create
Here are the options you have for installing Rails.
If you want the newest beta version or release candidate, you can install with --pre
.
I recommend installing the beta release:
$ gem install rails --pre
If you want to install the current stable release:
$ gem install rails
Or you can get a specific version.
For example, if you want the Rails 3.2.18 release:
$ gem install rails --version=3.2.18 $ rails -v
You’ll need a convenient folder to store your Rails projects. You can give it any name, such as code/ or projects/. For this tutorial, we’ll call it workspace/.
Create a projects folder and move into the folder:
$ mkdir workspace $ cd workspace
This is where you’ll create your Rails applications.
Here’s how to create a project-specific gemset, installing Rails, and creating a new application.
$ mkdir myapp $ cd myapp $ rvm use ruby-2.4.2@myapp --ruby-version --create $ gem install rails $ rails new .
We’ll name the new application “myapp.” Obviously, you can give it any name you like.
With this workflow, you’ll first create a root directory for your application, then move into the new directory.
With one command you’ll create a new project-specific gemset. The option “—ruby-version” creates .ruby-version and .ruby-gemset files in the root directory. RVM recognizes these files in an application’s root directory and loads the required version of Ruby and the correct gemset whenever you enter the directory.
When we create the gemset, it will be empty (though it inherits use of all the gems in the global gemset). We immediately install Rails. The command gem install rails
installs the most recent release of Rails.
Finally we run rails new .
. We use the Unix “dot” convention to refer to the current directory. This assigns the name of the directory to the new application.
This approach is different from the way most beginners are taught to create a Rails application. Most instructions suggest using rails new myapp
to generate a new application and then enter the directory to begin work. Our approach makes it easy to create a project-specific gemset and install Rails before the application is created.
The rails new
command generates the default Rails starter app. If you wish, you can use the Rails Composer tool to generate a starter application with a choice of basic features and popular gems.
For a “smoke test” to see if everything runs, display a list of Rake tasks.
$ rake -T
There’s no need to run bundle exec rake
instead of rake
when you are using RVM (see RVM and bundler integration).
This concludes the instructions for installing Ruby and Rails. Read on for additional advice and tips.
The starter application you create with rails new
is very basic.
Use the Rails Composer tool to build a full-featured Rails starter app.
You’ll get a choice of starter applications with basic features and popular gems.
Here’s how to generate a new Rails application using the Rails Composer tool:
Using the conventional approach:
$ rails new myapp -m https://raw.github.com/RailsApps/rails-composer/master/composer.rb
Or, first creating an empty application root directory:
$ mkdir myapp $ cd myapp $ rvm use ruby-2.4.2@myapp --ruby-version --create $ gem install rails $ rails new . -m https://raw.github.com/RailsApps/rails-composer/master/composer.rb
The -m
option loads an application template that is hosted on GitHub.
You can add the -T
flags to skip Test::Unit if you are using RSpec for testing.
You can add the -O
flags to skip Active Record if you are using a NoSQL datastore such as MongoDB.
If you get an error “OpenSSL certificate verify failed” when you try to generate a new Rails app, see the article OpenSSL errors and Rails.
The RailsApps project provides example apps that show how real-world Rails applications are built. Each example is known to work and can serve as your personal “reference implementation”. Each is an open source project. Dozens of developers use the apps, report problems as they arise, and propose solutions as GitHub issues. Purchasing a subscription for the tutorials gives the project financial support.
Example Applications for Rails | Tutorial | Comments |
---|---|---|
Learn Rails | Learn Ruby on Rails | introduction to Rails for beginners |
Rails and Bootstrap | Rails Bootstrap Tutorial | starter app for Rails and Bootstrap |
Rails and Foundation | Rails Foundation Tutorial | starter app for Rails and Zurb Foundation |
Mailing List with Active Job | Mailing List with Active Job Tutorial | background processing |
OmniAuth and Rails | OmniAuth Tutorial | OmniAuth for authentication |
Devise and Rails | Devise Tutorial | Devise for authentication |
Role-Based Authorization | Role-Based Authorization Tutorial | Simple authorization |
Pundit and Rails | Rails Pundit Tutorial | Pundit for authorization |
Sign Up and Download | Sign Up and Download Tutorial | Devise with authorization to download a PDF file |
Stripe Checkout | Stripe Checkout Tutorial | selling a product with Stripe Checkout |
If you’ve already created an application with the command rails new myapp
, you can still create a project-specific gemset. Here’s how to create a gemset for an application named “myapp” and create .ruby-version and .ruby-gemset files in the application’s root directory:
$ rvm use ruby-2.4.2@myapp --ruby-version --create
You’ll need to install Rails and the gems listed in your Gemfile into the new gemset by running:
$ gem install rails $ bundle install
If you have already created both an application and a gemset, but not .ruby-version and .ruby-gemset files, here’s how to add the files. For example, if you want to use an existing gemset named “ruby-2.4.2@myapp”:
$ echo "ruby-2.4.2" > .ruby-version $ echo "myapp" > .ruby-gemset
Using .ruby-version and .ruby-gemset files means you’ll automatically be using the correct Rails and gem version when you switch to your application root directory on your local machine.
Rails uses the SQLite database by default. RVM installs SQLite and there’s nothing to configure.
Though SQLite is adequate for development (and even some production applications), a new Rails application can be configured for other databases. The command rails new myapp --database=
will show you a list of supported databases.
Supported for preconfiguration are: mysql, oracle, postgresql, sqlite3, frontbase, ibm_db, sqlserver, jdbcmysql, jdbcsqlite3, jdbcpostgresql, jdbc.
For example, to create a new Rails application to use PostgreSQL:
$ rails new myapp --database=postgresql
The --database=postgresql
parameter will add the pg database adapter gem to the Gemfile and create a suitable config/database.yml file.
Don’t use the --database=
argument with the Rails Composer tool. You’ll select a database from a menu instead.
If you wish to run your own servers, you can deploy a Rails application using Capistrano deployment scripts. However, unless system administration is a personal passion, it is much easier to deploy your application with a “platform as a service” provider such as Heroku.
For easy deployment, use a “platform as a service” provider such as:
For deployment on Heroku, see the article:
By design, Rails encourages practices that avoid common web application vulnerabilities. The Rails security team actively investigates and patches vulnerabilities. If you use the most current version of Rails, you will be protected from known vulnerabilities. See the Ruby On Rails Security Guide for an overview of potential issues and watch the Ruby on Rails Security Mailing List for announcements and discussion.
Rails uses a session store to provide persistence between page requests. The default session store uses cookies. To prevent decoding of cookie data and hijacking a session, Rails encrypts cookie data using a secret key. When you create a new Rails application using the rails new
command, a unique secret key is generated. If you’ve used the Rails Composer tool to generate the application, the application’s secret token will be unique, just as with any Rails application generated with the rails new
command.
The file config/secrets.yml contains secret tokens for development and production.
Take care to hide the secret token you use in production. Don’t expose it in a public GitHub repo, or people could change their session information, and potentially access your site without permission. It’s best to set the secret token in a Unix shell variable.
If you need to create a new secret token:
$ rake secret
The command rake secret
generates a new random secret you can use. The command won’t install the key; you have to copy the key from the console output to the appropriate file.
You can get help directly from the RVM team using the IRC (Internet Relay Chat) channel #rvm on irc.freenode.net:
http://webchat.freenode.net/?channels=rvm
If you’ve never used IRC, it’s worthwhile to figure out how to use IRC because the RVM team is helpful and friendly. IRC on freenode requires registration (see how to register).
If you get a “segfault” when you try rails new
, try removing and reinstalling RVM.
Ruby and RubyGems (starting with Ruby 1.9.3p194 and RubyGems 1.8.23) require verification of server SSL certificates when Ruby makes an Internet connection via https. If you run rails new
and get an error “Gem::RemoteFetcher::FetchError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate” see this article suggesting solutions: OpenSSL errors and Rails.
Are you getting an error “OpenSSL certificate verify failed” when you try to generate a new Rails app from an application template? See this article suggesting solutions: OpenSSL errors and Rails.
Your best source for help with problems is Stack Overflow. Your issue may have been encountered and addressed by others.
You can also try Rails Hotline, a free telephone hotline for Rails help staffed by volunteers.
Daniel Kehoe wrote the article.
Comments
Is this helpful? Your encouragement fuels the project. Please tweet or add a comment. Couldn't get something to work? For the example apps and tutorials, it's best to open an issue on GitHub so we can help you.
comments powered by Disqus