Sub topic 2
Installing the Databricks CLI and authenticating
Every bundle command (validate, deploy, run, destroy) is a call to the Databricks CLI, and each call needs to prove who you are to a workspace. This chapter covers installing the CLI, creating a login, and checking which identity a command will use.
Prerequisites
This chapter assumes you:
- have a Databricks workspace you can sign in to, with permission to create jobs
- are comfortable in a terminal
- can read INI-style config files and YAML
- know what a workspace URL looks like, such as
https://<your-dev-workspace-url>
You do not need a bundle yet. The next chapters add databricks.yml.
Which CLI you need
Databricks has shipped two command-line tools. The legacy one is a Python package installed with pip (databricks-cli, versions 0.18 and earlier). It does not support bundles. The current CLI is a single Go binary with versions 0.205 and later. Bundles need the current one.
If you have both installed, the wrong one may come first on your PATH. The version check below catches this.
Installing the CLI
Pick the method that matches your operating system.
| Platform | Command | Column |
|---|---|---|
| macOS or Linux (Homebrew) | `brew tap databricks/tap` then `brew install databricks` | |
| Windows (WinGet) | `winget install Databricks.DatabricksCLI` | |
| Windows (Chocolatey) | `choco install databricks-cli` | |
| macOS, Linux, or Windows WSL (script) | `curl -fsSL https://raw.githubusercontent.com/databricks/setup-cli/main/install.sh \ | sh` |
Package managers are easier to upgrade later. The install script is useful in containers and CI runners, where you do not have Homebrew. You can also download a release archive from the databricks/cli GitHub releases page and put the binary on your PATH.
Verify the install:
# Print the installed version; it should be 0.205.0 or higher
databricks -v
# Confirm which binary is first on your PATH
which databricksIf the version is below 0.205, or the output looks like the old Python tool, remove the old package (pip uninstall databricks-cli) and reinstall using one of the methods above.
How the CLI decides who you are
The CLI uses unified authentication. Every command goes through the same resolution process, whichever Databricks tool you use. It collects three kinds of input:
- The host: which workspace to talk to.
- The credential: an OAuth token, a client ID and secret, or a personal access token.
- The source: command-line flags, environment variables, a bundle's
workspacesettings, or a profile in~/.databrickscfg.
You will mostly use three authentication types.
| Type | Use it for | Credential |
|---|---|---|
| OAuth user-to-machine (U2M) | You, working interactively | Browser sign-in, short-lived token cached locally |
| OAuth machine-to-machine (M2M) | CI/CD and scheduled automation | Service principal client ID and secret |
| Personal access token (PAT) | Older setups, quick tests | A long-lived token tied to your user |
Prefer OAuth. OAuth tokens expire quickly and are refreshed automatically. A PAT works until it expires or is revoked, so a leaked one stays dangerous for a long time.
Signing in interactively
For day-to-day development, use OAuth U2M:
# Opens a browser, asks you to sign in, and saves a profile
databricks auth login --host https://<your-dev-workspace-url>The CLI asks for a profile name. Accept the suggested name or type your own, such as dev. After you approve the sign-in in the browser, the CLI writes a profile to ~/.databrickscfg (on Windows, %USERPROFILE%\.databrickscfg). It also caches the OAuth token in a separate location under ~/.databricks/.
Check that it worked:
# List profiles the CLI knows about and whether each one is valid
databricks auth profiles
# Ask the workspace who you are, using a specific profile
databricks current-user me --profile devcurrent-user me is a good first command to run. It is read-only, it needs no extra permissions, and it prints the user name the workspace sees.
Profiles
A profile is a named section in ~/.databrickscfg. After the login above, the file looks like this:
[dev]
host = https://<your-dev-workspace-url>
auth_type = databricks-cliThe line auth_type = databricks-cli tells the CLI to use the cached OAuth login. No secret is stored in this file.
You can keep one profile per workspace and select one in several ways:
--profile devor-p devon a single commandDATABRICKS_CONFIG_PROFILE=devin the environment- The
[DEFAULT]profile, used when nothing else is specified
A profile named DEFAULT is the fallback. If you work with several workspaces, avoid relying on it, because a command run in the wrong terminal then goes to the wrong workspace. Give each workspace an explicit name and pass it.
Authenticating as a service principal
A service principal is a non-human identity in Databricks. Automation should use one, because a pipeline tied to your personal account stops working when you leave the team or your access changes.
For OAuth M2M, an admin creates the service principal and generates an OAuth secret for it. You get two values: the client ID (also called the application ID) and the client secret. The client secret is shown once, so store it in a secrets manager right away.
You can supply them as environment variables, which is the usual method in CI:
export DATABRICKS_HOST="https://<your-prod-workspace-url>"
export DATABRICKS_CLIENT_ID="<service-principal-application-id>"
export DATABRICKS_CLIENT_SECRET="<oauth-secret-from-your-secret-store>"
# Confirm the identity; the output should name the service principal
databricks current-user meYou can also put them in a profile for local testing. In that case the file contains a secret, so treat it with the care from the warning above:
[prod-sp]
host = https://<your-prod-workspace-url>
client_id = <service-principal-application-id>
client_secret = <oauth-secret>Select it with --profile prod-sp.
Personal access tokens
A PAT is a single string you generate in the workspace user settings. You pass it as DATABRICKS_TOKEN, or you store it in a profile with host and token keys:
export DATABRICKS_HOST="https://<your-dev-workspace-url>"
export DATABRICKS_TOKEN="<personal-access-token>"
databricks current-user meThis works, but it has real downsides. The token acts as you with your full permissions, it often outlives its purpose, and it is easy to paste into a script by accident. Use it only when OAuth is not available to you.
Precedence and how bundles pick a workspace
Several sources can name a host or a credential at once, and a mix of them is the most common cause of confusing authentication errors. In general, the CLI resolves them in this order:
- Values set directly in a bundle's configuration, such as
workspace.hostandworkspace.profile - Command-line flags such as
--profile - Environment variables such as
DATABRICKS_HOST,DATABRICKS_TOKEN, andDATABRICKS_CONFIG_PROFILE - The profile in
~/.databrickscfg
Treat this order as a guide, not a guarantee. Details differ between CLI versions and between commands, so confirm with databricks auth env or by running a read-only command when the result matters.
For bundles, the important points are:
- Each target in
databricks.ymlnames aworkspace.host. When you rundatabricks bundle deploy -t prod, the CLI looks for a profile in~/.databrickscfgwhose host matches that URL. - If more than one profile has that host, the CLI cannot choose. Pass
-p <profile>or setworkspace.profileon the target. - If the host in a profile and the host in the bundle differ, you get an error. This protects you from deploying to a workspace you did not intend.
A bundle target can name its profile explicitly. This example extends the prod target from the topic's databricks.yml:
targets:
prod:
mode: production
workspace:
host: https://<your-prod-workspace-url>
profile: prod-spNaming a profile in a bundle file ties the file to local configuration that teammates may not have. A common compromise is to leave profile out of databricks.yml, have each developer pick their profile with -p, and have CI use environment variables.
Inspecting what the CLI is using
When a command talks to the wrong workspace or fails with a 401 or 403 error, check these in order:
# Which profiles exist, and are they valid?
databricks auth profiles
# What host and credential settings does this profile resolve to?
databricks auth env --profile dev
# Which user does the workspace think you are?
databricks current-user me --profile devauth env prints values the CLI will use, and it may include secrets, so do not paste its output into a ticket or chat without removing them.
Also check the environment for variables left over from earlier work:
env | grep '^DATABRICKS_' | sed 's/=.*/=<hidden>/'That command lists which variables are set without printing their values.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `command not found: databricks` | The binary is not on `PATH` | Reinstall, or add its directory to `PATH` |
| Old-style help text, no `bundle` command | Legacy Python CLI is first on `PATH` | Uninstall it, confirm with `databricks -v` |
| 401 Unauthorized | Expired or revoked token, or wrong secret | Run `databricks auth login` again, or rotate the secret |
| 403 Forbidden | Identity is valid but lacks permission | Ask an admin to grant access to the workspace or resource |
| "multiple profiles matched" | Two profiles share one host | Pass `-p`, or set `workspace.profile` in the target |
| Deploys go to the wrong workspace | `DATABRICKS_HOST` or the `DEFAULT` profile overrides your intent | Unset the variable, name the profile explicitly |
| Browser sign-in loop | Stale cached token | Run `databricks auth logout --profile dev`, then log in again |
Practice
Run through the checklist once before moving on, so the next chapters are not blocked by a login problem:
- Install the CLI and confirm
databricks -vprints 0.205.0 or higher. - Run
databricks auth login --host <your workspace URL>and name the profiledev. - Run
databricks auth profilesand confirmdevshows as valid. - Run
databricks current-user me -p devand confirm the user name is yours.
Exercise
You run databricks bundle deploy -t prod on your laptop. Your ~/.databrickscfg has two profiles, prod-a and prod-b, and both point to the production workspace host named in the bundle. The CLI reports that it cannot choose a profile. Give two ways to fix this.
Show answer
Pass the profile explicitly with -p prod-a (or --profile prod-a) on the command, or set workspace.profile: prod-a on the prod target in databricks.yml. Setting the DATABRICKS_CONFIG_PROFILE environment variable also works. Removing the duplicate profile is a third option.
Exercise
A teammate wants a nightly pipeline to deploy a bundle. They plan to generate a personal access token from their own account and store it as a CI secret. Name two problems with this plan and what to use instead.
Show answer
The token acts with the teammate's full permissions, and the pipeline breaks if their access changes or they leave the team. A long-lived token is also a larger risk if it leaks. Use a service principal with OAuth M2M: set DATABRICKS_HOST, DATABRICKS_CLIENT_ID, and DATABRICKS_CLIENT_SECRET from the CI secret store.