# Turso Quickstart (Ruby)

> Get started with Turso and Ruby using the libSQL client in a few simple steps.
> Canonical HTML: https://tursodb.cn/docs/sdk/ruby/quickstart/
> Upstream Source (MIT): https://docs.turso.tech/sdk/ruby/quickstart
> Language: en

---
<div class="doc-callout doc-callout-warning"><div class="doc-callout-title">WARNING</div><div class="doc-callout-body">

This is currently in technical preview. [Join us in Discord](https://tur.so/discord) to report any issues.

</div></div>

In this Ruby quickstart we will learn how to:

* Retrieve database credentials
* Install the libSQL package
* Connect to a local or remote Turso database
* Execute a query using SQL
* Sync changes to local database (optional)

**1. Retrieve database credentials**

You will need an existing database to continue. If you don't have one, [create one](/docs/quickstart/).

Get the database URL:

```bash
turso db show --url <database-name>
```

Get the database authentication token:

```bash
turso db tokens create <database-name>
```

Assign credentials to the environment variables inside `.env`.

```bash
TURSO_DATABASE_URL=
TURSO_AUTH_TOKEN=
```

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">INFO</div><div class="doc-callout-body">

You will want to make sure to handle database tokens securely.

</div></div>

**2. Install**

Inside your Ruby project, install the following Rubygem:

```bash
bundle add turso_libsql
```

**3. Connect**

<details class="doc-accordion"><summary>Embedded Replicas</summary><div class="doc-accordion-body">

```rb
require 'turso_libsql'

db =Libsql::Database.new(
  path: 'local.db',
  url: ENV['TURSO_DATABASE_URL'],
  auth_token: ENV['TURSO_AUTH_TOKEN'],
  sync_interval: 100
)
```

</div></details>


<details class="doc-accordion"><summary>Local only</summary><div class="doc-accordion-body">

```rb
require 'turso_libsql'

db = Libsql::Database.new(path: 'local.db')
```

</div></details>


<details class="doc-accordion"><summary>Remote only</summary><div class="doc-accordion-body">

```rb
require 'turso_libsql'

db = Libsql::Database.new(
  url: ENV['TURSO_DATABASE_URL'],
  auth_token: ENV['TURSO_AUTH_TOKEN']
)
```

</div></details>


**4. Execute**

You can execute a SQL query against your existing database by preparing a statement and then executing it:

```c
db.connect do |conn|
  rows = conn.query 'SELECT * FROM users'
  rows.close
end
```

If you need to use placeholders for values, you can do that:

```c Positional
name = 'Iku'
rows = conn.execute 'INSERT INTO users (id) VALUES (?)', [name]
```

**5. Sync (Embedded Replicas only)**

When using embedded replicas, you should call `sync` on the database to sync your local database with the primary database, unless you are using `sync_interval` (though there is no issue with calling `sync` with `sync_interval` enabled):

```rb
db.sync
```
