-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanaging-rails-versions-gems.html
234 lines (232 loc) · 18.7 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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Managing Rails Versions and Gems</title>
<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/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 id="main">
<div class="site">
<div id="guides">
<div class="guide">
<div class="main">
<div class="actions">
<a href="http://railsapps.github.com/"><img src="http://railsapps.github.com/images/rails-36x36.jpg" title="Home" alt="Home"></a>
<a href="http://railsapps.github.com/">Rails Apps</a>
<a href="https://github.com/RailsApps">GitHub Repository</a>
</div><!-- class="actions" -->
<h1>Managing Rails Versions and Gems</h1>
<div class="content wikistyle gollum textile">
<h1>Managing Rails Versions and Gems</h1>
<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.1</h2>
<p>If you want, go directly to the nitty-gritty details. Read the step-by-step instructions for <a class="internal present" href="/installing-rails-3-1.html">Installing Rails 3.1</a> and see an <a class="internal present" href="/rails-3-1-example-gemfile.html">Example Rails 3.1 Gemfile</a>.</p>
<p>You can also see a <a href="http://railsapps.github.com/rails-3-0-8-example-gemfile.html">Rails 3.0.8 example gemfile</a> and <a href="http://railsapps.github.com/rails-3-0-7-example-gemfile.html">Rails 3.0.7 example gemfile</a> if you are using an older version of Rails.</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="https://github.com/railsapps/rails3-devise-rspec-cucumber/wiki/Tutorial">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-mongoid-devise">Devise, Mongoid</a> </td>
<td> <a href="http://github.com/railsapps/rails3-mongoid-devise/wiki/Tutorial">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="https://github.com/railsapps/rails3-mongoid-omniauth/wiki/Tutorial">Tutorial</a> </td>
<td> <a href="https://github.com/RailsApps/rails3-application-templates">App Template</a> </td>
<td> Combines OmniAuth with 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.2.</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.2 patch level 290 was current when this was written.</p>
<p>See instructions for <a class="internal present" href="/installing-rails-3-1.html">Installing Rails 3.1</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.10 or newer (version 1.8.10 resolves a security vulnerability).</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.10 but if you install a gem and find it doesn’t work, try a 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.1.</p>
<p>Each <a href="http://railsapps.github.com/">Rails Apps example</a> repository contains branches for earlier versions of Rails if you cannot use Rails 3.1.</p>
<p>See instructions for <a class="internal present" href="/installing-rails-3-1.html">Installing Rails 3.1</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.1, use rake 0.9.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.1.</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.1:</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 class="internal present" href="/installing-rails-3-1.html">Installing Rails 3.1</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 “ruby192@rails31”:</p>
<pre>
$ echo "rvm ruby192@rails31" > .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>
</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
// The following are highly recommended additional parameters.
// var disqus_identifier = 'managing-rails-versions-gems.html';
// var disqus_url = 'http://railsapps.github.com/managing-rails-versions-gems.html';
/* * * 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" -->
<br />
<br />
<div class="credits">
<div class="content wikistyle gollum">
<h2>Credits</h2>
</div>
<p>Daniel Kehoe (<a href="http://danielkehoe.com/">http://danielkehoe.com/</a>) initiated the project and wrote some examples and tutorials.</p>
<p>Your contributions are welcome!</p>
</div><!-- class="credits" -->
<div class="admin">
<small>Corrections? Additions? You can edit this page <a href="https://github.com/railsapps/railsapps.github.com/wiki/_pages">on the wiki</a>.</small>
<div style="float: right;">
<small>Last edited by <b>Daniel Kehoe</b>, 2011-09-01 12:24:10</small>
</div>
</div><!-- class="admin" -->
</div><!-- class="main" -->
</div><!-- class="guide" -->
</div><!-- id="guides" -->
</div><!-- class="site" -->
</div><!-- id="main" -->
<!-- MixPanel analytics -->
<script type='text/javascript'> var mp_protocol = (('https:' == document.location.protocol) ? 'https://' : 'http://'); document.write(unescape('%3Cscript src="' + mp_protocol + 'api.mixpanel.com/site_media/js/api/mixpanel.js" type="text/javascript"%3E%3C/script%3E')); </script> <script type='text/javascript'> try { var mpmetrics = new MixpanelLib('b1cf97418f8c5b129847e8a52edbae68'); } catch(err) { null_fn = function () {}; var mpmetrics = { track: null_fn, track_funnel: null_fn, register: null_fn, register_once: null_fn, register_funnel: null_fn }; } </script>
<!-- Clicky analytics -->
<script src="//static.getclicky.com/js" type="text/javascript"></script>
<script type="text/javascript">try{ clicky.init(66423523); }catch(e){}</script>
<noscript><p><img alt="Clicky" width="1" height="1" src="//in.getclicky.com/66423523ns.gif" /></p></noscript>
</body>
</html>