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

# Elixir

> Upsun Cloud supports building and deploying applications written in Elixir. There is no default flavor for the build phase, but you can define it explicitly in your build hook. Upsun Cloud Elixir images support both committed dependencies and download-on-demand. The underlying Erlang version is 22.0.7.

> [!TIP]
>
> You can now use composable image to install runtimes and tools in your application container. To find out more, see the [Composable image](https://developer.upsun.com/docs/configure-apps/app-reference/composable-image.md) topic.

## Supported versions

You can select the major and minor version.

Patch versions are applied periodically for bug fixes and other maintenance updates. By default, you get the latest available patches each time you deploy. If your project has a [maintenance window](https://developer.upsun.com/docs/core-concepts/build-deploy.md#maintenance-window) configured, Upsun Cloud applies patches only during that window. Upsun Cloud may still apply urgent or critical fixes outside of the maintenance window.

* 1.20

## Deprecated versions

The following versions are still available in your projects,
but they're at their end of life and are no longer receiving security updates from upstream.

* 1.19
* 1.18

To ensure your project remains stable in the future,
switch to a [supported version](#supported-versions).

## Retired versions

The following versions have been retired and are no longer available.
If your project uses a retired version, you must update to a [supported version](#supported-versions).

* 1.15
* 1.14
* 1.13
* 1.12
* 1.11
* 1.10
* 1.9

### Specify the language

To use Elixir, specify `elixir` as your [app's `type`](https://developer.upsun.com/docs/configure-apps/app-reference/single-runtime-image#type):


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     \<APP\_NAME>:
&#x20;       type: 'elixir:\<VERSION\_NUMBER>'
&#x20;&#x20;

For example:


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'elixir:\{\{version:elixir:latest}}'

## Built-in variables

Upsun Cloud exposes relationships and other configuration as [environment variables](https://developer.upsun.com/docs/development/variables).
Most notably, it allows a program to determine at runtime what HTTP port it should listen on
and what the credentials are to access [other services](https://developer.upsun.com/docs/add-services).

To get the `PORT` environment variable (the port on which your web application is supposed to listen) you would:

```elixir theme={null}
String.to_integer(System.get_env("PORT") || "8888")
```

Some of the environment variables are in JSON format and are base64 encoded. You would need to import a JSON parsing library such as [JSON](https://hexdocs.pm/json/readme.html) or [Poison](https://hexdocs.pm/poison/api-reference.html) to read those. (There is an example for doing this to decode the `PLATFORM_RELATIONSHIPS` environment variable in the section [below](#accessing-services-manually).)

> [!NOTE]
>
> Tip
> Remember `config/prod.exs` is evaluated at **build time** and has no access to runtime configuration. Use `config/releases.exs` to configure your runtime environment.

## Building and running the application

If you are using Hex to manage your dependencies, you need to specify the `MIX_ENV` environment variable:


&#x20;   applications:
&#x20;     myapp:
&#x20;       type: 'elixir:\{\{version:elixir:latest}}'
&#x20;       variables:
&#x20;         env:
&#x20;           MIX\_ENV: 'prod'

The `SECRET_KEY_BASE` variable is generated automatically based on the [`PLATFORM_PROJECT_ENTROPY` variable](https://developer.upsun.com/docs/development/variables/use-variables#use-provided-variables).
You can change it.

Include in your build hook the steps to retrieve a local Hex and `rebar`, and then run `mix do deps.get, deps.compile, compile` on your application to build a binary.


&#x20;   applications:
&#x20;     myapp:
&#x20;       type: 'elixir:\{\{version:elixir:latest}}'
&#x20;       hooks:
&#x20;         build: |
&#x20;           mix local.hex --force
&#x20;           mix local.rebar --force
&#x20;           mix do deps.get --only prod, deps.compile, compile

> [!NOTE]
>
> That build hook works for most cases and assumes that your `mix.exs` file is located at [your app root](https://developer.upsun.com/docs/configure-apps/app-reference/single-runtime-image.md#root-directory).

Assuming `mix.exs` is present at your app root and your build hook matches the above,
you can then start it from the `web.commands.start` directive.

The following basic app configuration is sufficient to run most Elixir applications.


&#x20;   applications:
&#x20;     myapp:
&#x20;       type: 'elixir:\{\{version:elixir:latest}}'

&#x20;       variables:
&#x20;         env:
&#x20;           MIX\_ENV: 'prod'

&#x20;       hooks:
&#x20;         build: |
&#x20;           mix local.hex --force
&#x20;           mix local.rebar --force
&#x20;           mix do deps.get --only prod, deps.compile, compile

&#x20;       web:
&#x20;         commands:
&#x20;           start: mix phx.server
&#x20;         locations:
&#x20;           /:
&#x20;             allow: false
&#x20;             passthru: true

Note that there is still an Nginx proxy server sitting in front of your application. If desired, certain paths may be served directly by Nginx without hitting your application (for static files, primarily) or you may route all requests to the Elixir application unconditionally, as in the example above.

## Dependencies

The recommended way to handle Elixir dependencies on Upsun Cloud is using Hex.
You can commit a `mix.exs` file in your repository and the system downloads the dependencies in your `deps` section using the build hook above.

```elixir theme={null}
  defp deps do
    [
	  {:platformshconfig, "~> 0.1.0"}
    ]
  end
```

## Accessing Services

You can access service credentials to connect to [managed services](https://developer.upsun.com/docs/add-services) from environment variables present in the application container.
Consult each of the individual service documentation to see how to retrieve and surface credentials into your application.

* [ClickHouse](https://developer.upsun.com/docs/add-services/clickhouse.md#usage-example)
* [Elasticsearch](https://developer.upsun.com/docs/add-services/elasticsearch.md#usage-example)
* [Gotenberg](https://developer.upsun.com/docs/add-services/gotenberg.md#usage-example)
* [Headless Chrome](https://developer.upsun.com/docs/add-services/headless-chrome.md#usage-example)
* [InfluxDB](https://developer.upsun.com/docs/add-services/influxdb.md#usage-example)
* [Kafka](https://developer.upsun.com/docs/add-services/kafka.md#usage-example)
* [MariaDB/MySQL](https://developer.upsun.com/docs/add-services/mysql#usage-example)
* [Memcached](https://developer.upsun.com/docs/add-services/memcached.md#usage-example)
* [mercure](https://developer.upsun.com/docs/add-services/mercure.md#usage-example)
* [mongodb](https://developer.upsun.com/docs/add-services/mongodb.md#usage-example)
* [Network Storage](https://developer.upsun.com/docs/add-services/network-storage.md#usage-example)
* [OpenSearch](https://developer.upsun.com/docs/add-services/opensearch.md#usage-example)
* [PostgreSQL](https://developer.upsun.com/docs/add-services/postgresql#usage-example)
* [RabbitMQ](https://developer.upsun.com/docs/add-services/rabbitmq.md#usage-example)
* [Redis](https://developer.upsun.com/docs/add-services/redis.md#usage-example)
* [Solr](https://developer.upsun.com/docs/add-services/solr.md#usage-example)
* [Valkey](https://developer.upsun.com/docs/add-services/valkey.md#usage-example)
* [Varnish](https://developer.upsun.com/docs/add-services/varnish.md#usage-example)
* [Vault KMS](https://developer.upsun.com/docs/add-services/vault.md#usage-example)
* [Chroma](https://developer.upsun.com/tutorials/self-hosted/chroma.md#3-use-the-relationship-in-your-application)
* [Qdrant](https://developer.upsun.com/tutorials/self-hosted/qdrant.md#4-use-the-relationship-in-your-application)

### Accessing Services Manually

The services configuration is available in the environment variable `PLATFORM_RELATIONSHIPS`.

Given a [relationship](https://developer.upsun.com/docs/configure-apps/image-properties/relationships.md) defined in `.upsun/config.yaml`:


&#x20;   applications:
&#x20;     myapp:
&#x20;       type: 'elixir:\{\{version:elixir:latest}}'

&#x20;       \[...]

&#x20;       \# Relationships enable an app container's access to a service.
&#x20;       \# The example below shows simplified configuration leveraging a default service
&#x20;       \# (identified from the relationship name) and a default endpoint.
&#x20;       \# See the Application reference for all options for defining relationships and endpoints.
&#x20;       relationships:
&#x20;         postgresql:

Assuming you have in `mix.exs` the Poison library to parse JSON:

```elixir theme={null}
  defp deps do
    [
      {:poison, "~> 3.0"}
    ]
  end
```

And assuming you use `ecto` you could put in `config/config.exs`:

```elixir theme={null}
relationships = Poison.decode!(Base.decode64!(System.get_env("PLATFORM_RELATIONSHIPS")))
[postgresql_config | _tail] = relationships["postgresql"]

config :my_app, Repo,
  database: postgresql_config["path"],
  username: postgresql_config["username"],
  password: postgresql_config["password"],
  hostname: postgresql_config["host"]
```

and setup Ecto during the deploy hook:

```yaml .upsun/config.yaml theme={null}
applications:
  myapp:
    hooks:
      deploy: |
        mix do ecto.setup
```

## View application instance details

To understand how an application's instances are distributed, you can view the instance details in the project's `/run/peers.json` file. To learn more, see [View application instance details](https://developer.upsun.com/docs/manage-resources/adjust-resources.md#view-application-instance-details) in the "Resource configuration" topic.


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