---
title: "Deploy Mem0 | The Memory Layer for Personalized AI"
description: "Self Host Mem0. Give your AI agents persistent memory"
category: "AI/ML"
url: https://railway.com/deploy/mem0
---

# Deploy Mem0 | The Memory Layer for Personalized AI

Self Host Mem0. Give your AI agents persistent memory

**[Deploy Mem0 | The Memory Layer for Personalized AI on Railway](https://railway.com/template/mem0)**

- **Creator:** Heimdall
- **Category:** AI/ML
- **Total deploys:** 30

## Template content

### Mem0 https://raw.githubusercontent.com/mem0ai/mem0/main/docs/logo/favicon.png

- **Image:** mem0/mem0-api-server:latest
- **Start command:** `/bin/sh -c 'pip install psycopg-binary && mkdir -p /app/history && alembic upgrade head && uvicorn main:app --host 0.0.0.0 --port 8000'`
- **Public domain:** Yes

### pgvector https://cdn.jsdelivr.net/gh/homarr-labs/dashboard-icons/svg/postgres.svg

- **Image:** pgvector/pgvector:pg16

## Documentation

![Mem0 logo](https://qdrant.tech/documentation/frameworks/mem0/mem0-banner.png)

# Deploy and Host Mem0 on Railway

Deploy Mem0 on Railway to get a production-ready memory layer for your AI agents and LLM-powered applications. Mem0 is an open-source REST API server that provides persistent, intelligent memory — it extracts facts, preferences, and relationships from conversations and retrieves them with vector similarity search.

Self-host Mem0 on Railway with this template that pre-configures the Mem0 API server (built from the `mem0ai/mem0` GitHub repo), a PostgreSQL database with pgvector for vector embeddings, API key authentication, and JWT-secured endpoints. Bring your own OpenAI API key and start storing memories immediately.

## Getting Started with Mem0 on Railway

After deployment completes, visit your Railway-generated public URL at `/docs` to access the Swagger UI with the full API reference. All protected endpoints require the `ADMIN_API_KEY` set in your Railway environment variables — pass it as a Bearer token in the Authorization header.

Store your first memory with a single API call:

```
curl -X POST https://your-app.up.railway.app/memories \
  -H "Authorization: Bearer $ADMIN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {"role": "user", "content": "I prefer Python over JavaScript and use Railway for all my deployments."}
    ],
    "user_id": "alex"
  }'
```

Mem0 automatically extracts structured facts from the conversation and stores them as vector embeddings. Search memories later with `GET /memories?user_id=alex&query=programming+preferences`. Add your `OPENAI_API_KEY` in Railway environment variables to enable LLM extraction and embedding generation.

## About Hosting Mem0

Mem0 is the memory layer for personalized AI. It sits between your application and your LLM, maintaining a persistent understanding of each user across sessions. Unlike raw conversation history, Mem0 compresses interactions into structured memories — extracting and updating facts, preferences, and relationships automatically.

Key features of self-hosted Mem0 include:

- **Automatic memory extraction** — feed conversations in, get structured facts out without manual tagging
- **Vector similarity search** — retrieve relevant memories using pgvector-powered semantic search
- **Multi-user and multi-agent** — a single Mem0 instance serves your entire product with isolated user memory spaces
- **REST API with OpenAPI docs** — full CRUD on memories at `/memories`, with Swagger UI at `/docs`
- **Token reduction** — up to 80% prompt token savings by replacing full chat history with compressed memory
- **Provider-agnostic** — works with OpenAI, configurable LLM and embedding models via environment variables

The template deploys two services: the Mem0 API server and a PostgreSQL database with pgvector for storing vector embeddings.

## Why Deploy Mem0 on Railway

- One-click deployment with PostgreSQL + pgvector pre-configured
- API key authentication and JWT security enabled by default
- Persistent vector memory survives container restarts via external database
- Scale API server and database resources independently
- No vendor lock-in — full data ownership with the open-source server

## Common Use Cases for Self-Hosted Mem0

- **Personalized AI assistants** — build chatbots that remember user preferences, past decisions, and context across sessions without bloating prompt windows
- **Multi-tenant SaaS memory** — add a persistent memory layer to your product where each user gets isolated, searchable memory
- **Agent tool memory** — give autonomous AI agents the ability to store and retrieve facts during multi-step workflows
- **Customer support automation** — create support bots that learn from ticket history and recall customer-specific context instantly

## Dependencies for Mem0 on Railway

- **Mem0 API Server** — built from `mem0ai/mem0` GitHub repo (`server/Dockerfile`) — the FastAPI application exposing the REST API
- **pgvector** — `pgvector/pgvector:pg16` — PostgreSQL with vector similarity search for memory embeddings

### Environment Variables Reference for Mem0

| Variable | Description | Default |
|---|---|---|
| `OPENAI_API_KEY` | OpenAI API key for LLM and embeddings | Must be set |
| `ADMIN_API_KEY` | API authentication key | Generated at deploy |
| `JWT_SECRET` | JWT token signing secret | Generated at deploy |
| `AUTH_DISABLED` | Disable API authentication | `false` |
| `MEM0_DEFAULT_LLM_MODEL` | LLM model for memory extraction | `gpt-4.1-nano-2025-04-14` |
| `MEM0_DEFAULT_EMBEDDER_MODEL` | Embedding model for vector search | `text-embedding-3-small` |
| `PORT` | HTTP server listening port | `8000` |

### Deployment Dependencies

- **Runtime:** Python 3.11+ (built from Dockerfile)
- **GitHub:** [mem0ai/mem0](https://github.com/mem0ai/mem0)
- **Docs:** [docs.mem0.ai](https://docs.mem0.ai)
- **API Reference:** [docs.mem0.ai/open-source/features/rest-api](https://docs.mem0.ai/open-source/features/rest-api)

## Hardware Requirements for Self-Hosting Mem0

| Resource | Minimum | Recommended |
|---|---|---|
| CPU | 1 vCPU | 2 vCPU |
| RAM | 512 MB | 1 GB |
| Storage | 1 GB (app) + 1 GB (DB) | 5 GB+ (DB grows with stored memories) |
| Runtime | Docker | Docker |

Mem0's API server is lightweight — resource consumption scales with the number of concurrent requests and the volume of stored memories. The pgvector database handles vector similarity search and grows with your memory corpus.

## Self-Hosting Mem0 with Docker

Run Mem0 locally with Docker Compose and an external PostgreSQL database:

```
services:
  pgvector:
    image: pgvector/pgvector:pg16
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: changeme
      POSTGRES_DB: postgres
      PGDATA: /var/lib/postgresql/data/pgdata
    volumes:
      - pgdata:/var/lib/postgresql/data

  mem0:
    build:
      context: https://github.com/mem0ai/mem0.git
      dockerfile: server/Dockerfile
    ports:
      - "8000:8000"
    environment:
      POSTGRES_HOST: pgvector
      POSTGRES_PORT: "5432"
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: changeme
      POSTGRES_DB: postgres
      OPENAI_API_KEY: sk-your-openai-api-key
      ADMIN_API_KEY: your-admin-key
      JWT_SECRET: your-jwt-secret
      PORT: "8000"
    depends_on:
      - pgvector

volumes:
  pgdata:
```

Or use the Python SDK to connect to your self-hosted instance programmatically:

```
from mem0 import MemoryClient

client = MemoryClient(
    api_key="your-admin-api-key",
    host="https://your-app.up.railway.app"
)

# Store a memory
client.add(
    messages=[{"role": "user", "content": "My favorite framework is FastAPI."}],
    user_id="alex"
)

# Search memories
results = client.search("What frameworks does Alex use?", user_id="alex")
```

## How Much Does Mem0 Cost to Self-Host?

Mem0's server is fully open-source under the Apache 2.0 license with no licensing fees. Self-hosting on Railway costs approximately $5-7/month for basic usage, covering compute and database storage. You pay separately for OpenAI API usage (LLM extraction and embeddings). Mem0 Cloud offers managed hosting starting free for 10K memories, but self-hosting gives you unlimited memories, full data ownership, and no per-request fees to Mem0.

## FAQ

**How do I authenticate API requests to self-hosted Mem0?**
Set `AUTH_DISABLED=false` (the default) and `ADMIN_API_KEY` in your environment variables. All API requests must include the key as a Bearer token: `Authorization: Bearer your-admin-api-key`. The `/docs` Swagger UI endpoint is accessible without authentication for API exploration.

**Why does this template build Mem0 from GitHub instead of using the Docker Hub image?**
The `mem0/mem0-api-server` image on Docker Hub is arm64-only, which is incompatible with Railway's amd64 infrastructure. Building from the `mem0ai/mem0` GitHub repository (`server/Dockerfile`) produces an amd64-compatible image that runs on Railway.

**What LLM providers work with self-hosted Mem0 on Railway?**
Mem0 uses OpenAI-compatible APIs for both LLM extraction and embedding generation. Set `OPENAI_API_KEY` for OpenAI models. You can configure the specific models via `MEM0_DEFAULT_LLM_MODEL` and `MEM0_DEFAULT_EMBEDDER_MODEL` environment variables. Any OpenAI-compatible endpoint works.

**Does the self-hosted Mem0 API use the same endpoints as Mem0 Cloud?**
No. The self-hosted OSS server does not use the `/v1/` prefix. Use `POST /memories` instead of `POST /v1/memories/`. The hosted platform at `api.mem0.ai` uses `/v1/` paths — those do not apply to the open-source server.

**How does Mem0 differ from just storing chat history in a database?**
Mem0 automatically extracts structured facts from conversations using an LLM, stores them as vector embeddings, and retrieves relevant memories via semantic search. Raw chat history grows linearly and requires full context windows. Mem0 compresses information, deduplicates facts, and retrieves only what is relevant — reducing prompt tokens by up to 80%.

**Can I use Mem0 on Railway with multiple AI agents or users?**
Yes. Mem0 supports multi-user and multi-agent isolation natively. Pass `user_id` or `agent_id` with each memory operation to partition memory spaces. A single Mem0 instance can serve your entire product without cross-contamination between users.


## Similar templates

- [Chat Chat](https://railway.com/deploy/-WWW5r) — Chat Chat, your own unified chat and search to AI platform.
- [stella](https://railway.com/deploy/stella) — Self-host stella with web, API, Postgres, Redis, and object storage.
- [Hermes Agent | OpenClaw Alternative with Dashboard](https://railway.com/deploy/hermes-agent-or-openclaw-alternative-wit) — Self-Hosted Hermes AI Agent for Telegram, Discord & Slack

Open this page in a browser: https://railway.com/deploy/mem0
