Rollback gems according to version in Gemfile.lock - ruby-on-rails

I need to revert back to an older set of gems in my dev environment. I've replaced my Gemfile.lock file with the old versions I need.
I thought I could just replace the Gemfile.lock and bundle install, but that doesn't seem to work. When I do that, then check the gem version numbers, I get the same versions I had before.
One of the gems I need to downgrade is the rake gem. The version I need is 11.1.2 but currently when I do gem list I get rake (12.0.0, 11.1.2, 10.4.2)
How can I revert rake and all the other gems back to their previous versions?

If you would like to clean out old gems no longer in your Gemfile.lock, you can run bundle clean.
However, even though the gems are still there, they are not being used if you run a task with bundle exec.

This is what I needed.. gem uninstall <gem name> and then if there are multiple gems you can pick which version you want to uninstall!

Related

I ran 'gem update' by accident can i revert it?

I ran 'gem update' to try to fix a problem in a different app i was working on. Now my main app doesn't work properly because i have a bunch of too-new gems. Is there a way to undo the gem update? Or revert my gems to the versions mentioned in my team's Gemfile.lock file?
Update: It specifically complains about Rake when i try to run 'rake spec'
(Gem::LoadError: You have already activated rake 10.5.0, but your Gemfile requires rake 10.4.2. Prepending bundle exec to your command may solve this.)
If you currently have a more recent version of a gem than is specified in your Gemfile.lock, then it should suffice to rewrite the Gemfile with the versions mentioned in Gemfile.lock and run a bundle install.
I tested this locally (downgraded a version in both Gemfile.lock and Gemfile) and bundler installed the old version without problems.
Run 'gem uninstall rake' if you have multiple versions it will ask you which you wish to remove.
Then run 'gem install rake -v 10.4.2' to install the right version.
Worked for me and much less risky than bundle updating everything.

Why does bundler update gems when it knows the gemfile requires a specific version

This seems to happen a lot. I run bundle update or bundle install and for one reason or another I often get something like this:
You have already activated kgio 2.8.0, but your Gemfile requires kgio
2.7.4. Using bundle exec may solve this
I then have to go and run: sudo gem uninstall kgio and select kgio 2.8.0 to uninstall it.
Why does bundler even update the gem if it knows my gemfile locks those gems to a specific version. I NEVER install gems outside of the gemfile and bundler so Im not circumventing its conventions. I do have another project on my machine, but I havent ran a bunle update on that project in a long time -- is there some mix up there? Has this happened to anyone else? Am I doing something wrong?
actually bundle update the gems specified (all gems, if none are specified), ignoring the previously installed gems specified in the Gemfile.lock.
whereas bundle install will fetch all remote sources, but use the dependencies specified in the Gemfile.lock instead of resolving dependencies.
and use
gem cleanup
This command will remove (uninstall) all the versions of a gem, except for the latest one.
bundle update, installs newer versions of your gems and states that in your Gemfile.lock. bundle install just makes sure you have the correct versions installed. I suppose you are running bundle install in projects with different locked versions for kgio.

Updating Gemfile from Gemfile.lock, to freeze gems

After generating a Gemfile.lock from Gemfile, is there any tool to round-trip back to freeze versions in the Gemfile. I'd rather avoid doing it manually.
Background: It's good practice to freeze minor version on gems, e.g. if Gemfile.lock is 2.4.1, I'd want to update Gemfile to include ~> 2.4 (as explained here).
You should define the gem verions that you wanna freeze your app to in the GemFile itself before you run bundle install.
Once, you have that define and you run bundle install, it will generate a Gemfile.lock with the versions of all installed gems and their dependancies.
again, .lock file is auto-generated ans should not be manually editted. this is what the GemFile is for.

How do you install gems from Gemfile.lock file?

I am trying to run an app taken off Github.
I have run bundle install to install required gems from the Gemfile. However when running the app, an error message tells me the gems installed are the wrong version.
On inspecting the Gemfile.lock I note that the versions are older than the gems installed. (i.e. I have newer versions of gems installed and the application requires older gems.)
Is there a quick way to install all the gems as per the versions described in the Gemfile.lock file? Alternatively is there a method to ignore that file?
Gemfile:
source 'http://rubygems.org'
gem 'rails', "3.0.9"
gem "sass"
..
Gemfile.lock:
sass (3.1.1)
..
In the above example, even though sass is installed the app specially requires version 3.1.1.
With a valid Gemfile.lock file, bundle install alone should be sufficient, unless some particular gem version has been yanked. In that case you would need to look for an alternative gem version that is still currently available (usually bundle update name_of_yanked_gem would suffice).
About the sass 3.1.1, it is not so much that the application requires that particular version, but rather, that was likely the newest version available when the Gemfile.lock was last generated/updated given the overall version constraints as specified in Gemfile. As you have listed, there is no version range specified for sass itself, but other gems may impose further constraints if they have sass as a dependency.
Outright ignoring Gemfile.lock is not a good idea as under normal circumstances it will be specifying the gem versions that were last known to be still usable with the application.
try this ..
bundle install --deployment
With above deployment option, bundle then reads from Gemfile.lock.
What's more, the gems are installed to directory vendor/bundle, with the bundle directory being auto created.
Also, new directory .bundle is created directly under the rails root directory, and has a file named config, whose content is as follows ...
BUNDLE_FROZEN: '1'
BUNDLE_PATH: vendor/bundle
BUNDLE_DISABLE_SHARED_GEMS: '1'
Hope the above works for you.
Make sure you're running the web server with bundle execute rails server

What is the difference between Gemfile and Gemfile.lock in Ruby on Rails

I am a beginner to Ruby on Rails and I am using Rails 3.0.9.
What is the difference between Gemfile and Gemfile.lock in Rails?
The Gemfile is where you specify which gems you want to use, and lets you specify which versions.
The Gemfile.lock file is where Bundler records the exact versions that were installed. This way, when the same library/project is loaded on another machine, running bundle install will look at the Gemfile.lock and install the exact same versions, rather than just using the Gemfile and installing the most recent versions. (Running different versions on different machines could lead to broken tests, etc.) You shouldn't ever have to directly edit the lock file.
Check out Bundler's Purpose and Rationale, specifically the Checking Your Code into Version Control section.
Usually we write dependencies in Gemfile as:
gem "nokogiri", "~> 1.4.4"
gem 'bcrypt-ruby', '~> 3.0.0'
gem 'uglifier', '>= 1.2.3'
..
Here you basically say: "I want nokogiri as long as it’s greater than version 1.4.4", etc. Now suppose that I have set up my Gemfile 8 months ago and I successful setup my app with this requirement. 8 months ago nokogiri version was 1.4.4. My rails apps was running perfectly without any problems with this version.
Now think I'm trying to build with the same Gemfile. But if we look at nokogiri versions we see that the current stable version has changed to 1.4.9. That means if we try to build, bundler will install version 1.4.9 of nokogiri (suppose we don't have Gemfile.lock).
What does it mean ?
As you see if you don't have any Gemfile.lock and run:
bundle install
then the currently used gems can be different at any time. Your app used the version 1.4.4 and it works 8 months ago without any problems, but if you try to build it now you get the version 1.4.9. Maybe it's broken with the latest version of nokogiri, the awesome feature you used with 1.4.4 is not more available, etc..
To prevent this kind of problem Gemfile.lock is used. In Gemfile.lock only the exact versions are written and thus only these will be installed. That means if you distribute your app with a Gemfile.lock, every machine will have the same gems installed and most important they all get the same version. This will give you a stable and common deployment stack.
How is Gemfile.lock created?
It is automatically created with the first:
bundle install
command. After that everytime you run bundle install, bundle will first look up Gemfile.lock and install the gems specified there. It's an habit to distribute this file among your projects to provide consistently and stability.
How to update Gemfile.lock?
If you're happy with the the latest version of your apps than you can update Gemfile.lock. Just reflect your changes to Gemfile. That means change the dependencies to the new exact versions in Gemfile. After that run:
bundle install
This will update you Gemfile.lock with your newest version of apps.
The Gemfile.lock
When you run bundle install, Bundler will persist the full names and versions of all gems that you used (including dependencies of the gems specified in the Gemfile(5)) into a file called Gemfile.lock.
Bundler uses this file in all subsequent calls to bundle install, which guarantees that you always use the same exact code, even as your application moves across machines.
Because of the way dependency resolution works, even a seemingly small change (for instance, an update to a point-release of a dependency of a gem in your Gemfile(5)) can result in radically different gems being needed to satisfy all dependencies.
As a result, you SHOULD check your Gemfile.lock into version control. If you do not, every machine that checks out your repository (including your production server) will resolve all dependencies again, which will result in different versions of third-party code being used if any of the gems in the Gemfile(5) or any of their dependencies have been updated.

Resources