Deploying Qwen Code CLI Assistant on Windows 11: A Practical Guide
This guide provides step-by-step instructions for installing and configuring the Qwen Code command-line AI programming assistant on Windows 11. It covers environment setup, API key configuration, environment variable management, and functionality verification, enabling developers to read, explain, and modify code directly from the terminal.
1. Scope and Prerequisites
This article is designed for developers on Windows 11 who want to use a command-line AI programming assistant to read code, explain code, execute commands, and modify files. The assistant itself runs in the local terminal, while reasoning is handled by a cloud-based large language model. Therefore, you need an active network connection and an API key from an LLM platform.
We will use Qwen Code as an example. It is an open-source command-line programming assistant published via npm that supports OpenAI-compatible interfaces. This guide uses the OpenAI-compatible interface from Alibaba Cloud Bailian as an example to illustrate how to configure the API key and model. The configuration process for other platforms is similar; only the endpoint URL and model name will differ.
During the operation, all commands are executed in PowerShell. It is recommended to open PowerShell as a regular user and only retry as an administrator if you encounter permission issues.
2. Environment Preparation
2.1 Check System Requirements
| Item | Requirement |
|---|---|
| Operating System | Windows 11 |
| Node.js | Version 20 or higher (based on the official Qwen Code README) |
| Disk Space | Approximately 5GB for Node.js, npm dependencies, and cache |
| Network | Access to the npm registry and the selected platform’s API endpoint |
2.2 Install Node.js
Visit the official Node.js website to download the LTS version Windows installer (.msi). Keep the default options during installation. After installation, close and reopen PowerShell, then run the following commands:
node -vnpm -v
Expected output will look similar to:
v20.18.010.8.2
Different version numbers are fine, as long as the Node.js major version is 20 or higher. If you receive a “node not recognized” error, it means the installer did not add it to the PATH. Try reopening the terminal, and if that fails, rerun the installer.
2.3 Confirm the npm Global Directory
Globally installed commands are placed in the npm global directory. Run the following command to view this directory:
npm config get prefix
With a default installation, the output is typically C:\Users\YourUsername\AppData\Roaming\npm. This directory must be in your user PATH; otherwise, you will encounter a “command not found” error after installation. For troubleshooting, see Section 7.
3. Install Qwen Code
Execute the global installation in PowerShell:
npm install -g @qwen-code/qwen-code@latest
Verify the version after installation:
qwen --version
The expected output is a single line with the version number, such as 0.x.x. If the installation process stalls or reports a network error, check your proxy settings (see Section 7, item 3), run npm cache clean --force to clear the cache, and retry. This command only clears the download cache and will not delete installed programs.
When you run qwen for the first time, the tool will generate a configuration directory in your user directory, typically located at C:\Users\YourUsername\.qwen. Configuration files and session records are saved here.
4. Obtain API Key and Select Model
4.1 Create an API Key
Log in to your chosen platform’s console and navigate to the API Key management page to create a new key. Copy and save it immediately, as most platforms will not display the full key again later.
4.2 Confirm API Endpoint and Model Name
The OpenAI-compatible API endpoint for Alibaba Cloud Bailian is:
https://dashscope.aliyuncs.com/compatible-mode/v1
Please refer to the model list in the console for the exact model name. This article uses qwen-turbo as an example of a lightweight model. If this name is deprecated or unavailable in your console, simply replace it with another model listed as lightweight.
4.3 Regarding Free Quotas
Some platforms offer free usage quotas for lightweight models. The amount, validity period, and rate limits of these quotas are subject to change, so this article does not guarantee zero-cost usage. Please verify the conditions on the platform’s official pricing or free quota page and note the date of verification. Regardless of whether the quota is sufficient, it is recommended to set up usage alerts in the console (see Section 8).
5. Configure Environment Variables
Qwen Code reads three environment variables for the OpenAI-compatible interface: OPENAI_API_KEY, OPENAI_BASE_URL, and OPENAI_MODEL. It is recommended to set these as user-level environment variables so that newly opened terminals can read them.
Set the key first. Using Read-Host to input the key prevents it from appearing in plain text in your PowerShell history:
[Environment]::SetEnvironmentVariable("OPENAI_API_KEY", (Read-Host "Enter API Key"), "User")
Next, set the API endpoint and model name:
[Environment]::SetEnvironmentVariable("OPENAI_BASE_URL", "https://dashscope.aliyuncs.com/compatible-mode/v1", "User")[Environment]::SetEnvironmentVariable("OPENAI_MODEL", "qwen-turbo", "User")
User-level environment variables only take effect in newly opened terminals. Close your current PowerShell window, open a new one, and check if the variables exist. The following command only shows whether the key is set and its length, without printing the key itself:
if ($env:OPENAI_API_KEY) { "Key is set, length $($env:OPENAI_API_KEY.Length)" } else { "Key is not set" }$env:OPENAI_BASE_URL$env:OPENAI_MODEL
Do not use echo $env:OPENAI_API_KEY or Write-Host to output the key. Terminal output, screen recordings, and screenshots can all lead to key leakage.
If you want to use a different model only in a specific project, you can write the variables into that project’s .env file, but ensure the file is not committed to the version control repository (see Section 8 for details).
6. Functionality Verification
6.1 Prepare a Test Project
First, create a test directory and a small file to verify read and modify capabilities:
New-Item -ItemType Directory -Force D:\projects\demo | Out-NullSet-Content -Path D:\projects\demo\hello.py -Value 'print("hello")'cd D:\projects\demogit initgit add .git commit -m "Initial version"
git init is used to retain a version before modification. If Git is not installed on your machine, you can skip the three git-related lines and manually copy hello.py as a backup.
6.2 Start the Assistant
Execute the following in the test directory:
qwen
Once in the interactive interface, complete the following four checks in order. Each has clear observation points and failure handling methods.
| Step | Prompt Input | Expected Outcome | Handling on Failure |
|---|---|---|---|
| 1. Connectivity | Briefly explain the purpose of the current directory in two sentences. | Returns a two-sentence explanation within a few seconds. | If 401 or 403, see Section 7, item 2; if timeout, see item 3. |
| 2. Read File | Read hello.py and explain its functionality. | The response points out that the file outputs “hello”. | Confirm that hello.py actually exists in the current directory. |
| 3. Execute Command | Execute the dir command and tell me how many files there are. | A confirmation prompt appears; after approval, it returns the file count. | If there is no confirmation prompt, check the approval mode settings to ensure commands require manual confirmation. |
| 4. Modify Code | Add a Chinese comment to the first line of hello.py indicating this is a test file. | Shows a modification preview (diff) and writes it after confirmation. | If it does not confirm before modifying, the file will not change; use git diff to check actual changes. |
After all four steps pass, exit the assistant (type /quit or press Ctrl+C), then run the following command to check the file contents:
Get-Content D:\projects\demo\hello.pygit diff
If you only see the added comment line and no other unrelated changes, the environment setup is successful.
7. Troubleshooting
1. “qwen not recognized” or command not found
This usually happens when the npm global directory is not added to the user PATH. First, run npm config get prefix to view the path, then append that path to the user PATH in PowerShell:
$p = [Environment]::GetEnvironmentVariable("Path", "User")$npmDir = npm config get prefix[Environment]::SetEnvironmentVariable("Path", "$p;$npmDir", "User")
Reopen the terminal after modifying and run qwen --version. If the directory is already included in the PATH, there is no need to add it again.
2. API call returns 401 or 403
Check in the following order:
- Run the check command from Section 5 in a new terminal to confirm that
OPENAI_API_KEYis set and has the expected length. A noticeably short length usually means characters were missed during copying. - Check for leading or trailing spaces or newlines in the key. Run
$env:OPENAI_API_KEY.Trim().Length -eq $env:OPENAI_API_KEY.Length. If the result isFalse, there are extra spaces or newlines that need to be removed.
