Uninstall all installed gems in a Rails project - ruby-on-rails

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 :)

Related

How do I remove a gem with all its dependencies? [duplicate]

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.

Proper way to set up Rails app on local machine using existing Github repo and RVM

I've tried looking around the internet for a solution to this but to no avail. Every single time I try to set up a new app on my local machine I run into a ridiculous amount of issues with RVM. I decided its finally time I learn to do this right.
Basically I have been given permission to a Rails project in Github and I want to get it running properly on my local machine.
Generally speaking, what I have been doing in the past is this:
$ git clone git#github.com/projectpath
$ ls projectpath
$ bundle install
$ rake db:setup
And cross my fingers. Sometimes it works, other times I have to use RVM to change rubies and redo bundle install. Today, everything is breaking and I keep running into problems like this:
$ rails -v
Rails is not currently installed on this system. To get the latest version, simply type:
$ sudo gem install rails
You can then rerun your "rails" command.
I have a feeling that I am not setting up these apps correctly and also not using RVM as its supposed to be used - basically I believe that my fundamental understanding of RVM is wrong. I would very greatly appreciate it if someone could outline the proper way to do set up a new app using a remote Github repo, as well as explain the proper way to use RVM to get this new app running. Thanks in advance!
I think your life would be easier if you used RVM's gemsets. One gemset per rails application. This keeps everything nice and isolated from each other.
So... something like this...
# install bundler in the global gemset as you want it available everywhere.
$ rvm use #global
$ gem install bundler
# create a project specific rvmrc/gemset.
$ cd /folder/containing/your/github/repo
$ echo "rvm use #myapp" > .rvmrc
$ cd into/rails/root/for/your/app
$ bundle install
At this point you're installing the gems into the 'myapp' gemset, totally isolated from your other gems. There's a new file name for .rvmrc, but I can never remember what it is. .ruby-version or something.
You may also need to create the gemset before it can be used. See the RVM docs for how to make that happen automatically.
Do these cloned projects have files named
.ruby-version
and
.ruby-gemset
in the top level directory? Those files instruct rvm (and other versioning systems) to use a particular ruby version and gemset combo. It could be sometimes you cd into a cloned directory that references the name of a gemset you've already created, in which case bundle and rake work, and sometimes it references a gemset you don't have, in which case you get mysterious blowups.

Using RVM Gemsets & Bundler & RubyMine

I use RVM to manage Ruby versions.
In my project I use Bundler to manage gems for the project.
RVM also have gemsets.
Gem in gemset don't have a connection with Bundler's gem. ← Is this correct?
I came to this conclusion because gem files stored in different locations:
RVM gemset: ~/.rvm/gems/ruby-2.0.0-p247#myApp
Bundler: [my_app_dir]/vendor/bundle/gems
So app uses Bundler gems, not RVM gemset gems.
But when I add gem to my Gemfile, RubyMine IDE shows me warning, that this gem is not in RVM gemset. So I add this gem to RVM gemset also (just to get rid of this warning).
So the questions are:
Is there any good reason to add gems in both places (RVM Gemset and Gemfile)?
If no, then why RubyMine warning me about this?
Is there any good reason to add gems in both places (RVM Gemset and Gemfile)?
The gemset is incidental, the Gemfile is absolutely the place to declare your dependencies. Where you store those gems is up to you.
It sounds like Bundler is configured to store them in a project-local path, but you're expecting them to be in a gemset. Bundler got that configuration by running bundle install --path vendor/bundle/gems at some point. It stores that configuration in its project configuration file at project_dir/.bundle/config:
BUNDLE_PATH: vendor/bundle/gems
I'm unfamiliar with Rubymine, but if you run the Rails server using Bundler (i.e. bundle exec rails server) you can ignore that warning. Bundler will correctly load the gems listed in the Gemfile.
If you want to use a gemset instead of the Bundler cache, you can just remove that line from the Bundler configuration file and reinstall your gems with bundle install.
If no, then why RubyMine warning me about this?
My guess is that Rubymine is not reading the Bundler project configuration (in project_path/.bundle/config) and does not understand where the gems are installed.
You (or if you are working in a team, somebody of your team) has once done a bundle install and specified a installation-folder. In your case vendor/bundle/gems. Bundle remembers this setting and all next invocations of the bundle command will use the same path.
There is a good reason to do it that way: your application-folder will contain all requirements and will be easier to redistribute (for instance).
Now if you want that bundle installs your gems in the normal locations, you can do the following:
run bundle install --system which will use the default location
alternatively: bundle stores it settings in a config file, I think .bundle/config and you can
check that one as well. Normally it is not needed, since bundle install --system will set that
correctly again.
then you can safely remove the vendor/bundle/gems folder
No, something's wrong, you shouldn't have anything under vendor/bundle, it should all be under ~/.rvm/gems/ruby-2.0.0-p247#myApp and perhaps ~/.rvm/gems/ruby-2.0.0-p247#global assuming your .rvmrc (or.ruby-version) is setup correctly.
What does "gem env" look like? Also "bundle env"?
So this just took me 3 days, since nothing else I was finding here was helping. I also run multiple projects through RubyMine at the same time (and different versions) so setting my GEM_PATH and launching from command-line doesn't work for me. I use IntelliJ with RM plugin, this should work on RM standalone.
Bundler seems to install custom gems, or gems from custom repos, in a different directory than gems from rubygems, or github.
/Users/YOURUSER/.rvm/environments/ruby-{version}\#yourgemset/bundler/gems
One thing I wasn't able to fix is in the GEMFILE, I have some custom git_sources, and rubymine highlights those and gives me the warning that it cannot find the gem in my bundle (you can ignore this warning; unless the gem doesn't install at all):
gem 'somegem', custom_git:'gituser/repo'
is highlighted and warning is "Gem x cannot be found... in SDK'
However Bundler installed it, and ruby is able to load it.
# TLDR: Steps to have RUBYMine find extra gems, and show up in external libs
vim ~/.rvm/environments/ruby-{your-verion-here}\#{your-gemset}
add the bundler gems path to GEM_PATH entry
export GEM_PATH='/Users/YOURUSER/.rvm/gems/ruby-{version}#yourgemset/bundler/gems:{the rest}'
save the file
Restart RubyMine/IntelliJ, reopen your project (if not open automatically)
Open the Project Structure dialog > Platform SDKs > Choose the GEMSET you're working with
add /Users/YOURUSER/.rvm/environments/ruby-{version}\#yourgemset/bundler/gems to your classpath
Hit OK, then REOPEN the Project Structure Dialog > Project Settings > Project
Your project will likely have no SDK So select the one you're using again and hit OKAY
RM/IJ will now reindex files
You're done, any broken/missing inspection links should now be fixed. And you should be able to introspect into your gems.
In the above instructions that when you run bundle install (from terminal or RM) it works successfully, and that you have RVM correctly setup, and gemset already created
I hope this helps! Let me know if I should clarify anything (happy NYE)

Uninstalling rails and gems, getting error "cannot uninstall, check 'gem list -d ...'

I'm going to start with the usual noob line, "I'm new to rails". Oh, and I'm running Mac OSX 10.6.4
I've been following a bunch of guides to get set up, specifically these two here and here. The guides are great, the reason I'm using the second one is because of RVM and the reason I'm using the first is for MYSQL.
Anyway, when I started, I wasn't following the directions completely and so after I installed RVM, for some reason I installed rails with sudo gem install rails -v 2.3.8 because thats the version I need. So... I realized I was using the system ruby, and wasn't taking advantage of RVM. What I did now was install Ruby 1.9.2 so I can install rails 3 and test it out, and I also installed 1.8.7 and rails 2.3.8 so I can use it for what I need it.
Now, I'm trying to uninstall the gems from the system Ruby, but whenever I try to I'm getting this error:
ERROR: While executing gem ...
(Gem::InstallError)
cannot uninstall, check 'gem list -d
whatever gem I try to uninstall'
Any ideas on how to remove rails and all these gems? I just want to start from scratch with RVM.
UPDATE:
By running the command gem list -d rails I've located the gems in /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/18. Should I delete them manually?
SOLUTION!! I still don't understand why this happened, I'd love if someone could explain. Why was the path non-existant? What caused this error?
Also, I want to mention that the solution I linked to has a comment saying that the question is a duplicate. However, the original has a different solution and did not help me (though its the basis to finding this answer). Simply deleting the gems manually in finder would not remove them from the gem list.
Without further ado - it turns out that when trying to uninstall the gem, it can't locate its path (I think the problem is because of installing with sudo, but I might be wrong). What you need to do is (you have to do this one by one for each gem, or at least I had to):
gem list -d 'name of gem' and note the "Installed at:" location (in my case, /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8)
sudo gem uninstall 'name of gem' -i 'the path noted above' (ex. in my case, sudo gem uninstall rails -i /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8
Some gems still might not uninstall returning a permissions error. If this is the case, what you need to do is create a folder /bin, in the path above. (in my case, mkdir /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/bin
Continue uninstalling as in step 2, still using the original path (/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8).
Now all uninstalls should work!
Two things you should note when using rvm:
You should NEVER use sudo to install gems, just do a gem install xxx
You can not uninstall gems installed in the global gemset from within another gemset. You should switch to the global gemset and uninstall from there:
rvm gemset use global
gem uninstall xxx
execute this either in irb or in a script proper:
`gem list --no-versions`.split("\n").each do |gem|
`gem list -d #{gem}`.gsub(/Installed at(.*):.*/).each do |dir|
dir = dir.gsub(/Installed at(.*): /,'').gsub("\n", '')
system "gem uninstall #{gem} -aIx -i #{dir}"
end
end
try this way :
sudo apt purge rails
ok i see....
at this point, if you're in the terminal, it shouldn't take but a few minutes to reinstall the whole shebang
I suspect you might not be in the correct rvm name that has the gems you're trying to install so thats why i'd suggest reinstalling rubygems and building you're core gems from the beginning in your Global rvm gemset name.
A more generic answer to delete all gems for older versions of gem 1.8.
gem list --no-versions | xargs sudo gem uninstall -aIx
I was using RVM to manage my gemsets and had not selected the gemset. I wasn't that I had selected the wrong gemset, it was that I hadn't selected the gemset at all. A lovely way to spend the bulk of an hour of my first morning back after my summer holidays!

How to completely wipe rubygems along with rails etc

Ok, so I decided I'd be cool and try to use Rails3 that's in beta. Then, things were getting hard to manage so I got rvm. I installed ruby 1.9.2-head in rvm and things were working, and then a computer restart later rails wouldn't start up. So I figured I'd just try running the system ruby and start rails in it. same error. Then, I uninstalled rails3 and got rails: no such file or directory type errors..
So now I'm royally screwed because rails2 is still installed but will not uninstall because of invisible dependencies, along with a lot of other random gems. How do I completely clear out all ruby gems and such so I can start anew?
I've recently had to so just this. I had built up alot of cruft with my system installed ruby and gems and wanted to clean all that out and move everything over to run under rvm for various projects.
1. Clean up old and busted
First thing I did, before messing with rvm (or run rvm system to get back to the system ruby), was to remove all my gems:
gem list | cut -d" " -f1 | xargs gem uninstall -aIx
WARNING: this will uninstall all ruby gems. If you installed as root you may want to switch to root and run this.
2. Install new hotness
Now you can run gem list to see what is left.
Time to install rvm, I recomend blowing away your current install and reinstall fresh:
rm -rf $HOME/.rvm
bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
Now the real trick is to use gemsets to install rails 3, and this is easy if you follow Waynee Seguin's gist:
rvm update --head
rvm install 1.8.7
rvm --create use 1.8.7#rails3
curl -L http://rvm.beginrescueend.com/gemsets/rails3b3.gems -o rails3b3.gems
rvm gemset import rails3b3.gems
One difference is I use 1.8.7 since I have had issues with 1.9.2-head and RSpec, but 1.8.7 has been smooth.
You say that you already got Rails 2 and afterwards installed rvm. Try if you can remove Ruby and Rails using the package manager of your system (yum, apt, ...)
The error you got is most likely the result a link in some general place which still exists, but is pointing to the real installation directory which no longer exists.
For example: /usr/bin/ruby exists and is pointing to /usr/local/lib/ruby/1.9.2/bin/ruby which no longer exists.
Afterwards manually clean out any Ruby and Rails related paths in /usr and its subdirectories.

Resources