# The legion command line

> Install the legion CLI, log in, and rent a GPU, SSH in and stop billing from your terminal.

Source: https://legion.ambient.xyz/docs/cli

After this page `legion` is installed and logged in, and you have rented a GPU, connected to it and stopped it, all from your terminal. The CLI uses the same API as the app, so everything you do shows up there too.

## Install

On macOS and Linux:

```sh
curl -fsSL https://raw.githubusercontent.com/AmbientCrypto/legion-cli/main/scripts/install.sh | bash
```

The script picks the build for your system, checks its checksum and installs `legion` to `~/.local/bin`. Make sure that folder is on your `PATH`. Set `LEGION_INSTALL_DIR` to install elsewhere, or `LEGION_VERSION` to pin a release. Run the same command again to upgrade.

On Windows, download the `.zip` from the [releases page](https://github.com/AmbientCrypto/legion-cli/releases), unzip it and put `legion.exe` in a folder on your `PATH`.

Check it works:

```sh
legion version
```

## Log in

```sh
legion auth login
```

This opens Legion's sign-in page in your browser. Sign in, approve **Legion CLI**, and legion saves the API key the approval creates: scope **full** (**Full access** in the app), named `Legion CLI (<this computer>)`, valid for 90 days. It is listed under **Settings**, then **API keys**, where you can revoke it. `legion auth logout` revokes it too. See [Authentication](https://legion.ambient.xyz/docs/api/authentication) for what keys are.

The browser must run on the same computer as legion, because the sign-in returns to a port on `127.0.0.1`. Add `--no-browser` to print the sign-in URL instead of opening a browser.

On a remote machine, use `--paste`. Create a key under **Settings**, then **API keys**, and paste it at the hidden prompt:

```sh
legion auth login --paste
```

To check who you are logged in as, run `legion whoami`.

In CI, skip the sign-in. Pipe a key in, or set an environment variable:

```sh
echo "$LEGION_API_KEY" | legion auth login --with-token
```

```sh
export LEGION_API_KEY=YOUR_LEGION_API_KEY
```

With that set, you don't need to log in.

## Quickstart

### 1. Find a GPU

```sh
legion offer list --gpu-model "RTX 4090" --max-price 2.50
```

Offers are listed cheapest first, with prices per GPU-hour. Add `-o wide` for more columns. `legion catalog list` shows the starter images and premade models you can run.

### 2. Rent it

```sh
legion rental create --offer amber-falcon --catalog pytorch --gpus 2
```

legion shows a preview first: host, GPUs, price per GPU-hour and per hour, your balance, image and SSH key. It asks you to confirm, then waits until the rental is running. Billing starts then.

- `--dry-run` shows the preview and stops, so nothing is rented.
- `--async` returns as soon as the rental is created.
- The price you saw is the most you will pay. If the host's price rises before you confirm, nothing is rented and legion tells you the new price. Use `--max-price` to allow more.

### 3. Connect

```sh
legion ssh quiet-river-7k2f
legion ssh quiet-river-7k2f -- nvidia-smi
```

The first opens a shell as root. The second runs one command and returns. A rental is named by its id, its friendly name or any unique prefix. On a terminal you can leave the name out to pick from a list.

### 4. Stop it

```sh
legion rental stop quiet-river-7k2f
```

This ends GPU billing and keeps your workspace. Stored files are billed per GiB until you delete the rental. See [Stopping, backups and how long your data is kept](https://legion.ambient.xyz/docs/renting/stopping-and-backups).

### 5. Delete it

```sh
legion rental delete quiet-river-7k2f
```

This destroys the workspace for good, so back up first if you need the files: `legion rental backup create`. On a terminal you type the rental's name to confirm.

## Shell completion

Completion suggests commands and flags, and also your live rental names, host names, catalog ids, saved SSH key labels and GPU models.

**bash**, in `~/.bashrc`:

```sh
source <(legion completion bash)
```

**zsh**. Write the script to a folder on your `fpath`, then restart the shell. If completion isn't on yet, enable it first:

```sh
echo "autoload -U compinit; compinit" >> ~/.zshrc
legion completion zsh > "${fpath[1]}/_legion"
```

**fish**:

```sh
legion completion fish > ~/.config/fish/completions/legion.fish
```

## Next steps

- [Scripting and automation](https://legion.ambient.xyz/docs/cli/scripting): output formats, exit codes, environment variables and profiles.
- [Command reference](https://legion.ambient.xyz/docs/cli/commands): every command, with a link to its full flags.
