How do I remove a gem with all its dependencies? [duplicate] - ruby-on-rails

I have installed a gem on my Rails application (devise). After I installed the gem, I realized that I don't need it.
I want to remove the gem, its dependencies and the files it created on my application. In other words, I want to restore the system to what it used to be before the gem. How can I do this? (I'm using Ruby on Rails 3.)

You can use
gem uninstall <gem-name>

If you're using Rails 3+, remove the gem from the Gemfile and run bundle install.
If you're using Rails 2, hopefully you've put the declaration in config/environment.rb. If so, removing it from there and running rake gems:install should do the trick.

Devise uses some generators to generate views and stuff it needs into your application. If you have run this generator, you can easily undo it with
rails destroy <name_of_generator>
The uninstallation of the gem works as described in the other posts.

For Rails 4 - remove the gem name from Gemfile and then run bundle install in your terminal. Also restart the server afterwards.

How about something like:
gem dependency devise --pipe | cut -d \ -f 1 | xargs gem uninstall -a
(this assumes that you're not using bundler - but I guess you're not since removing from your bundle gemspec would solve the problem)

You are using some sort of revision control, right? Then it should be quite simple to restore to the commit before you added the gem, or revert the one where you added it if you have several revisions after that you wish to keep.

Related

Uninstall all installed gems in a Rails project

First, I did not use rvm or rbenv. I want to uninstall every gem in my Rails project and then bundle from the beginning. Gem installation directory is pointing to my global directory so I can't just delete that one.
In modern Javascript projects, you can simply rm -rf node_modules and npm/yarn install instantly, is there a similar way in Rails?
This is basically a duplicate question
This answer is probably best in your case:
gem list --no-versions | xargs gem uninstall -a
Well, rvm or rbenv are useful, consider using one of them.
You're using bundle, so if you want to make sure your command line only use the gems indicated in your Gemfile, you can just add "bundle exec your_command", like bundle exec rails s. So, it might be unnecessary to delete all the gems.
If you want to save place, my best advice would be to empty your global directory, use rbenv or rvm :)

Using specified version of rails when starting?

I have multiple version of rails installed. But when I start the server with "rails s" it always uses the latest one. Is there a way to specify the version of rails (and all dependent gems) that should be used? (And how would I specify that in Pasenger?)
Yes, I could use RVM or similar - but this I would like to avoid.
Thanks in advance!
Phusion Passenger author here.
The version of Rails that is loaded does not depend on 'rails server', and also does not depend on Passenger. RVM also has got nothing to do with it.
It only depends on your Gemfile.lock, which locks down all gem versions in your project. If you want a different version of Rails, you need to modify your Gemfile to specify that exact version, and run bundle install to update Gemfile.lock.
You can't do that with rails server command. Every project depend from a GemFile. But you can create a bash script that will move a custom specify GemFile to the default GemFile then start the server.
vi /script/rails4_to_rails3
#/bin/bash
mv /opt/web/my_app/GemFile /opt/web/my_app/GemFile.rails4
cp /script/GemFile.rails3 /opt/web/my_app/GemFile
bundle install
rails s
I'm not a Passenger Expert but in my opinion it's impossible to.

Why won't Rails 4 uninstall?

After uninstalling Rails 4(RC1) I still get Rails 4 apps generated with rails new.
➲ rails -v
Rails 4.0.0.rc1
➲ which rails
/Users/brandon/.rvm/gems/ruby-1.9.3-p392/bin/rails
➲ gem uninstall rails
Select gem to uninstall:
1. rails-3.2.13
2. rails-3.2.3
3. All versions
>
What's the cleanest way to fix this?
It is quite easy.
gem uninstall rails -v=4.0.0.rc1
gem uninstall railties
gem install rails -v 3.2.13
gem update --system
rails -v
By using commands above I was able to install older version of rails as needed :)
Rails does not come as an all-in-one package. You have a base Rails gem, plus it's many dependencies:
Action Mailer
Action Pack
Active Record
Active Resource
Active Support
Bundler
Railties <---- (contains generators)
Sprockets adapter for Rails
To get rid of your Rails 4 installation as a whole, you must remove all of these gems.
The easiest way to do this is to delete your entire gem folder, then reinstall whatever you need.
Try specifying your installed version:
gem uninstall rails -v=4.0.0.rc1
EDIT:
If you've already uninstalled (which you have), the following should work:
gem update --system
rails _3.2.2_ new app_name # or whatever version you're on
Fortunately, this worked as a simple fix for me:
Please note that in order to shift back to 3.2.13 (or whatever version you'd like to go back to), you must remove Railties as well as Rails.
Just do:
gem uninstall rails
Then, select the version of Rails 4 you have and delete it.
Then, do:
gem uninstall railties
And do the same thing.
When I uninstalled the Rails 4 version of railties, it told me that dependencies for a couple gems (coffee-rails and sass-rails) wouldn't be met. So I just did the same thing with both of them as I did above (such as, gem uninstall sass-rails), and deleted their Rails 4 versions as well. For example, for sass-rails, I had a version installed called sass-rails-4.0.0.rc1, so I uninstalled that version).
And that's it; the terminal will list 3.2.13 as your current Rails version, and new apps will be generated from this version as well.
TL;DR:
The simplest and safest solution to the immediate problem is
gem uninstall railties
Slightly Longer & More Complete Approach
If you want to uninstall everything that gem install rails installed, you can get a list of commands to run with this:
gem dependency rails --pipe | ruby -ne 'puts $_.gsub(/\([0-9\. <>=~,]*\)/,"")' | ruby -ne 'puts "gem uninstall #{$_}"'
Copy those and run them one-by-one, and for each one you'll be told what else depends on it, and asked if you want to go ahead with uninstallation. If you see anything in the list that is not part of rails (say you've installed something else that needs that version of active_record) then leave it, otherwise go ahead and uninstall.
The longer explanation
The version displayed is taken from the version of the railties gem, which is not uninstalled by uninstalling the rails gem.
If you open the rails executable with
vim `which rails`
(or the equivalent with the editor of your choice) you'll see the code at the bottom that decides which version of rails to use based on the version of railties:
#!/usr/bin/env ruby_noexec_wrapper
#
# This file was generated by RubyGems.
#
# The application 'railties' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'rubygems'
version = ">= 0"
if ARGV.first
str = ARGV.first
str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
if str =~ /\A_(.*)_\z/
version = $1
ARGV.shift
end
end
gem 'railties', version
load Gem.bin_path('railties', 'rails', version)
The simplest solution, therefore, is just to gem install railsties. There is no solution built-into RubyGems (that I can find) that will detect which other gems were installed with rails and are no longer used by anything else and uninstall them. RubyGems does not have the idea of an exclusive dependency, so even though nothing else besides rails uses railties, you're still stuck having to know that it (and several other things) are left over and must be manually uninstalled. This is not ideal, but it's what we've got right now, and it's not that bad, especially if you use the solution above to find and remove all the rails dependencies.

Calling 'rails new demo' created a folder named "new"?

I installed Ruby and Rails using apt-get in Ubuntu. Then when I test my installation, this thing happen.
When I call rails server inside a rails-created-folder, rails created a new folder called "server" for me, with a correct folders structure, including controller folder, app folder, gemlock file, etc.
How could it possibly happen? I will try reinstalling RoR, but has anyone encountered this?
Last time, I used RVM, but whenever I create a new app, I will have to waith for rails to redownload all the bundle file, but in this installation, I don't have to. Can you help me explaining it?
Thank you and best regards
As pointed out in the comments, it sounds like your rails executable is rails 2.
Try gem uninstall rails, select all versions.
Run rails -v. If this command works you've got a system version of rails that isn't being handled by RVM. BTW, this is why many rails devs are shifting from RVM toward rbenv + bundler.
If you still have rails after gem uninstall, run sudo gem uninstall rails. On RVM, sudo gets to your system gems. You might want to sudo gem uninstall everything, so you don't have this conflict in the future.
gem install rails, you should get version 3.2.8.
Try rails new my_app again. It should work. If this doesn't work, try the following:
Create a parent directory for your rails projects, like ~/rails. Then create a GEMFILE that looks like this:
source :rubygems
gem 'rails', '~>3.2.8'
Then inside ~/rails run bundle exec rails new app_name.
If that doesn't work... you've got a bigger system config problem of some kind I guess.

How to use the Rails 2.3 app generator when I have Rails 3 installed?

to clarify: there's only one rails command, which gets installed from the latest Rails gem, which is Rails 3 ATM. However, I'm required to create a Rails 2.3 app.
Running ruby /usr/local/lib/ruby/gems/1.8/gems/rails-2.3.8/bin/rails fails with a NoMethodError, I suppose because it also tries to use gems from the 3.0.0 release.
Uninstalling the gem produces some strange results:
$ gem uninstall rails-3.0.0
ERROR: While executing gem ... (Gem::InstallError)
cannot uninstall, check `gem list -d rails-3.0.0`
$ gem list -d rails-3.0.0
*** LOCAL GEMS ***
(and no gems here)
What should I do?
The easiest way to do it was:
Create the directory for the project
Create a Gemfile there containing
gem "rails", "2.3.9"
gem "sqlite3-ruby", :require => "sqlite3"
Run bundle install
Run bundle exec rails . to create an app in the current path
You don't even need rvm to do this.
(I assume 2.3.11, given it's the latest)
rails _2.3.11_ new app will do this for you without you having to muck about.
(Had to make a comment since I don't have enough Stack Overflow cred and can't directly respond to answers yet.)
For folks running rails 3 now the "new" command is now required for creating new rails applications. As such "new" will need to be appended to the end of the commands.
So for Leonid Shevtsov's answer, Step 4: bundle exec rails new .
And for Robert Speicher's answer: rails new .
Install rvm and then create a new gemset, so that Rails 2 is isolated.
Or, go to the directory where you want your Rails 2 app to be, create a Gemfile like a Rails 3 app, but specify gem "rails", "~> 2.3" and run bundle install, and you should now be able to issue rails .

Resources