# Run dry-cli script in other ruby projects

**URL:** <https://discourse.dry-rb.org/t/run-dry-cli-script-in-other-ruby-projects/1804>\
**Category:** Support\
**Created:** [June 5, 2024, 2:42pm UTC](https://discourse.dry-rb.org/t/run-dry-cli-script-in-other-ruby-projects/1804 "2024-06-05T14:42:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrianthedev](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/adrianthedev/32/974_2.png) [@adrianthedev](https://discourse.dry-rb.org/u/adrianthedev)\
**Post date:** [June 5, 2024, 2:42pm UTC](https://discourse.dry-rb.org/t/run-dry-cli-script-in-other-ruby-projects/1804/1 "2024-06-05T14:42:33Z")

</div>

I have [this](https://github.com/avo-hq/support/blob/main/bin/bud) cli built with dry-cli and a couple of other gems.

The way I’d like to run it is to keep it in my current directory (the repo where I’m working on it) and be able to run it globally on my machine

What I did was to symlink it to my `~/bin` directory and add that directory to `PATH`  
This allows me to run it from everywhere, but I have a different problem

Whenever I run it in one of my repos that is a ruby repo and has a `Gemfile` , it tries to run with that repo’s `Gemfile` and it’s dependencies, and naturally it will fail because that project does not have my CLIs dependencies.

The error I get when I’m in another ruby project

```auto
▶ bud release patch
Could not find activesupport-7.0.8, dry-cli-1.0.0, paint-2.3.0, tty-command-0.10.1, concurrent-ruby-1.2.2, i18n-1.14.1, pastel-0.8.0, tty-color-0.6.0 in locally installed gems
Run `bundle install` to install missing gems.

```

Is there any way to tell it to use it’s own `Gemfile` from it’s own repo?

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/alassek/32/314_2.png) [@alassek](https://discourse.dry-rb.org/u/alassek)\
**Post date:** [June 6, 2024, 6:32pm UTC](https://discourse.dry-rb.org/t/run-dry-cli-script-in-other-ruby-projects/1804/2 "2024-06-06T18:32:38Z")

</div>

`bundler/setup` is dependent on the Gemfile being present in the `$PWD` hierarchy. This only works for scripts you run while working in a project directory.

If you want to have a global script, I would recommend using [`bundler/inline`](https://bundler.io/guides/bundler_in_a_single_file_ruby_script.html) to put the Gem dependencies into the script.

---

<div class="post-metadata">

**Author:** ![postmodern](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/postmodern/32/696_2.png) [@postmodern](https://discourse.dry-rb.org/u/postmodern)\
**Post date:** [June 8, 2024, 11:46am UTC](https://discourse.dry-rb.org/t/run-dry-cli-script-in-other-ruby-projects/1804/3 "2024-06-08T11:46:22Z")

</div>

You can add this bit of code at the top of your `bin/` scripts that detect whether the project is under git control or installed via rubygems.

```ruby
root = File.expand_path(File.join( __dir__ ,'..'))
if File.file?(File.join(root,'Gemfile.lock'))
  Dir.chdir(root) do
    require 'bundler/setup'
  rescue LoadError => e
    warn e.message
    warn "Run `gem install bundler` to install Bundler"
    exit(-1)
  end
end

# rest of bin script

```

I would strongly recommend releasing your CLI tool as a rubygem.

---

<div class="post-metadata">

**Author:** ![adrianthedev](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/adrianthedev/32/974_2.png) [@adrianthedev](https://discourse.dry-rb.org/u/adrianthedev)\
**Post date:** [June 9, 2024, 5:45pm UTC](https://discourse.dry-rb.org/t/run-dry-cli-script-in-other-ruby-projects/1804/4 "2024-06-09T17:45:02Z")

</div>

Thanks for the reply @postmodern.

I still get an error about missing gems.  
Any other thing I can try?

 ![CleanShot 2024-06-09 at 13.33.59@2x](https://canada1.discourse-cdn.com/flex031/uploads/dry_rb/original/1X/b479721163070a8babc5bd4f1e477eebe3561244.png)

---

<div class="post-metadata">

**Author:** ![adrianthedev](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/adrianthedev/32/974_2.png) [@adrianthedev](https://discourse.dry-rb.org/u/adrianthedev)\
**Post date:** [June 9, 2024, 5:47pm UTC](https://discourse.dry-rb.org/t/run-dry-cli-script-in-other-ruby-projects/1804/5 "2024-06-09T17:47:59Z")

</div>

No, wait!  
I was fiddling with the code and what made it work was not to navigate to the parent directory `..` for setting the `root`.

```ruby
root = File.expand_path(File.join( __dir__ ))
if File.file?(File.join(root,'Gemfile.lock'))
  Dir.chdir(root) do
    require 'bundler/setup'
  rescue LoadError => e
    warn e.message
    warn "Run `gem install bundler` to install Bundler"
    exit(-1)
  end
end

```

Thanks for setting me on the right path.

Why did you recommend to release this as a CLI? Do you think it’s valuable?

---

<div class="post-metadata">

**Author:** ![postmodern](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/postmodern/32/696_2.png) [@postmodern](https://discourse.dry-rb.org/u/postmodern)\
**Post date:** [September 3, 2024, 12:06am UTC](https://discourse.dry-rb.org/t/run-dry-cli-script-in-other-ruby-projects/1804/6 "2024-09-03T00:06:03Z")

</div>

Distributing CLIs as a gem allows installing all of it’s dependencies and the CLI commands are globally accessible by the user. Users won’t have to deal with `~/bin` or symlinking `bud` (Fedora’s version of rubygems actually installs non-root-user-installed gem executables into `~/bin` by default).
