> ## Documentation Index
> Fetch the complete documentation index at: https://docs.localdev.build/llms.txt
> Use this file to discover all available pages before exploring further.

# Environment variables

> What each service receives, which variables Localdev refuses, and how to change them while it runs

## Set variables for one service

To set environment variables for a single service, list them in the service's `env` block:

```yaml theme={null}
services:
    api_server:
        command: ./main
        env:
            API_PORT: "10001"
```

## Set variables for every service

To share environment variables across the project, list them in the top-level `env` block. A service's own `env` overrides them:

```yaml theme={null}
env:
    STORAGE_URL: "https://test-bucket.s3.amazonaws.com/"
    LOG_LEVEL: info
services:
    api:
        command: ./main
        env:
            API_PORT: "10001"
            LOG_LEVEL: debug    # overrides the shared value
```

Values are passed as the text in the file. See [`env`](https://docs.localdev.build/configuration/introduction#env) for how numbers, booleans and empty values are read.

## What a service receives

A service's environment is built from four sources. Each one overrides the ones before it:

1. The environment you started `ldev` in (`PATH`, `HOME`, …)
2. The variables Localdev sets (below)
3. The top-level `env`
4. The service's own `env`

So a variable set both in your shell and in the Projectfile takes the Projectfile's value. A variable set in both the top-level `env` and a service's `env` takes the service's value.

## Variables Localdev sets

Localdev sets these for every service, so that OpenTelemetry SDKs send their telemetry to the collector without any configuration:

| Variable | Value |
| - | - |
| `OTEL_EXPORTER_OTLP_ENDPOINT` | `http://localhost:4998`, or the port in [`collector.receivers.grpc`](https://docs.localdev.build/configuration/introduction#collector) |
| `OTEL_EXPORTER_OTLP_PROTOCOL` | `grpc` |
| `OTEL_EXPORTER_OTLP_INSECURE` | `true` |
| `OTEL_RESOURCE_ATTRIBUTES` | `service.name=<service name>` |

* You can override any of them in the Projectfile. They're never written to it.
* If you override `OTEL_RESOURCE_ATTRIBUTES`, include `service.name` yourself, for example `service.name=api,team=core`. Without it, the service's telemetry isn't labelled with its name.
* To send over OTLP HTTP instead of gRPC, override both the protocol and the endpoint:

```yaml theme={null}
services:
    api:
        command: node app.js
        env:
            OTEL_EXPORTER_OTLP_PROTOCOL: http/protobuf
            OTEL_EXPORTER_OTLP_ENDPOINT: http://localhost:4999
```

## Variables Localdev refuses

Some variables change which code a service runs: they point it at other programs or libraries, or make a runtime load extra code at start-up. Localdev doesn't let you set them in the Projectfile or in the environment editor.

| Group | Variables |
| - | - |
| Programs and libraries | `PATH`, `LD_PRELOAD`, `LD_LIBRARY_PATH`, `LD_AUDIT`, `LD_ORIGIN_PATH`, every `DYLD_*` variable |
| glibc | `GCONV_PATH`, `GLIBC_TUNABLES` |
| OpenSSL | `OPENSSL_CONF`, `OPENSSL_ENGINES`, `OPENSSL_MODULES` |
| Node.js | `NODE_OPTIONS`, `NODE_PATH` |
| Python | `PYTHONPATH`, `PYTHONHOME`, `PYTHONSTARTUP` |
| Perl | `PERL5OPT`, `PERL5LIB`, `PERLLIB` |
| Ruby | `RUBYOPT`, `RUBYLIB` |
| Java | `JAVA_TOOL_OPTIONS`, `JDK_JAVA_OPTIONS`, `_JAVA_OPTIONS` |
| Shells | `BASH_ENV`, `ENV` |

* If the Projectfile sets one, Localdev doesn't start. The error names the variable, for example:
  `services.api.env: invalid environment variable: PATH can't be set in localdev because it changes which code a service runs; set it in the shell that starts ldev instead`
* If you add one in the environment editor, the change is refused.
* Names are case-sensitive: `path` and `MY_PATH` are ordinary variables.

### What to do instead

* Set the variable in the shell that starts Localdev. Every service inherits it:

  ```sh theme={null}
  PATH=/custom/bin:$PATH ldev Projectfile
  ```

* Move the option into the command. For example, instead of `NODE_OPTIONS: --require ./tracing.js`, use `command: node --require ./tracing.js app.js`.

* For a value that only one service needs, make a small wrapper script the service's `command`.

### What this protects

The list is fixed, so other variables can still change what a program does (`GOFLAGS`, or an application's own plugin setting, for example). Anyone who can edit the Projectfile can already run any program through `command`. The list mainly stops the environment editor, which can't change commands, from being used to run other code.

## Change variables while Localdev runs

The UI has an environment editor: **Project → Environment** for the shared variables, and **Project → Services** for each service's own. It edits the same `env` blocks as the Projectfile.

### Three separate steps

A change goes through three steps, and each one is separate:

| Step | What it changes |
| - | - |
| Edit a variable | Localdev's copy in memory. It survives a page reload, but is lost when Localdev stops. |
| **Save to Projectfile** | The Projectfile on disk |
| Restart the service | The running process, which only sees the new value after a restart |

Editing doesn't write the file or touch running processes. Saving doesn't restart anything. Restarting doesn't save. While something is pending, a bar at the top of the **Project** page lists the unsaved changes and the services that need a restart.

### Saving

* Only the lines of the variables you changed are rewritten. Comments, key order, blank lines and formatting stay as they were.
* The variables Localdev sets (`OTEL_*`) are never written to the file.
* **Discard** drops your edits and reads the variables from the Projectfile again.

If something else changed the Projectfile since Localdev read it, saving is refused and nothing is written: "The Projectfile changed on disk, so nothing was written." Your edits stay in the editor. Either **Discard** them to load the file as it is now, or make the same changes in the file by hand.

### What can't be saved

Localdev can't rewrite an `env` that uses YAML anchors, aliases, merge keys or inline syntax (`api: {command: ./api}`). Saving a change to one fails with:

```
this env can't be updated automatically: services.api.env uses YAML anchors, aliases or inline {…} syntax; edit it by hand
```

This applies only to an `env` you changed. Changes to other services in the same file are saved normally.

### Hidden values

The editor hides a value until you click the eye icon when:

* the name ends in `_KEY` or `_TOKEN`
* the name contains `PASSWORD`, `PASSWD`, `SECRET`, `CREDENTIAL` or `PRIVATE_KEY`
* the value is a URL with a password, like `postgres://app:secret@db/app`

This only hides the value on screen.

Changes are accepted only from the machine Localdev runs on, even if other machines can open the UI (`-server 0.0.0.0:2345`).

## Variables that configure Localdev

`LDEV_STORE_LOGS`, `LDEV_STORE_METRIC_POINTS` and `LDEV_STORE_TRACE_SPANS` set how much telemetry Localdev keeps in memory. See the [CLI reference](https://docs.localdev.build/CLI-reference.md#environment-variables).


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