I'm trying to understand what exactly happen when I'm installing gems.
At first I thought that using the "gem install gem_name" command (and after it the "bundle install) will make sure I have the proper files and dependencies and then it will update the gemfile.
But I've noticed that whenever I add gem using the commend line it doesn't appears at the gemfile yet I'm still able to use its functionality.
so is there is any reason to use the comment "gem install gem_name" insted of just adding the gem name to the gemfile?
The reason to use a Gemfile is that you can install all required gems in a single go.
Imagine that you work in a development team and a new member starts contributing to your application.
Al he has to do is checkout the repository and run bundle install.
Only use the command gem install if you just want to install a gem that is not nessecarily relevant to your project.
If the project requires the gem; put it in the Gemfile.
Related
I faced some issues with bundle install.
When we run 'bundle install' , One of the dependency gem in Gemfile.lock is get auto upgraded.
As per theory, "bundle install" will look the Gemfile.lock for version and won't resolve the version and will install the same versions. It will resolve only when there is no Gemfile.lock or when we give "bundle update".
In our server, we having Gemfile.lock but "bundle install" is updated the particular gem mentioned gemfile.lock(it's a dependency gem , so we not specified in gemfile), It should not happen like this, because already one version present in lock file, even though that version is get auto updated, Due to this upgrade some major functionality is broken in the site.
For your references:
bundler version - 1.17.2
ruby version - 2.5.3
gem version - 2.7.6
rails version - 5.2.3
that dependency gem name is "nokogiri", This gem locked as 1.11.7, But it's updated to "1.12.1" when i give "bundle install"
Any idea to prevent this issue in future?
First of all it'll be great if you shared the Gemfile.lock error so as to know what i particular might be causing that upgrade. But from afar I think as you said this gem is a dependency gem and it is not stated in your gemfile. It could be that another gem also depends on this gem and per that requirement it triggers an upgrade even before your supposed gem line is run which may be leading to the error. Read the error thoroughly and you can identify the gem(s) causing this.
After your update I have read around on this.
Exactly so as stated earlier on, one of these gems could be the reason why your particular gem gets updated with every bundler install. Unfortunately there is no true turn around to solving this but bundler does give a way around.
You can use the --frozen option with bundler which freezes your gemfile.lock to the current versions for each gem and does not update any gem but only installs new gems that you have. Unfortunately this has been deprecated and can only be done be done from /.bundle/config. This can be done from the command line in the root of your project.
run
bundle config frozen true to freeze bundler from updating your gems in gemfile.lock
You may have to grant write permissions to your user to be able to edit the bundle configurations.
I found this article as well from bigbinary.com
I want to get a Gem's version without running bundle install.
Which is to say I want figure out what version bundle is planning to install without actually installing the gem.
Say read it from the Gemfile.lock(and Gemfile) combined.
Is there a way I can resolve what version bundler plans to install?
I need this because I want to cache the expensive installs while running docker build.
Gems like rails(nokogiri) take a while to install and I would like to do gem install rails -v ... in a previous step before running bundle install.
For this purpose i need to get the rails version before hand
If you add a new gem to your gemfile, but don't do bundle install, it doesn't install yet. Instead, you can run bundle lock, which generates a new lock file. This includes the gem version of the new gem that would be installed.
By running bundle show new_gem, it shows it isn't actually installed.
To be sure maybe get a backup of the original Gemfile.lock before running the command though.
By default if no version is specified in the Gemfile, running bundle install will attempt to install the latest version of the gem which is compatible with the rest of the gems and ruby version in your project. This will create a Gemfile.lock file if one doesn't already exist. If a Gemfile.lock file is already committed to git repo, it should then install the versions specified in Gemfile.lock. The point of bundler is to handle dependencies to insure your stack works correctly.
To see the version of a gem bundler is currently using you can run
bundle show rails
You will probably want to specify the ruby version in the Gemfile for example
ruby '~> 2.5' #
You can specify exact version of a gem in the Gemfile like this which you should be able to rely on to be the version bundler will install so long as it's compatible with the rest of the stack. bundle install will throw errors if there are incompatible gem versions.
gem 'rails', '4.2.11' # this will always install only this version.
You may also use pessimistic operator (~>) to set for only minor updates
gem 'influxdb', '~> 0.6.1' # could go to 0.6.2 but never 0.7.0
You can also set minimum versions like this although it's probably not what you need for your question.
gem 'pg_query', '>= 0.9.0'
If you have a Gemfile.lock already in your repo you can see which version would be installed by running for example:
gem show rails
Which would show you the version and weather it or not it is currently installed.
For more info see bundle --help
I'm using Travis CI for a rails project, and I'm getting a "Could not find foobar-0.2.3 gem in any of the sources." at the bundle install stage. (sorry, not at my computer right now, and don't remember the exact gem)
It looks like this is because a new version of that gem was recently put on RubyGems, and the old version of the gem (which is in my Gemfile.lock) was yanked.
How am I supposed to fix this break? Am I supposed to manually go into my Gemfile.lock and put the correct version? (Manually editing the Gemfile.lock seems weird to me, not sure if that's what you're supposed to do.)
It's never wise to manually edit your lockfile, as you can't know what has changed with the gem in question, it might have added dependencies for example.
If you use bundle update gemname Bundler will update that gem to the latest available version and automatically update your Gemfile.lock. You can also manually specify a version in the Gemfile and run bundle install.
Like the title says, if I use a gem in one app (install it, add to Gemfile, etc.) do I still have to run gem install xxx in a new app?
Unless you're not using Bundler, you very rarely need to run gem install ... at all, actually.
More often than not, unless you're using different Rubies for each of your projects, all of your gems live in folders that get shared across all of the projects that use them. If you're using rvm you can see this directory by running rvm gemdir.
When you use Bundler, it will automatically handle loading the appropriate version of the gem in the (likely) case that you have several versions installed.
No.
In fact, for a modern (Rails 3+) app you should never need to run gem install (except, of course, for the initial gem install bundler rails that you need to do once), you should just add the gem to your Gemfile and then run bundle install. Let Bundler take care of dependencies and installing for you, that's what it's there for.
I have a weird trouble, i've updated the .irbrc file to make better looking, for this I
installed awesome_print, wirble and looksee gems, check them by gem list - every gem is there.
And when i run rails console i got this:
Cannot find awesome_print gem. Please run 'gem install awesome_print' to install it.
Cannot find wirble. Please run 'gem install wirble' to install it.
Cannot find looksee. Please run 'gem install looksee' to install it.
I've no idea why it happen. I have rvm on my system but I don't think it causes the problem.
Thanks.
To install gems in Rails 3, add them to your Gemfile
For instance:
gem 'awesome_print'
gem 'wirble'
gem 'looksee'
Then run bundle install.
It's also a best practice to create a gemset with rvm per-project to isolate dependencies among projects.
To do this, in your Rails root directory run: rvm --create --rvmrc 1.9.2#myproject (substitute 1.9.2 with whatever version of Ruby you want to use).
After creating the gemset, rerun bundle install.
You'll notice an .rvmrc file has been created, which makes sure whenever you cd to that directory, the "myproject" gemset will automatically be used. Add this to version control so other developers get the same effect.