xCander
xCander

Reputation: 1338

Add a Ruby command-line application to /usr/bin?

I have written a shell program in Ruby. Now I want to add it to my bin directory so that I can call the program by running $ my-rb-prog ....

First I tried to symlink my file into /usr/bin but then it said that it couldn't load the modules that I required.

On my second try, I tried to build a gem out of my project which worked fine, but I can still not access my shell program. After that I installed the gem. Here's what my gemspec looks like:

# -*- encoding: utf-8 -*-

$:.unshift(File.join(File.dirname(__FILE__), "/lib"))
require 'webcheck'

Gem::Specification.new do |s|
  s.name        = "webcheck"
  s.version     = WebCheck::VERSION
  s.platform    = Gem::Platform::RUBY
  s.authors     = ["Victor Jonsson"]
  s.email       = ["[email protected]"]
  s.homepage    = "http://victorjonsson.se"
  s.summary     = %q{Check your website man!}
  s.description = %q{Just check it!}

  s.required_ruby_version     = '>= 1.9.3'

  s.add_dependency "httparty", "~> 0.12.0"

  s.post_install_message = "Just check it!"

  s.files         = `git ls-files`.split("\n")
  s.executables   = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
  s.require_paths = ["lib"]
end

I thought that I would get access to my shell program, which is a Ruby file located in the bin directory inside my project, after that I had installed the gem but it clearly isn't that easy.

This is my first day of coding Ruby if you can't tell.

Upvotes: 1

Views: 370

Answers (2)

Josh
Josh

Reputation: 5721

Don't forget to add #!/usr/bin/env ruby to the top of your Ruby script.

"Making a Ruby Script Executable" is a really good tutorial on making your executable available system wide, without the use of a gem.

Upvotes: 1

Sergei
Sergei

Reputation: 118

First, add a "she-bang" string as first line of your file. It will allow the shell to run the file. It should be:

#!/usr/bin/env ruby

Then give execution permissions to the file:

$ chmod +x your_file_name.rb

Now you can run your application:

./your_file_name.rb

Also you can add the path to the directory with this script to the PATH variable and run the application from anywhere you want.

# You may do this in ~/.bashrc file
PATH=$PATH:path/to/dir/with/script/

Upvotes: 1

Related Questions