r/ruby • u/unselective-amnesia • Aug 28 '26
Question Centralized location for shared Gemfile?
I'm using ruby 3.3.8 under linux.
There's more than one user on this machine who does ruby development. When I, running as the root user, want to add new gem references to Gemfile on my system, I want to add these gem references into one, single, shared, centralized Gemfile that all users will automatically see when running and developing ruby code.
Where should this shared, centralized Gemfile be located?
When any user runs "gem environment", the following item is always listed among that command's output for every user who runs that command:
- INSTALLATION DIRECTORY: /var/lib/gems/3.3.0
Does this mean that I can put a Gemfile into the /var/lib/gems/3.3.0 directory, and that then, this Gemfile will be recognized and used by all users who run and develop ruby code?
Or is there perhaps a different location in which to locate such a shared, centralized Gemfile that will always be the one that's used by each ruby user?
Thank you very much.
11
u/Karew Aug 28 '26 edited Aug 29 '26
The point of a Gemfile is so that each individual project can track and pin their dependencies. It isn't intended to be a manifest of what you want installed on a whole system. You can write a shell script that you run at OS install time or update time for that.
You can, however, give everyone write access to the same gem repo on the system so that they don't all download their own personal copies of each gem to their home directories.
4
u/jrochkind Aug 28 '26 edited Aug 28 '26
i'm not sure what you are doing exactly... normally a Gemfile woudl be in a project, and the project would be checked into source control (like git), and shared that way, with changes being committed to source or going through a "pull review" process etc.
I'd recommend trying to find a way to use that sort of process, most basically using git source control to share shared files files.
But a Gemfile is intended to be for a specific project, defined by a directory and all it's contents (including sub-directories), I think you are going to run into bumps trying to use it machine-wide.
I think you will need a different technique than a Gemfile for managing what gems are installed machine-wide. I mean, you can just install the gems machine wide -- most default installations of rubygems install gems system-wide I think.
gem install gemname. May or may not have to use sudo. I'd look for advice on installing gems system-wide, not using bundler/Gemfile but just using rubygems -- that should be possible as it was the original design of rubygems, although many have been moving away from that.If you want to have the config of what you have installed captured somewhere for repeatability, I'd like at tools for managing your systems, "infrastructure as code", instead of at Bundler (which is what uses Gemfiles), it's not really set up for that and I think it's going to give you grief.
5
u/hankeroni Aug 28 '26
What's your goal here? You might be misunderstanding the role of a Gemfile, or of how the installed gems location interacts with bundler and specific projects.
Say you have 5 users and they each have 5 projects checked out. Maybe there's some overlap in projects, doesn't matter really. Each project will have it's own directory with a checked out git repo or whatever. Within the project dir there will be a Gemfile which tells the project which gems to load, and a Gemfile.lock which pins certain versions.
When the project is being loaded by ruby, bundler processes this file and requires all the specific versions of relevant gems. It can also help manage the install and update process. "Where does bundler look for gems?" is sort of an "it depends" question (many ways to install and configure ruby and rubygems), but generally speaking it will look at a system-wide gem install dir, and/or the gem install dir configured by whatever version manager (mise, rvm, rbenv, etc) the user may be using.
All that said, if all system users are just using the system ruby, then yes you can get "shared storage" of a sort by having the correct versions of needed gems installed systemwide ... but you don't really need to interact with the Gemfile from a sysadmin POV to make that happen.
2
u/bradland Aug 28 '26
Do you just want users on the system to be able to use the gems you've installed? If that's the case, you don't need Bundler (and therefore Gemfiles) at all. You can install system-wide gems. The caveat here is that if you want to use system-wide gems, developers can't use Bundler, because doing things like
bundle exec ruby my_script.rbwill explicitly isolate the execution environment from system-wide Gems.1
u/zaskar Aug 28 '26
This is what asdf and its ilk solve.
The ruby version and its packages defined by gemfile are per project and managed for the project.
Two folders next to each other may have completely different versions of everything.
1
u/flummox1234 Sep 04 '26
I think the problem is you want to maintain in house gems. Maybe setting up a geminabox server can satisfy this requirement for your team. At this point semantic versioning would track your versioning.
Otherwise perhaps you’d be better off shifting your development environment to docker based. Which also has versioning via tagging