> ## Documentation Index
> Fetch the complete documentation index at: https://ruby-on-dspy.ralak.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Installation

> Add the gem, install DSPy and make a first call

After reading this guide, you will know:

* what Ruby on DSPy needs;
* how to add the gem and install DSPy;
* how to make a first call;
* how to start DSPy with your app, and ship it in a Docker image.

If you haven't yet, read the [Introduction](https://ruby-on-dspy.ralak.co/index.md) first: it explains the model, signatures, predictors and predictions this page uses.

## Requirements

* Ruby 3.3.1 or later
* macOS or Linux, on arm64 or x86\_64
* an API key for your model's provider, such as OpenAI

You don't need Python. The gem installs its own.

## Install the gem and DSPy

**1. Add the gem**

Until Ruby on DSPy is on RubyGems, Bundler installs it from GitHub. Add it to your Gemfile:

```ruby Gemfile theme={"theme":"one-dark-pro"}
gem "ruby-on-dspy", github: "samuelralak/ruby-on-dspy"
```

Then install it:

```bash theme={"theme":"one-dark-pro"}
bundle install
```

**2. Install DSPy**

```bash theme={"theme":"one-dark-pro"}
bundle exec ruby-on-dspy setup
```

`ruby-on-dspy setup` installs Python 3.12 and DSPy 3.5.0 in `~/.cache/ruby-on-dspy`, without touching any Python you have. The first run downloads them, which takes about a minute.

Each copy of the gem you install, a release or a commit from GitHub, gets an environment of its own there, so after you update the gem, run `bundle exec ruby-on-dspy setup` again. Until you do, calls raise `DSPyRb::SetupError`.

**3. Check the install**

```bash theme={"theme":"one-dark-pro"}
bundle exec ruby-on-dspy doctor
```

`ruby-on-dspy doctor` checks the install and starts DSPy once, without calling a model. It prints six lines, each starting with `ok`.

> [!NOTE]
>
> `setup` also removes the environments of gem copies you've uninstalled. If a copy's whole Ruby or app folder is gone, it keeps the environment and says so; delete it yourself once that Ruby or app is gone for good.

## Make a first call

A first call checks that DSPy can reach your model with your key. Start a script, `first_call.rb`, that names the model DSPy calls:

```ruby theme={"theme":"one-dark-pro"}
# frozen_string_literal: true

require "ruby-on-dspy"

DSPyRb.configure do |config|
  config.lm = DSPyRb::LM["openai/gpt-4.1-mini"]
end
```

[DSPy's language model docs](https://github.com/stanfordnlp/dspy/blob/3.5.0/docs/docs/learn/programming/language_models.md) list the providers, their model names and their options.

Then add a call to the end of the script. Its signature is the string `"question -> answer"`:

```ruby theme={"theme":"one-dark-pro"}
predict = DSPyRb::Predict["question -> answer"]

puts predict.call(question: "What is the capital of France?").answer
```

DSPy reads your key from the environment. With it in `OPENAI_API_KEY`, run the script:

```bash theme={"theme":"one-dark-pro"}
bundle exec ruby first_call.rb
```

```text Output theme={"theme":"one-dark-pro"}
Paris
```

If you keep the key elsewhere, such as Rails credentials, `config.env` passes it to DSPy, as the [tutorial](https://ruby-on-dspy.ralak.co/helpdesk/new-rails-app.md) shows.

> [!NOTE]
>
> DSPy caches answers in `~/.dspy_cache`, so the same call returns the same answer at once, without calling the model. `cache: false` in `DSPyRb::LM[...]` turns it off.

## Start DSPy with your app

DSPy starts on the first call, which takes a few seconds, and stops when Ruby exits. In a web app, that makes the first request slow.

To start DSPy before the first request, call `DSPyRb.start` in `config/puma.rb`. Don't call it in an initializer: consoles and rake tasks load initializers too, and each would start DSPy.

* With Rails' default Puma, which runs no workers, call it in `after_booted`.
* With Puma workers, call it in `before_worker_boot`. Each worker starts its own DSPy, which uses about 250 MB.

Before Puma 7, these hooks were `on_booted` and `on_worker_boot`.

## Ship DSPy in a Docker image

Docker is optional. If you ship your app in a Docker image, install DSPy while the image builds, in a folder the final image keeps.

`RUBY_ON_DSPY_ENVIRONMENT` names that folder, in place of `~/.cache/ruby-on-dspy`. With Rails' Dockerfile, add two lines:

```dockerfile Dockerfile theme={"theme":"one-dark-pro"}
# In the base stage
ENV RUBY_ON_DSPY_ENVIRONMENT=/rails/vendor/ruby-on-dspy

# In the build stage, after the bundle install step
RUN bundle exec ruby-on-dspy setup
```

Set in the base stage, `RUBY_ON_DSPY_ENVIRONMENT` points both `setup` and your app at the same folder. The final stage copies `/rails`, so the image carries DSPy.

`setup` assumes one machine uses the install folder. If another machine or container shares it, `setup` can remove the environments that one built.

## Next

> [card] [A new Rails app](https://ruby-on-dspy.ralak.co/helpdesk/new-rails-app.md)
>
> Create Helpdesk, add Ruby on DSPy and make a first prediction in the console.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.