Setting Up a Command-Line AI Tool and Connecting LLMs on Ubuntu
Learn how to install a Node.js-based command-line AI tool on Ubuntu, fix common npm errors like ENOTEMPTY, connect OpenAI-compatible LLM providers through a configuration wizard, and secure your API keys with best practices for permissions and environment variables.
1. Use Cases and Preparation
Command-line AI tools don’t depend on a graphical interface, which makes them a good fit for shell scripts, automation pipelines, and remote servers. This guide walks through six steps: checking your Node.js environment, configuring npm’s global directory, installing the tool, handling errors caused by leftover directories, running the configuration wizard to connect an LLM provider, and finally hardening security.
This article uses aicli as the example command name and @example/aicli as the example npm package name. When following along, replace both with the actual package name and command name from your tool’s official documentation.
1.1 Check Node.js and npm
First confirm both are installed:
node --versionnpm --version
Most Node.js-based CLI tools require Node.js 18 or higher. The nodejs package in Ubuntu’s repositories may be outdated, so it’s recommended to install Node.js 20 LTS with nvm. Check the nvm GitHub repository for the latest installation script and version number, then run:
nvm install 20nvm use 20node --version
If you don’t have nvm installed yet, follow its official documentation first. Avoid piping an installation script directly into bash without reviewing it.
1.2 Configure npm’s Global Directory and Network
A global install writes packages into npm’s global prefix directory. Check where that directory is:
npm config get prefixnpm root -g
If the output points under /usr or /usr/local, you’ll usually need sudo to install. A more robust approach is to move the global prefix to your home directory, which removes the need for sudo:
mkdir -p "$HOME/.npm-global"npm config set prefix "$HOME/.npm-global"echo 'export PATH="$HOME/.npm-global/bin:$PATH"' >> ~/.bashrcsource ~/.bashrc
Then verify that npm’s registry is reachable:
npm ping
If npm ping times out, see Section 6 for proxy configuration.
2. Installing the Tool
Run the global install:
npm install -g @example/aicli@latest
The -g flag installs the package globally so the command can be run from any directory. @latest installs the version tagged as latest on npm. To check the remote version first, run npm view @example/aicli version.
You may see many deprecated warnings during installation. These usually indicate that a dependency is no longer maintained or has a newer version available, and in most cases they don’t prevent the tool from working. To judge whether the installation succeeded, check the command’s exit code and the tool’s actual functionality:
echo $?aicli --version
An exit code of 0 plus a working version command means the installation succeeded. The version output format varies by tool; refer to the official documentation.
3. Understanding and Fixing the ENOTEMPTY Error
ENOTEMPTY means “target directory is not empty.” When npm overwrites or replaces an older version, it needs to delete or rename the existing directory. If leftover files remain, the operation fails. There are three common causes:
- A previous installation was interrupted, leaving incomplete files behind.
- Another npm process is writing to the same directory at the same time.
- The package was previously installed with sudo, so the directory is owned by root and the current user can’t modify it properly.
3.1 Diagnose the Problem First
Check whether any npm process is still running:
ps aux | grep -i npm
Inspect the contents and ownership of the target directory:
PKG_DIR="$(npm root -g)/@example/aicli"ls -la "$PKG_DIR"
If the directory is owned by root, a previous sudo install is the cause. In that case, don’t change the ownership directly—instead, switch to a user-level global directory as described in Section 1 and reinstall.
3.2 Back Up, Then Clean Up
After confirming no npm process is running, move the leftover directory to a backup location rather than deleting it outright. This lets you restore it if something goes wrong:
BACKUP_DIR="$HOME/npm-backup-$(date +%Y%m%d%H%M%S)"mkdir -p "$BACKUP_DIR"mv "$PKG_DIR" "$BACKUP_DIR/"
Next, check the integrity of npm’s cache. npm cache verify validates the cache and removes invalid data, which is gentler than running npm cache clean --force:
npm cache verify
Then reinstall:
npm install -g @example/aicli@latestaicli --version
If both the installation and the version check pass, delete the backup:
rm -r "$BACKUP_DIR"
If it still fails, restore the backup to its original location and save the full error output for further troubleshooting.
4. Running the Configuration Wizard and Connecting an LLM
4.1 Start the Wizard
aicli config
The wizard’s interface and options vary by tool, but it typically includes these steps: choose to add a custom provider, enter a provider name, fill in the API endpoint, enter the API key, and select a default model. Pick a memorable provider name, such as custom-llm. When you connect to multiple models, the name helps you tell them apart.
4.2 Enter the API Endpoint
An OpenAI-compatible endpoint usually ends with /v1, for example https://api.example.com/v1. The tool typically appends paths like /chat/completions on top of it. Whether the URL should end with a slash or include a version segment depends on the provider’s documentation and the tool’s implementation. A common symptom of a wrong endpoint is a 404 response. When that happens, first verify the path against the provider’s documentation, then check how the tool constructs the final URL.
4.3 Test the Endpoint Before Using the Wizard
To avoid trial and error inside the wizard, test the endpoint with curl first. Keep the key in an environment variable so it doesn’t appear in your command history:
read -rs LLM_API_KEYexport LLM_API_KEYcurl -sS https://api.example.com/v1/models \-H "Authorization: Bearer $LLM_API_KEY"
read -rs hides your input and prevents it from being written to shell history. If the response contains a JSON list of models, both the endpoint and the key are valid. Some providers don’t offer a /models endpoint; in that case, send a short message through the chat endpoint instead.
When a test fails, you can diagnose the issue from the HTTP status code:
- 404: The endpoint path is incorrect.
- 401: The key is invalid, or the header format is wrong.
- 403: Account permissions or regional restrictions apply.
- 429: The request rate is too high, or your quota is exhausted.
5. Securing Your API Key
The wizard may store the key in a local configuration file, such as ~/.config/aicli/config.json. The exact path depends on the tool’s documentation. Regardless of the storage method, follow these practices:
- Restrict the configuration file’s permissions so only the current user can read and write it:
chmod 600 ~/.config/aicli/config.jsonls -l ~/.config/aicli/config.json
If the tool supports reading the key from an environment variable, prefer that option. Store only the variable name in the configuration file, not the plaintext key. See the official documentation for the exact syntax.
Never write the key into scripts, code repositories, or CI logs. This is especially important on shared servers.
If a key is leaked, immediately revoke it in the provider’s console, create a new key, and update your local environment variable or configuration.
Avoid permanently writing the key into ~/.bashrc for convenience—doing so is effectively storing the plaintext key in an ordinary file.
6. Network Proxy Settings
If the tool’s outbound requests fail, you may need to configure a proxy. Refer to the tool’s official documentation for the supported proxy configuration options, and set the appropriate environment variables for your network environment.
