How to create a gem repository - ruby-on-rails

I'm trying to deploy my local app to dreamhost using Capistrano. While running the cap deploy:cold for the first time, I'm getting an error for missing gems. The dreamhost wiki also recommends that users should maintain their own gem repository in the folder $HOME/.gems .
Going back to the main question :
Where are the .gem files stored locally? I can find the individual gem folders in /Library/Ruby/Gems/1.8/gems/ (on my mac). But I cannot find the respective .gem files.
Would copying these .gem files to the server at the location $HOME/.gems/gems solve the problem of missing gems?
Is there a better way to avoid this?

All you need to do is put your gems in a folder and point your app to that; however, this is obsolete. Bundler what what you should use. Below is an article on how it works with DH:
http://wiki.dreamhost.com/Bundler#Get_Your_App_Using_Bundler

Related

Where are Ruby gems located on a server?

My understanding is that the gemfile in a Rails app only provides references to the actual code of these gems on your local computer. So when you're running your app locally, it's pulling the gem code from your local computer. What happens when you deploy though? The server runs your rails code, but does it also hold all the references in your gem file and automatically download them as well?
Yep. If you deploy on Heroku, you can see bundler doing its work and pulling down the gems.
As per the Bundler docs, you can use bundle show --paths to see exactly where your gems are being loaded from.
Additionally, if you aren't using bundler, you can use the command gem environment to see gem paths on the system.
See this existing answer for more info: How can I find where gem files are installed?

Bundler unable to find custom gem

I have a custom gem built as a .gem file that I am trying to reference from my Gemfile. I have placed the .gem file, along with the .gemspec, in the vendor/gems folder. My Gemfile has the following line:
gem 'umlgrader', '1.0.0', :path=>'vendor/gems'
When I run bundle install, it claims to have found the gem but it says it is "using" the gem, rather than "installing" it, even though the gem was not previously installed on my machine. When I try to run my app, I then get a NoMethodError when it tries to call any of the methods in the gem. Why isn't Bundler installing the gem?
I have gotten it to work by unpacking the gem in that directory and then editing the Gemfile as follows:
gem 'umlgrader', '1.0.0', :path=>'vendor/gems/umlgrader-1.0.0'
This solution is less than desirable. I would prefer to be able to install the gem using Bundler since I am trying to deploy the app to Heroku. I have already tried a lot of the solutions I have found online, but I am open to any suggestions.
EDIT:
Some of the other pages I have already gone through and tried:
Bundler: installing a specific .gem file
How to use Bundler with offline .gem file?
How do I specify local .gem files in my Gemfile?
I also noticed a lot of people suggest pointing to a Git repository containing the gem. I would rather not do this if I don't have to.
The Bundler documentation is somewhat cryptic on that topic, but that is the intended behaviour of Bundler. In order to use the path option you must unpack the gem at the desired location.
But that should be no problem for Heroku. After all, you are still using Bundler, even if the gem is already unpacked. It's just a step less in the gem installing process...
Without Docker
You need to clone the repository from github (or other source) to your custom folder. In example below the steps to reproduce how I use custom path to edit gems in a separeted folder:
In this case I use a custom_gems folder inside /bundle: mkdir /bundle/custom_gems.
cd /bundle/custom_gems.
git clone <gem-repository-source>. Is necessary to clone because if you copy from other folder in your computer probably some files are missed.
Set in your Gemfile: gem '<gem-name>', path: '/bundle/custom_gems/<gem-name>'.
Restart you application.
In docker-compose (or Docker)
With docker is little different, in this case I use a /gems folder inside my rails application folder: mkdir <my-app>/gems.
cd <my-app>/gems.
git clone <gem-repository-source>. Is necessary to clone because if you copy from other folder in your computer probably some files are missed.
Set in your Gemfile: gem '<gem-name>', path: '/bundle/custom_gems/<gem-name>'.
If you use docker-compose, you need to bind folders with volumes config, like below:
volumes:
- ./app/gems:/bundle/custom_gems
With this, your local folder (your machine) copy files inside ./app/gems to /bundle/custom_gems in Docker container.
Restart service.
If you NOT use docker-compose, you need add in Dockerfile some like:
ADD ./app/gems /bundle/custom_gems

Why do I need to run bundle install after I reformatted my computer?

I'm very new to ruby on rails which is why I'm trying to get a basic understanding of how it works. I have just created my very first rails app which is located in my dropbox folder. As I reformatted my computer two questions arise.
1) I had to do bundle install again in order to launch my local server. Why is that?
2) Also, prior to reformatting, I did cmd+p in sublime to search for files. However, files within gems didn't show in the results - now they do.
Everything works just as it did before I reformatted, I'm just trying to understand those two elements.
Thanks in advance!
Gem are not stored within your project.
If you are using RVM to maintain ruby version then gems are stored within that rvm folder.
As you have formatted your PC, it has removed gem folder from your system.
So, it is requesting for "bundle install".
This command will installing gems listed in applications GEMFILE.
If gem are already present in gem folder then it just use it for your project.
Gems that you require in your project are not stored in your project they are normally stored in a gems folder within your ruby install folder which would be on your local drive.
If for instance your start a new rails project you would see when you run bundle install every gem it checks for would already be installed becasue it's looking into the local gems folder.

Rails: adding vendor/cache to SCM?

Page 239 of Agile Web Development with Rails instructs us to add the vendor/cache directory to git.
Is this actually recommended practice? I was under the impression that this directory was platform-specific. Will it cause problems to commit a cache dir under OSX and then deploy to prod under Linux?
This is where your app's gems are stored if you package them locally. So if you deploy from the SCM source and you want to use the exact gem packages that you are using locally, you'll need these files, which is why I suspect the book suggests this.
As far as the gem files go, it won't cause a problem if you develop on a Mac and deploy to a Linux server.
Short answer - yes.
Long answer - it's very handy to keep your gem dependencies with your application. Not just a Gemfile and Gemfile.lock but also the gems themselves.
There are numerous advantages - such as having all your gems available without having to connect to a gem server.

How to move ruby gems from one server to another

I have a RoR server that I am decommissioning and need to move the existing Rails apps to a new server. The Rails apps are running Rails 2.3.5 and RubyGems 2.3.5.
The problem I am having is that some of the Gems do not seem to be available anymore - one example is thoughtbot-paperclip --version=2.3.1
I need to install these gems in order for the apps to work on the new server.
Would these gems have been removed from the remote repos? And if so how can I transfer them to the new server? I have tried just copying the entire gems folder from one server to the other but this does not seem to work.
Most of the old gems got renamed, as they usually were in a github_username-gem_name format. But after GitHub stopped hosting gems and gemcutter.org came, they got renamed to only the gem_name, such as thoughtbot-paperclip to just paperclip.
You might get away with just specifying the version number in a Gemfile, such as
gem 'paperclip', '2.3.1'
Looks like the gem has been renamed to simply paperclip. Check the repository.

Resources