Skip to content

The legion command line

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

On this page

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:

Shell
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 (opens in a new tab), unzip it and put legion.exe in a folder on your PATH.

Check it works:

Shell
legion version

Log in

Shell
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 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:

Shell
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:

Shell
echo "$LEGION_API_KEY" | legion auth login --with-token
Shell
export LEGION_API_KEY=YOUR_LEGION_API_KEY

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

Quickstart

1. Find a GPU

Shell
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

Shell
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

Shell
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

Shell
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.

5. Delete it

Shell
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:

Shell
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:

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

fish:

Shell
legion completion fish > ~/.config/fish/completions/legion.fish

Next steps