How to install Keycloak¶
Keycloak is an open-source identity and access management server. It gives your applications single sign-on, OAuth 2.0 / OpenID Connect, user federation (LDAP, Active Directory) and role management, so your apps don't have to store passwords themselves.
By the end of this guide you will have:
- Keycloak running locally
- A realm, a client and a user
- An access token returned by Keycloak
What you're setting up: apps send users to Keycloak to log in, and Keycloak stores everything in PostgreSQL.
Prerequisites¶
| Requirement | Version | Needed for |
|---|---|---|
| Docker or Podman | any recent | Options 1 and 2 |
| OpenJDK | 21 | Option 3 (Linux, no containers) |
| Free ports | 8080, 9000 |
HTTP and health endpoints |
Which option should I pick?
- Option 1 is the quickest way to try Keycloak. Data is lost when the container is removed.
- Option 2 keeps data in PostgreSQL and is the right base for a real server.
- Option 3 runs Keycloak directly on a Linux machine without containers.
Step 1: Install and start Keycloak¶
docker run --name keycloak -p 8080:8080 -p 9000:9000 \
-e KC_BOOTSTRAP_ADMIN_USERNAME=admin \
-e KC_BOOTSTRAP_ADMIN_PASSWORD=admin \
-e KC_HEALTH_ENABLED=true \
quay.io/keycloak/keycloak:26.4 start-dev
start-dev runs Keycloak in development mode: plain HTTP, relaxed settings and an embedded
database. Use it for learning and local testing only.
Create a folder with this compose.yaml:
services:
postgres:
image: postgres:17
environment:
POSTGRES_DB: keycloak
POSTGRES_USER: keycloak
POSTGRES_PASSWORD: change-me # (1)!
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U keycloak"]
interval: 5s
retries: 10
keycloak:
image: quay.io/keycloak/keycloak:26.4
command: start-dev # (2)!
environment:
KC_DB: postgres
KC_DB_URL: jdbc:postgresql://postgres:5432/keycloak
KC_DB_USERNAME: keycloak
KC_DB_PASSWORD: change-me
KC_BOOTSTRAP_ADMIN_USERNAME: admin
KC_BOOTSTRAP_ADMIN_PASSWORD: admin
KC_HEALTH_ENABLED: "true"
ports:
- "8080:8080"
- "9000:9000"
depends_on:
postgres:
condition: service_healthy
volumes:
pgdata:
- Use a strong password, ideally from a
.envfile that is not committed to git. - Switch to
startfor production, see Going to production.
Start both containers:
# 1. Java 21 (RHEL / Fedora shown; use apt on Debian / Ubuntu)
sudo dnf install -y java-21-openjdk
# 2. Download and extract Keycloak
KC_VERSION=26.4.0
curl -LO https://github.com/keycloak/keycloak/releases/download/${KC_VERSION}/keycloak-${KC_VERSION}.tar.gz
tar -xzf keycloak-${KC_VERSION}.tar.gz
cd keycloak-${KC_VERSION}
# 3. Start in development mode with a temporary admin user
export KC_BOOTSTRAP_ADMIN_USERNAME=admin
export KC_BOOTSTRAP_ADMIN_PASSWORD=admin
bin/kc.sh start-dev --health-enabled=true
Tip
Check the Keycloak releases page for the
latest version number and change KC_VERSION.
Wait for a log line similar to:
Step 2: Check that Keycloak is healthy¶
Expected output:
Step 3: Log in to the admin console¶
Open http://localhost:8080/admin and sign in with admin / admin.
Temporary admin
The bootstrap admin is meant to be temporary. After your first login, create a permanent
admin user in the master realm and delete the bootstrap one.
Step 4: Create a realm¶
A realm is an isolated space with its own users, clients and settings. Keep master for
administering Keycloak and create a separate realm for your applications.
- Click the realm dropdown at the top left (it shows master).
- Click Create realm.
- Set Realm name to
demoand click Create.
Step 5: Create a client¶
A client is an application that asks Keycloak to authenticate users.
- In the
demorealm, go to Clients → Create client. - Client type:
OpenID Connect, Client ID:demo-app, then Next. - Leave Client authentication off (a public client) and enable Direct access grants
so we can test with
curlin Step 7. Click Next. - Valid redirect URIs:
http://localhost:3000/*(your app's URL), then Save.
Note
Direct access grants (the password grant) are used here only to test quickly. Real web apps should use the Authorization Code flow with PKCE.
Step 6: Create a user¶
- Go to Users → Create new user.
- Username:
alice, fill in email, first and last name, then Create. - Open the Credentials tab → Set password, enter a password and turn Temporary off.
Step 7: Get an access token¶
curl -s -X POST http://localhost:8080/realms/demo/protocol/openid-connect/token \
-d grant_type=password \
-d client_id=demo-app \
-d username=alice \
-d password='your-password' | jq
You should get back an access_token, a refresh_token and an expires_in value. Paste the
access token into https://jwt.io to see its claims: iss, preferred_username, realm_access
and so on.
Done. Keycloak is running and issuing tokens for your realm.
Going to production¶
start-dev is not for production. For a real server, use start and configure a hostname and TLS:
Production checklist:
- Use PostgreSQL (or another supported database), never the embedded dev database
- Serve Keycloak over HTTPS only
- Replace the bootstrap admin with a permanent admin account
- Keep the management port
9000internal (health and metrics only) - Back up the database regularly
Troubleshooting¶
Port 8080 is already in use
Map a different host port, for example -p 8180:8080, and open http://localhost:8180.
HTTPS required error when logging in
The master realm requires HTTPS for non-local addresses. Use localhost while testing, or set
up TLS as shown in Going to production.
invalid_grant or unauthorized_client from the token endpoint
Check that Direct access grants is enabled on the client, the username and password are right, and the password is not marked Temporary.
Next steps¶
- How to secure a Spring Boot API with Keycloak
- Keycloak Laravel Auth for Laravel applications
- Official Keycloak server guides