Error message when trying to create Gemfile - ruby

Can someone please break down the very very beginnings of how to run a Gemfile. I've read the Bundler webiste and it says I have to type "Gem Install Bundler" then source "https://rubygems.org". Everytime I do that I get an error message saying "Source is not recognised as an internal or external command, operable program or batch file. I've tried creating an empty gemfile first by typing "bundle init" but when I type my source, I get the error message. I also found some literature about telling it to look in github and tried to direct it to the actual project in github but it gave me the same error message.
I am completely new to coding but have to run some gems off a file in github to test my answers on some questions I've been given. There's a gemfile on the github page with this in it:
source 'https://rubygems.org'
gem rspec
gem pry
Any help for an absolute beginner is much appreciated.

You need to use source 'http://rubygems.org' not https:// which isn't supported yet

Run
gem install bundler
then you can author a Gemfile like
source 'https://rubygems.org'
gem 'rspec'
gem 'pry'
after this, you may run
bundle install
and as an outcome rspec and pry gems will be installed in your system.
You have more information about Gemfiles in Bundler official documentation.
Also, bear in mind that you are installing those gems system-wide, this might cause dependency problems and/or version problems if you have more projects where those gems are required. If that is the case, I recommend you using something to control Ruby versions and gemsets, for example RVM.

Related

Can't install gem using Bundler's Rakefile install task when developing a custom gem

I'm developing a couple of private gems and I think I don't understand correctly the PATH/GEM_PATH and/or Bundler/RVM installation flow, would love if someone could chip in.
I have a repository with two gems (A & B for simplicity sake). I've developed the gems using the scaffolding + following the guidelines provided by this bundler tutorial.
Thanks to the Bundler project I have a few Rakefile tasks like rake build, rake install, rake install:local and rake release. Because of the private nature of these gems I can't release them to RubyGems (and we haven't looked into hosting our rubygems).
My machines are using RVM to manage ruby versions and Bundler version 1.15.1
What I want to do: Assuming a new machine/developer trying out the project, ideally we would cd into each of the subfolders (currently 2, gem A and gem B), run rake install and after that we should have the gems available system wide for the current user.
What is happening: The gems are built and work properly, but they are only available inside the subfolder of each gem i.e. gem A is only available inside the subfolder A and gem B is only available inside subfolder B.
What I've tried: So, after rake build/install/install:local a new .gem file is generated under pkg. I've tried to manually install the "compiled" file using gem install pkg/A.gem, gem install --local pkg/A.gem and gem install --local --user-install pkg/A.gem without success. (there are plenty of SO questions/answers about this)
I believe this has something to do with the PATH variables, but like I said before I don't fully understand the way they are managed. I get the following results from these commands:
# Our gem
> gem which A
/home/ubuntu/.rvm/gems/ruby-2.4.0/gems/A-0.1.8/lib/A.rb
# Pry, available globally
> gem which pry
/home/ubuntu/.rvm/gems/ruby-2.4.0/gems/pry-0.11.1/lib/pry.rb
I've been lost and frustrated for far too long now, any help is appreciated. Also open to hear suggestions of better private gem installation flows :)
Yes, it has something to do with your PATH variables. Your installation seems to be good.
I advise you to first affirm your gems installation path with:
echo $GEM_HOME
The double check your PATH to ensure its present and also confirm that the GEM home is also where the gem got installed into from the rake install
echo $PATH
If not, put it in your path and you should be fine with something like this:
echo PATH=$PATH:$GEM_HOME >> ~/.bashrc
source ~/.bashrc
Build your gem as per that guide you linked. You should end up with a gem file. Distribute this as you see fit (I use rsync/crontab to download newer gem versions but anything goes). User can install the gem as follows:
gem install --user-install /path/to/your/file.gem
This will install the gem in the user's ~/.gem/ruby/<version>/gems/<your-gem-name> directory.
Tried it with an empty gem (foodie, as in that example guide) and it works fine. But if you don't specify the --user-install parameter it will try to install in the system ruby dir (/usr/lib/ruby/gems...)

Calling another Gem as a program from my Gem

I'm working on a command line tool (a ruby gem) to quickly and easily generate custom jekyll repositories from some basic user input for my company. I would like to add a feature where the gem can automatically build the repo and output the finished directory, but the jekyll gem doesn't seem to provide any internal ruby interface to do that, at least not one that would be easy to implement.
Is there a way I could have my gem call their gem as a program, i.e. system 'jekyll build', but without having to worry about whether or not the user has the jekyll gem installed?
You can add jekyll to your gems list of dependencies, by adding the following to your gem's .gemspec file. That means whenever someone installs your gem, jekyll would also be installed.
spec.add_dependency 'jekyll'

How to contribute to a Ruby Gem

I am trying to contribute to a Ruby gem and I don't understand how to test a local gem without using a globally installed gem.
The gem I want to contribute to is a command line interface gem. I clone the gem into a directory then cd into that directory. However, when I run commands in the terminal when I'm in the cloned project directory it still uses the global gem. I've even run
gem uninstall gemname
then while inside the newly cloned gem directory I redo
gem install gemname.
No matter what changes I make to the gem, I can't see the results or what my contributions are doing because it's always running the global gem.
When I do try to type a command line command that is supposed to interact with the gem while in the cloned gem directory I get:
-bash: ~/.gem/ruby/2.1.0/bin/githubrepo: No such file or directory
I've done a ton of research but I'm just not getting it. Help?
gem install gemname will look for a .gem file in the current directory. If not found it will look for it on the web.
gem install --local /path/to/your/gemname.gem will allow you to target a particular directory. You may need to gem build gemname.gemspec first, so it has your changes.
Instead of doing this, I would write tests in the gem directory itself. It's likely that when running code in there, you can simply require 'gemname' in Ruby to get the gem functionality.
If it's a well-written gem, it should have tests already. They will most likely be in a directory called test or spec. Have a look at these tests and try to carry on in that style to test your changes. This will make your code changes far far more likely to be accepted as a pull request.

Bundler not installing private gem from git repo

I am having trouble creating a private gem, pushing it to a private git repo, and then USING that gem in my Gemfile via a :git reference.
The problem is that bundler (while giving no error) doesn't seem to install the gem for me.
I found that I can demonstrate this with any gem, not just mine. So I'm going to demonstrate it using the 'colorize' gem since it is already on github and rubygems, and is a simple gem that has no dependencies. Here is my test.rb file that uses the gem:
require 'colorize'
puts 'some blue text'.colorize(:blue)
CASE ONE (this works):
The Gemfile is as follows:
source 'https://rubygems.org'
gem 'colorize'
Bundle runs happily, and ruby test.rb outputs the blue text just fine.
I then run gem uninstall colorize to clean up before the next test.
CASE TWO (fails):
Now, I change the Gemfile to this:
#source 'https://rubygems.org'
gem 'colorize', :git => 'git#github.com:fazibear/colorize.git'
Note that I commented out the rubygems.org line to be sure I don't accidentally get the gem from rubygems.
Bundle again runs just fine, and can be seen to get the gem from the repo. So far so good. But now, ruby test.rb fails: require cannot load 'colorize'. It would seem that the gem didn't get installed, and indeed if I run gem uninstall colorize to clean up, it says that colorize is not installed!
So what am I doing wrong here, or failing to understand? I want to have the gem installed from a git repo, not rubygems, since the gem is a private gem.
Thanks,
-- Glenn
It seems to me your ssh connection to github is not setup properly.
Try doing a ssh -T git#github.com as suggested here on github, this will give you an error if things are misconfigured. Follow the steps mentioned in the link and then check again, things should work fine then. FWIW, I tried and was able to install the gem in this fashion.
If you were installing this gem on a server, run this command on the server itself.
An alternative to overcome this (the limitation of configuring ssh keys for each account to use git#github.com url in :git) is to instead use a https url (Check out this thread); this however beats all the purpose of using ssh keys.
Of course the gem isn't installed in second case, that is correct, since it was removed. But when bundler clones a git repo, or to use a path key to create a gem, it doesn't use ruby's gem utility, and to know weither the gem is installed successfully you have just to run, and to see the path of the installed gem:
$ bundle show colorize
/home/user/.rvm/gems/ruby-~.~.~#irb/gems/colorize-~.~.~
If case the gem isn't properly installed, you shell see:
Could not find gem 'colorize'.
And will have to issue bundle install again, and trap errors if any.
Since the bundler doesn't call the gem command, doesn't put the checked out gems from git repos or GitHub into common gem pull, and instead of it creates the gem itself inside its pull, and controls it. You should run your script using the bundler itself:
$ bundle exec ./test.rb
or
$ bundle exec ruby test.rb

Bundler can't find Ruby gem that appears on website

I installed Bundler on a pre-Rails 3 application and am trying to use it to install gems. My Gemfile contains the following lines:
source :rubygems
[...]
gem "RubyInline", "3.8.1"
However, when I run bundle install I get this error:
Fetching source index for http://rubygems.org/
Could not find gem 'RubyInline', required by 'memcache-client (= 1.6.3)', in any of the sources
The gem appears on the rubygems website:
http://rubygems.org/gems/RubyInline
Why is it giving me an error then?
I'm afraid this resolved itself after changes to fix other issues, and I'm not sure what the fix was. The source of several other issues was wrong permissions on various gems/binaries.
This kind of issue for me seems to be resolved on occasion by applying
bundle update
before
bundle install
The effect is to resolve old dependencies from when the bundle was originally produced and hence a gem that has been superseded (or whatever) will no longer be in the Gemfile.

Resources