How to use ChatGPT locally on Linux for coding
Purpose
This post demonstrates how to use ChatGPT locally on Linux for coding tasks.
Environment
- Ubuntu 22.04 LTS
- Firefox 120.0
- Python 3.10
- OpenAI CLI
The Problem
When I tried to run ChatGPT locally on my Linux machine for coding, I found many options but was confused about what actually works. I wanted ChatGPT to “run in my linux machine and do the coding for me” like I can with Claude Code, but I wasn’t sure if a subscription gets me local access or if I need to “pay for API.”
The ChatGPT Linux Options
There are three main approaches to using ChatGPT on Linux:
- Web interface: Browser access to chat.openai.com
- Desktop app: OpenAI’s official desktop app (if available)
- API integration: Using OpenAI’s API with local tools
We will explore each option to show what’s possible.
Approach 1: Web Interface
The simplest way is using the web interface. I don’t need to install anything - just use my browser.
# No installation needed - just use in browser# Open Chrome/Firefox and navigate to chat.openai.com# Works on any Linux distribution with a browserWhen I run this:
# Open Firefoxfirefox https://chat.openai.comI get full access to ChatGPT in my browser. The interface works well, and I can paste code for debugging help.
Approach 2: Python API Integration
I wanted to integrate ChatGPT directly into my development workflow. I tried using the Python library.
import openaiimport os
# Set up API keyclient = openai.OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
# Make API request for coding helpresponse = client.chat.completions.create( model="gpt-4", messages=[ {"role": "user", "content": "Help me debug this Python function:"}, {"role": "user", "content": "def find_max(numbers):\n max_num = 0\n for num in numbers:\n if num > max_num:\n max_num = num\n return max_num"} ], temperature=0.1)
print(response.choices[0].message.content)When I run this:
# Install openai library firstpip install openai
# Set API key and runexport OPENAI_API_KEY=sk-...python chatgpt_script.pyI get ChatGPT’s coding advice directly in my terminal.
Approach 3: API with curl
For quick command-line help, I tried using curl directly.
# Make API request directly from terminalcurl -X POST "https://api.openai.com/v1/chat/completions" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d '{ "model": "gpt-4", "messages": [ {"role": "user", "content": "Explain this bash command: find . -name '*.py' -exec grep -l "import" {} ;"} ] }'When I run this with a valid API key:
export OPENAI_API_KEY=sk-...curl -X POST "https://api.openai.com/v1/chat/completions" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d '{ "model": "gpt-4", "messages": [ {"role": "user", "content": "Explain this bash command: ls -la"} ] }' | jq '.choices[0].message.content'I get a detailed explanation of the bash command.
The Truth About “Local” ChatGPT
After testing all these options, I learned that none provide a truly “local” ChatGPT experience. I still need internet connection and OpenAI services. The API approach feels local because the requests come from my machine, but the AI processing happens on OpenAI’s servers.
Summary
In this post, I showed how to use ChatGPT locally on Linux for coding. The key point is understanding that while you can access ChatGPT through various methods on Linux, you can’t run it fully offline like Claude Code. The API integration gives the most seamless local experience, but requires an OpenAI account and API access.
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments