Set up Ollama in the AppHost
This article is the reference for the Aspire Ollama hosting integration from the Aspire Community Toolkit. It enumerates the AppHost APIs — with examples for both AppHost.cs and apphost.mts — that you use to model an Ollama server and its model resources in your AppHost project.
If you’re new to the Ollama integration, start with the Get started with Ollama integrations guide. For how consuming apps read the connection information this page exposes, see Connect to Ollama.
Installation
Section titled “Installation”To start building an Aspire app that uses Ollama, install the 📦 CommunityToolkit.Aspire.Hosting.Ollama NuGet package:
aspire add ollama --source CommunityToolkitLearn more about aspire add in the command reference.
Or, choose a manual installation approach:
#:package CommunityToolkit.Aspire.Hosting.Ollama@*<PackageReference Include="CommunityToolkit.Aspire.Hosting.Ollama" Version="*" />aspire add ollama --source CommunityToolkitLearn more about aspire add in the command reference.
This updates your aspire.config.json with the Ollama hosting integration package:
{ "packages": { "CommunityToolkit.Aspire.Hosting.Ollama": "*" }}Add Ollama resource
Section titled “Add Ollama resource”Once you’ve installed the hosting integration in your AppHost project, you can add an Ollama server resource and then add model resources as shown in the following examples:
var builder = DistributedApplication.CreateBuilder(args);
var ollama = builder.AddOllama("ollama");var llama3 = ollama.AddModel("llama3");
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice") .WithReference(llama3);
// After adding all resources, run the app...import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.aspire/modules/aspire.mjs';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const ollama: OllamaResource
ollama = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addOllama(name: string, options?: { port?: number;}): OllamaResource (+1 overload)
Adds an Ollama container resource to the application model.
addOllama("ollama");const const llama3: OllamaModelResource
llama3 = await const ollama: OllamaResource
ollama.OllamaResource.addModel(modelName: string): OllamaModelResource
Adds a model to the Ollama resource.
addModel("llama3");
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource
Adds a node application to the application model. Node should be available on the PATH.
addNodeApp("api", "./api", "index.js") .ExecutableResource.withReference(source: EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): NodeAppResource (+1 overload)
Adds a reference to another resource
withReference(const llama3: OllamaModelResource
llama3);
// After adding all resources, run the app...-
When Aspire adds a container image to the AppHost, as shown in the preceding example with the
docker.io/ollama/ollamaimage, it creates a new Ollama instance on your local machine. -
The resource name passed to
AddOllamais used as the connection string name when referenced in a dependency. Model sub-resources are named by sanitizing the model name (for example,"llama3"produces the resource name"ollama-llama3"). -
The AppHost reference call configures a connection in the consuming project named after the referenced model resource.
Add Ollama model resource
Section titled “Add Ollama model resource”Models are sub-resources of an Ollama server resource. You add them with the AddModel (or addModel) method. Each model is downloaded when the Ollama container first starts.
var builder = DistributedApplication.CreateBuilder(args);
var ollama = builder.AddOllama("ollama");
// Add by model name — resource name is generated automaticallyvar phi35 = ollama.AddModel("phi3.5");
// Add with an explicit resource namevar llama3 = ollama.AddModel("ollama-llama3", "llama3");
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice") .WithReference(phi35) .WithReference(llama3);
// After adding all resources, run the app...import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.aspire/modules/aspire.mjs';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const ollama: OllamaResource
ollama = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addOllama(name: string, options?: { port?: number;}): OllamaResource (+1 overload)
Adds an Ollama container resource to the application model.
addOllama("ollama");
// Add by model name — resource name is generated automaticallyconst const phi35: OllamaModelResource
phi35 = await const ollama: OllamaResource
ollama.OllamaResource.addModel(modelName: string): OllamaModelResource
Adds a model to the Ollama resource.
addModel("phi3.5");
// Add with an explicit resource nameconst const llama3: OllamaModelResource
llama3 = await const ollama: OllamaResource
ollama.OllamaResource.addNamedModel(name: string, modelName: string): OllamaModelResource
Adds a model to the Ollama resource.
addNamedModel("ollama-llama3", "llama3");
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource
Adds a node application to the application model. Node should be available on the PATH.
addNodeApp("api", "./api", "index.js") .ExecutableResource.withReference(source: EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): NodeAppResource (+1 overload)
Adds a reference to another resource
withReference(const phi35: OllamaModelResource
phi35) .ExecutableResource.withReference(source: EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): NodeAppResource (+1 overload)
Adds a reference to another resource
withReference(const llama3: OllamaModelResource
llama3);
// After adding all resources, run the app...Download the LLM
Section titled “Download the LLM”When the Ollama container for this integration first spins up, it downloads the configured LLMs. The progress of this download displays in the State column for this integration on the Aspire dashboard.
Add Ollama resource with data volume
Section titled “Add Ollama resource with data volume”Add a data volume to the Ollama resource to persist downloaded models across container restarts:
var builder = DistributedApplication.CreateBuilder(args);
var ollama = builder.AddOllama("ollama") .WithDataVolume();
var llama3 = ollama.AddModel("llama3");
var exampleProject = builder.AddProject<Projects.ExampleProject>() .WithReference(llama3);
// After adding all resources, run the app...import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.aspire/modules/aspire.mjs';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const ollama: OllamaResource
ollama = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addOllama(name: string, options?: { port?: number;}): OllamaResource (+1 overload)
Adds an Ollama container resource to the application model.
addOllama("ollama");await const ollama: OllamaResource
ollama.OllamaResource.withDataVolume(options?: { name?: string; isReadOnly?: boolean;} | undefined): OllamaResource (+1 overload)
Adds a data volume to the Ollama container.
withDataVolume();
const const llama3: OllamaModelResource
llama3 = await const ollama: OllamaResource
ollama.OllamaResource.addModel(modelName: string): OllamaModelResource
Adds a model to the Ollama resource.
addModel("llama3");
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource
Adds a node application to the application model. Node should be available on the PATH.
addNodeApp("api", "./api", "index.js") .ExecutableResource.withReference(source: EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): NodeAppResource (+1 overload)
Adds a reference to another resource
withReference(const llama3: OllamaModelResource
llama3);
// After adding all resources, run the app...The data volume is mounted at /root/.ollama in the Ollama container. When a name parameter isn’t provided, the volume name is generated automatically. For more information on data volumes and details on why they’re preferred over bind mounts, see Docker docs: Volumes.
Use GPUs when available
Section titled “Use GPUs when available”By default, the Ollama container runs on CPU. To enable GPU acceleration, use the WithGPUSupport (or withGPUSupport) extension method:
Nvidia:
var builder = DistributedApplication.CreateBuilder(args);
var ollama = builder.AddOllama("ollama") .WithGPUSupport();
ollama.AddModel("llama3");
// After adding all resources, run the app...AMD:
var builder = DistributedApplication.CreateBuilder(args);
var ollama = builder.AddOllama("ollama") .WithGPUSupport(OllamaGpuVendor.AMD);
ollama.AddModel("llama3");
// After adding all resources, run the app...Nvidia:
import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.aspire/modules/aspire.mjs';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const ollama: OllamaResource
ollama = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addOllama(name: string, options?: { port?: number;}): OllamaResource (+1 overload)
Adds an Ollama container resource to the application model.
addOllama("ollama");await const ollama: OllamaResource
ollama.OllamaResource.withGPUSupport(options?: { vendor?: OllamaGpuVendor;} | undefined): OllamaResource (+1 overload)
Adds GPU support to the Ollama container.
withGPUSupport();
await const ollama: OllamaResource
ollama.OllamaResource.addModel(modelName: string): OllamaModelResource
Adds a model to the Ollama resource.
addModel("llama3");
// After adding all resources, run the app...AMD:
import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder, type OllamaGpuVendor = "Nvidia" | "AMD"const OllamaGpuVendor: { readonly Nvidia: "Nvidia"; readonly AMD: "AMD";}
Enum Aspire.Hosting.OllamaGpuVendor
OllamaGpuVendor } from './.aspire/modules/aspire.mjs';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const ollama: OllamaResource
ollama = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addOllama(name: string, options?: { port?: number;}): OllamaResource (+1 overload)
Adds an Ollama container resource to the application model.
addOllama("ollama");await const ollama: OllamaResource
ollama.OllamaResource.withGPUSupport(options?: { vendor?: OllamaGpuVendor;} | undefined): OllamaResource (+1 overload)
Adds GPU support to the Ollama container.
withGPUSupport({ vendor?: OllamaGpuVendor | undefined
vendor: const OllamaGpuVendor: { readonly Nvidia: "Nvidia"; readonly AMD: "AMD";}
Enum Aspire.Hosting.OllamaGpuVendor
OllamaGpuVendor.type AMD: "AMD"
AMD });
await const ollama: OllamaResource
ollama.OllamaResource.addModel(modelName: string): OllamaModelResource
Adds a model to the Ollama resource.
addModel("llama3");
// After adding all resources, run the app...For more information, see GPU support in Docker Desktop and GPU support in Podman.
Add a model from Hugging Face
Section titled “Add a model from Hugging Face”To use a model in GGUF format from Hugging Face, use the AddHuggingFaceModel (or addHuggingFaceModel) extension method:
var builder = DistributedApplication.CreateBuilder(args);
var ollama = builder.AddOllama("ollama");
var llama = ollama.AddHuggingFaceModel( "llama", "bartowski/Llama-3.2-1B-Instruct-GGUF:IQ4_XS");
var exampleProject = builder.AddProject<Projects.ExampleProject>() .WithReference(llama);
// After adding all resources, run the app...import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.aspire/modules/aspire.mjs';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const ollama: OllamaResource
ollama = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addOllama(name: string, options?: { port?: number;}): OllamaResource (+1 overload)
Adds an Ollama container resource to the application model.
addOllama("ollama");
const const llama: OllamaModelResource
llama = await const ollama: OllamaResource
ollama.OllamaResource.addHuggingFaceModel(name: string, modelName: string): OllamaModelResource
Adds a model from Hugging Face to the Ollama resource. Only models in GGUF format are supported.
addHuggingFaceModel( "llama", "bartowski/Llama-3.2-1B-Instruct-GGUF:IQ4_XS");
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource
Adds a node application to the application model. Node should be available on the PATH.
addNodeApp("api", "./api", "index.js") .ExecutableResource.withReference(source: EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): NodeAppResource (+1 overload)
Adds a reference to another resource
withReference(const llama: OllamaModelResource
llama);
// After adding all resources, run the app...Only models in GGUF format are supported. The integration automatically prefixes hf.co/ to the model name if it doesn’t already start with hf.co/ or huggingface.co/, so Ollama can resolve it from Hugging Face.
Add Ollama resource with parameters
Section titled “Add Ollama resource with parameters”To use a fixed port for the Ollama container, pass it as a parameter:
var builder = DistributedApplication.CreateBuilder(args);
var ollama = builder.AddOllama("ollama", port: 11434);
ollama.AddModel("llama3");
// After adding all resources, run the app...import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.aspire/modules/aspire.mjs';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const ollama: OllamaResource
ollama = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addOllama(name: string, options?: { port?: number;}): OllamaResource (+1 overload)
Adds an Ollama container resource to the application model.
addOllama("ollama", { port?: number | undefined
port: 11434 });
await const ollama: OllamaResource
ollama.OllamaResource.addModel(modelName: string): OllamaModelResource
Adds a model to the Ollama resource.
addModel("llama3");
// After adding all resources, run the app...Open WebUI support
Section titled “Open WebUI support”The Ollama integration also provides support for running Open WebUI alongside the Ollama container:
var builder = DistributedApplication.CreateBuilder(args);
var ollama = builder.AddOllama("ollama") .WithOpenWebUI();
ollama.AddModel("llama3");
// After adding all resources, run the app...import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from "./.aspire/modules/aspire.mjs";
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const ollama: OllamaResource
ollama = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addOllama(name: string, options?: { port?: number;}): OllamaResource (+1 overload)
Adds an Ollama container resource to the application model.
addOllama("ollama");await const ollama: OllamaResource
ollama.OllamaResource.withOpenWebUI(options?: { configureContainer?: ((obj: OpenWebUIResource) => Promise<void>) | undefined; containerName?: string;} | undefined): OllamaResource (+1 overload)
Adds an Open WebUI container to the application model for administering Ollama. This version of the package defaults to the main tag of the Open WebUI container image.
withOpenWebUI();
await const ollama: OllamaResource
ollama.OllamaResource.addModel(modelName: string): OllamaModelResource
Adds a model to the Ollama resource.
addModel("llama3");
// After adding all resources, run the app...Connection properties
Section titled “Connection properties”For the full reference of Ollama connection properties — and how consuming apps in C#, TypeScript, Python, and Go read them — see Connect to Ollama.
Hosting integration health checks
Section titled “Hosting integration health checks”The Ollama hosting integration automatically adds health checks for both the Ollama server and each model resource:
- Server health check. Verifies that the Ollama server is running and that a connection can be established to it.
- Model health check. Verifies that a model has been downloaded and is available. The model resource is marked as unhealthy until the download completes.