Have you been stumped by an error message saying “command not found: openai” while trying to get your hands on the OpenAI CLI? I guess your first idea is to install the OpenAI CLI using homebrew. After all it’s everyones favorite package manager on MacOS, right? Well, that was literally my thought process. So I went ahead and tried to execute
brew install openai. Well turns out homebrew does not have the OpenAI cli in its repository. For this post will guide you through a foolproof installation process to get this situation sorted.
While exploring the possibilities of OpenAI and ChatGPT for training a model on my Intercom customer support chat history, I came across a tutorial on OpenAI’s platform. This tutorial required me to install the OpenAI CLI. Although the instructions seemed straightforward, running the installation commands yielded the infamous “command not found: openai” error. Thus began the journey looking into installing the OpenAI CLI. Side note: It wasn’t through homebrew.
Following the OpenAI guide, I ran the installation command
pip install --upgrade openai. However, when I attempted
openai --help, MacOS 13.3 told me that I don’t have the openai command installed.
brew install openai didn’t work either. So I found myself in a situation that required me to dig deeper into the OpenAI CLI installation. Here’s the step-by-step process I undertook to make it work.
Steps to Install OpenAI CLI on MacOS
There are prerequisites to ensure the smooth installation of the OpenAI CLI. Here’s a rundown of the necessary steps.
Ensure Python3 Installation
First and foremost, ensure Python is successfully installed on your machine.
You can verify if Python3 already exists on your system by executing:
If this command returns the Python version, you’re all set. If not, I suggest you install Python using Homebrew by running:
brew install python3
To confirm a successful installation, execute
OpenAI CLI Installation via pip
Though the OpenAI guide suggests
pip install --upgrade openai, the installation for Python 3 users needs a minor tweak.
To install the OpenAI CLI, use:
pip3 install --upgrade openai
openai --help at this point might still result in a “command not found” error. The subsequent steps address this issue.
Making OpenAI CLI Executable
Next, we need to create a binary file in the local user’s bin directory (/usr/local/bin), which holds all user-specific executable binaries.
Run the following command:
sudo sh -c 'echo python3 "$(python3 -m site --user-site)"/openai/_openai_scripts.py \$\@ > /usr/local/bin/openai'
This command essentially does three things:
- It obtains the path to the directory containing globally installed Python packages:
python3 -m site --user-site.
- It extends the path to the OpenAI CLI scripts Python script:
- It writes this extended path into a file in the /usr/local/bin directory, enabling it to be used as a shell command from any location.
Lastly, we need to ensure the file is executable by running:
sudo chmod +x /usr/local/bin/openai
This final step should allow you to use the OpenAI CLI from anywhere in your terminal.
Testing the Installation
To verify if everything is correctly set up, execute:
This command should return all available OpenAI CLI commands, indicating a successful installation.
And there you have it – a comprehensive guide on installing the OpenAI CLI on MacOS. Feel free to leave a comment if you have any queries or run into any trouble during the process. And all that without homebrew. Maybe one day brew will add a package to support installing the cli tool by just running brew install openai.