-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanaging-rails-versions-gems.html
248 lines (238 loc) · 19.3 KB
/
managing-rails-versions-gems.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Managing Rails Versions and Gems</title>
<link href="https://plus.google.com/u/0/b/117374718581973393536/117374718581973393536/posts/" rel="publisher" />
<link rel="stylesheet" href="/css/bootstrap.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="/css/screen.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="/css/gollum.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="/css/site.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="/css/syntax.css" type="text/css" charset="utf-8" />
<script src="http://code.jquery.com/jquery-1.6.min.js" type="text/javascript"></script>
<script src="/javascript/jquery.text_selection-1.0.0.min.js" type="text/javascript"></script>
<script src="/javascript/jquery.previewable_comment_form.js" type="text/javascript"></script>
<script src="/javascript/jquery.tabs.js" type="text/javascript"></script>
<script src="/javascript/gollum.js" type="text/javascript"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-5109366-14']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a href="/" class="brand">RailsApps Project</a>
<ul class="pull-right nav">
<li><a href="http://blog.railsapps.org/" class="twitter">Blog</a></li>
<li><a href="http://twitter.com/rails_apps" class="twitter">Twitter</a></li>
<li><a href="https://plus.google.com/117374718581973393536" class="google">Google +</a></li>
<li><a href="https://github.com/RailsApps" class="github">GitHub Repository</a></li>
</ul>
</div>
</div>
</div>
<div class="container">
<div class="page-header">
<h1>Managing Rails Versions and Gems</h1>
</div>
<div class="content wikistyle gollum textile">
<h1>Managing Rails Versions and Gems</h1>
<h4>by Daniel Kehoe</h4>
<p><em>Last updated 1 February 2012</em></p>
<p>Learn tips and tricks for managing Rails and Ruby gems. Relieve your headaches when versions collide.</p>
<p>This is a guide for developers using the example apps from the <a href="http://railsapps.github.com/">Rails Apps</a> repository. Others may find it helpful as well.</p>
<p>Before you install one of the <a href="https://github.com/RailsApps">Rails example apps</a>, there are a few things you should know about managing the pieces of the Rails platform. Often, everything “just works,” but when it doesn’t, here’s what you need to review. Read on if you’d like to know how all the moving parts work together.</p>
<h2>Install Rails 3.2</h2>
<p>See <a href="http://railsapps.github.com/installing-rails.html">Installing Rails 3.2</a> for detailed instructions and advice and see an <a href="http://railsapps.github.com/rails-3-2-example-gemfile.html">Example Rails 3.2 Gemfile</a>.</p>
<h2>Background</h2>
<p>The Ruby on Rails platform is a living ecosystem: a community of developers, their projects, and complex dependencies among their projects. At times (often before and after the release of a major new version of Rails) the ecosystem enters a state of disequilibrium and important pieces no longer work together easily. These times of disequilibrium are valuable for the ecosystem because, as a whole, developers innovate and Rails improves during these times.</p>
<p>For you as a developer, it may feel like a trainwreck. You productivity will stall as you try to sort out what is broken and how to fix it. But you’ll find help here.</p>
<h4>Example Apps</h4>
<p>The example apps from the <a href="http://railsapps.github.com/">Rails Apps</a> repository help you keep current with changing Rails and gem versions.</p>
<p>Each example provides a set of useful, popular Rails gems integrated into a working application. Each example is known to work and can serve as your personal “reference implementation”. Each is an open source project. Many developers use the apps, report problems as they arise, and propose solutions as GitHub issues.</p>
<p>These examples provide a working <strong>Rails app</strong> with a <strong>detailed tutorial</strong> plus an <strong>application template</strong> that can be used to create a <strong>starter app</strong>.</p>
<table>
<tr>
<th>Author </th>
<th>Example App </th>
<th>Tutorial </th>
<th>App Template </th>
<th>Comments </th>
</tr>
<tr>
<td> Daniel Kehoe </td>
<td> <a href="https://github.com/RailsApps/rails3-devise-rspec-cucumber">Devise, RSpec, Cucumber</a> </td>
<td> <a href="http://railsapps.github.com/tutorial-rails-devise-rspec-cucumber.html">Tutorial</a> </td>
<td> <a href="https://github.com/RailsApps/rails3-application-templates">App Template</a> </td>
<td> Uses ActiveRecord and SQLite </td>
</tr>
<tr>
<td> Daniel Kehoe </td>
<td> <a href="https://github.com/RailsApps/rails3-bootstrap-devise-cancan">Twitter Bootstrap, Devise, CanCan</a> </td>
<td> <a href="http://railsapps.github.com/tutorial-rails-bootstrap-devise-cancan.html">Tutorial</a> </td>
<td> <a href="https://github.com/RailsApps/rails3-application-templates">App Template</a> </td>
<td> Devise for authentication, CanCan for authorization, Twitter Bootstrap for <span class="caps">CSS</span> </td>
</tr>
<tr>
<td> Daniel Kehoe </td>
<td> <a href="https://github.com/RailsApps/rails-prelaunch-signup">Startup Prelaunch Signup</a> </td>
<td> <a href="http://railsapps.github.com/tutorial-rails-prelaunch-signup.html">Tutorial</a> </td>
<td> <a href="https://github.com/RailsApps/rails3-application-templates">App Template</a> </td>
<td> For a startup prelaunch signup site </td>
</tr>
<tr>
<td> Daniel Kehoe </td>
<td> <a href="https://github.com/RailsApps/rails3-mongoid-devise">Devise, Mongoid</a> </td>
<td> <a href="http://railsapps.github.com/tutorial-rails-mongoid-devise.html">Tutorial</a> </td>
<td> <a href="https://github.com/RailsApps/rails3-application-templates">App Template</a> </td>
<td> Combines Devise with MongoDB </td>
</tr>
<tr>
<td> Daniel Kehoe </td>
<td> <a href="https://github.com/RailsApps/rails3-mongoid-omniauth">OmniAuth, Mongoid</a> </td>
<td> <a href="http://railsapps.github.com/tutorial-rails-mongoid-omniauth.html">Tutorial</a> </td>
<td> <a href="https://github.com/RailsApps/rails3-application-templates">App Template</a> </td>
<td> OmniAuth for authentication with a MongoDB datastore </td>
</tr>
<tr>
<td> Daniel Kehoe </td>
<td> <a href="https://github.com/RailsApps/rails3-subdomains">Subdomains, Devise, Mongoid</a> </td>
<td> <a href="http://railsapps.github.com/tutorial-rails-subdomains.html">Tutorial</a> </td>
<td> <a href="https://github.com/RailsApps/rails3-application-templates">App Template</a> </td>
<td> Basecamp-style subdomains with Devise and MongoDB </td>
</tr>
</table><h2>Ruby</h2>
<p>Ruby is the programming language used for Rails projects.</p>
<p>What version of Ruby is installed on your machine? Check with:</p>
<p><code>$ ruby -v</code></p>
<p>For the example apps it should be version 1.9.3.</p>
<p>You can check for the current <a href="http://www.ruby-lang.org/en/downloads/">recommended version of Ruby</a>.</p>
<p>Ruby 1.9.3 patch level 0 was current when this was written.</p>
<p>See instructions for <a href="http://railsapps.github.com/installing-rails.html">Installing Rails 3.2</a> to install a newer version of Ruby if necessary.</p>
<h2>RubyGems</h2>
<p>RubyGems is a package management framework for Ruby. A gem is a packaged Ruby application or library. You can <a href="http://docs.rubygems.org/shelf/index">read more about gems</a> if you wish. Gems come from a wonderful pool of open source love but can be a headache to track and integrate.</p>
<p>The RubyGems package management system comes as part of a standard Ruby language installation.</p>
<p>What version of the RubyGems system is installed on your machine? Check with:</p>
<p><code>$ gem -v</code></p>
<p>For the example apps it should be version 1.8.17 or newer.</p>
<p>The RubyGems package management system is part of a standard Ruby language installation. It can be updated by installing the rubygems-update gem.</p>
<p>You can check for the current <a href="https://rubygems.org/gems/rubygems-update">rubygems-update gem</a>.</p>
<p>Use <code>gem update --system</code> to upgrade the RubyGems system.</p>
<p>As of release 1.8.0 (4 May 2011), the RubyGems system gem began a series of rapid updates (with <a href="http://blog.majesticseacreature.com/mending-the-rubygems-fences">controversy</a> and a promised <a href="http://blog.majesticseacreature.com/establishing-release-management-policies-for">resolution</a>). The example apps all work with version 1.8.15 but if you install a gem and find it doesn’t work, roll back to an older version of RubyGems:</p>
<p><code>gem update --system 1.7.2</code></p>
<p>or even:</p>
<p><code>gem update --system 1.3.7</code></p>
<h2>Rails</h2>
<p>Rails itself is a gem, which means it can be installed and updated like any other gem.</p>
<p>What version of Rails is installed on your machine? Check with:</p>
<p><code>$ rails -v</code></p>
<p>You can check for the newest released <a href="https://rubygems.org/gems/rails">Rails version</a>.</p>
<p>For the example apps it should be version 3.2.</p>
<p>See instructions for <a href="http://railsapps.github.com/installing-rails.html">Installing Rails 3.2</a> to install or update Rails. I recommend that you create a new gemset using <a href="http://rvm.beginrescueend.com/">rvm</a> before installing a new version of Rails (rather than using <code>gem update rails</code>). With rvm, you can switch gemsets to run your application under an earlier version of Rails.</p>
<h2>Rake</h2>
<p><a href="http://lukaszwrobel.pl/blog/rake-tutorial">Rake</a> is a software task management or “build” tool used extensively by Rails and many gems.</p>
<p>What version of rake is installed on your machine? Check with:</p>
<p><code>$ rake --version</code></p>
<p>You can check for the newest released <a href="https://rubygems.org/gems/rake">rake gem</a>.</p>
<p>For apps built with Rails 3.2, use rake 0.9.2.2 or newer.</p>
<p>If you are using an older version of Rake, you should update Rake to 0.9.2 or newer immediately after creating a new gemset and before installing Rails 3.2.</p>
<p>Here’s how to update Rake:</p>
<pre>
$ gem update rake
$ rake --version
</pre>
<p>If you see this error message when installing one of the <a href="http://railsapps.github.com/">Rails Apps examples</a>, you should create a new gemset, update Rake, and create a fresh install of Rails 3.2:</p>
<pre>
The template ... could not be loaded.
Error: You have already activated rake 0.8.7, but your Gemfile requires rake 0.9.x.
Consider using bundle exec.
</pre>
<p>See an explanation by David Chelimsky in his blog post <a href="http://blog.davidchelimsky.net/2011/05/28/rake-09-and-gem-version-constraints/">Rake 0.9 and Gem Version Constraints</a> about collisions among versions of Rake and gems.</p>
<h2>Bundler and the Gemfile</h2>
<p>The <a href="http://gembundler.com/">Ruby Bundler</a> manages an application’s gems. Bundler works by reading a <strong>Gemfile</strong> in an application’s root directory, downloading and unpacking specified versions of each required gem, resolving any dependencies among gems, and creating a <strong>Gemfile.lock</strong> file to force use of the specified gem versions.</p>
<p>Each of the example apps is provided with a gemfile that specifies a known working set of required gems.</p>
<p>After creating an example app, run <code>bundle install</code> to install all the necessary gems.</p>
<p>If you add a new gem to the gemfile, run <code>bundle install</code> again.</p>
<p>If you want to force an upgrade to all the gems that have newer versions, run <code>bundle update</code>. When you update a gem, bundler will not update a dependency of that gem if another gem still depends on it.</p>
<h4>Gem Version Constraint</h4>
<p>You will specify the gems you need in your application’s Gemfile. If you don’t specify a version, your application will use the newest gem version installed on your machine.</p>
<p>If you run <code>bundle update</code>, each gem will be updated to the newest available. You may benefit from the features (or the bug fixes) of the newest version. Or you may discover integration issues with other gems which cause failures. To avoid surprises and manage gem updates cautiously, you can specify gem versions in your Gemfile (<a href="http://docs.rubygems.org/read/chapter/16">described in the RubyGems manual</a>).</p>
<h4>Optimistic Version Constraint</h4>
<p>In the gemfiles for the example apps, you’ll often see “optimistic version constraint” specified with the <code>>=</code> operator like this:</p>
<p><code>gem 'devise', '>= 1.3.4'</code></p>
<p>This means, “use any version of the Devise gem that is newer than version 1.3.4.” It will force an upgrade of the Devise gem to version 1.3.4 the first time you run <code>bundle update</code> and check and install newer versions each subsequent time you run <code>bundle update</code>. That’s a good scheme for keeping your gems up-to-date unless there is a known issue with a gem.</p>
<h4>Pessimistic Version Constraint</h4>
<p>In the some gemfiles you may see “pessimistic version constraint” like this:</p>
<p><code>gem 'library', '~> 2.2'</code></p>
<p>which is shorthand for</p>
<p><code>gem 'library', '>= 2.2.0', '< 3.0'</code></p>
<p>which means use any gem version newer than 2.2.0 but don’t install 3.0 or anything newer.</p>
<p>We seldom specify pessimistic version constraint for gems in the example apps unless a gem developer recommends it.</p>
<p>See recommendations about using pessimistic version constraint in a blog post by Dan Croak titled <a href="http://robots.thoughtbot.com/post/2508037841/twiddle-wakka">Twiddle Wakka</a> (yes, the <code>~></code> operator is variously known as the “twiddle wakka,” “tildemator,” or “spermy operator.”)</p>
<h4>Absolute Version Constraint</h4>
<p>In the example apps, we use “absolute version constraint” to specify a known working version of a gem if we’ve seen reported problems with newer versions. Absolute version constraint looks like this:</p>
<p><code>gem 'library', '2.2'</code></p>
<h2>Using rvm to Manage Multiple Versions of Rails</h2>
<p>I recommend installing rvm, the <a href="http://rvm.beginrescueend.com/">Ruby Version Manager</a>. It’s essential if you must use multiple versions of Rails. And rvm makes it easy to switch sets of gems if you suspect a problem with incompatibilities among gems.</p>
<h4>Install <span class="caps">RVM</span>
</h4>
<p>The rvm website explains <a href="https://rvm.beginrescueend.com/rvm/install/">how to install rvm</a>.</p>
<h4>Create a Gemset for a Project</h4>
<p>See instructions for <a href="http://railsapps.github.com/installing-rails.html">Installing Rails 3.2</a> to install Rails and create a new gemset using rvm.</p>
<h4>Using the .rvmrc file for Project-Specific Gemsets</h4>
<p>After you’ve created an rvm gemset that you’ll use for a project, you can create an <strong>.rvmrc</strong> file in the application’s root directory. <span class="caps">RVM</span> recgonizes an <strong>.rvmrc</strong> file in a directory and loads the gemset specified inside.</p>
<p>Here’s how to create an <strong>.rvmrc</strong> file if you’re using a gemset named “ruby-1.9.3-p0@rails32”:</p>
<pre>
$ echo "rvm ruby-1.9.3-p0@rails32" > .rvmrc
</pre>
<p>Using an <strong>.rvmrc</strong> file means you’ll automatically be using the correct Rails and gem version when you run your application on your local machine. This works best if you create an rvm gemset specifically for your application.</p>
<h2>Using Pik to Manage Multiple Versions of Rails on Windows</h2>
<p>For Windows users juggling different versions of Ruby, <a href="https://github.com/vertiginous/pik">pik</a> is a tool similar to <span class="caps">RVM</span>. The pik set of commands for managing multiple rubies is similar to that of <span class="caps">RVM</span>.</p>
<h4>Install Pik</h4>
<p>The readme section on the <a href="https://github.com/vertiginous/pik">repository home page</a> gives the requirements for running pik, the most notable of which is that it requires Mat’s Ruby Interpreter version 1.8.7-p330. Make sure to install this version as your “default” Ruby version, which can be done most easily using <a href="http://rubyinstaller.org/downloads/archives">RubyInstaller</a>. In addition to Mat’s Ruby Interpreter, RubyInstaller also includes <a href="https://github.com/oneclick/rubyinstaller/wiki/Development-Kit">DevKit</a> and other useful tools for Ruby programmers using Windows.</p>
<p>Pik can be installed with Windows Installer or by simply typing: </p><pre>C:\gem install pik</pre> once RubyInstaller 1.8.7-p330 has been installed and configured.
</div><!-- class="content" -->
<div class="comments">
<div class="content wikistyle gollum">
<h2>Comments</h2>
</div>
<p>Is this helpful? Please "like" below. Question or suggestion? Please add a comment below. Got a correction or addition? You can edit this page <a href="https://github.com/RailsApps/railsapps.github.com/wiki/_pages">on the wiki</a> or create a <a href="https://github.com/RailsApps/railsapps.github.com/issues">GitHub issue</a> to alert me.</p>
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'railsapps'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</div><!-- class="comments" -->
<div class="footer row">
<div class="span4">
<h3>Credits</h3>
<p><a href="http://danielkehoe.com/">Daniel Kehoe</a> initiated the <a href="http://railsapps.github.com/">RailsApps Project</a>. Thanks to <a href="http://tigrish.com/">Christopher Dell</a> for design contributions.</p>
</div>
<div class="span4">
<h3>Contributions</h3>
<p>Corrections? Additions? You can edit this page <a href="https://github.com/RailsApps/railsapps.github.com/wiki/_pages">on the wiki</a>.</p>
</div>
<div class="span4">
<h3>Last edit</h3>
<p>by <b>Daniel Kehoe</b>, 2012-05-19 20:21:06</p>
</div>
</div>
</div>
</body>
</html>