Run DeepSeek Locally with Ollama: Offline ChatGPT Alternative + PHP API Guide
Running ChatGPT-like models locally without an internet connection is now possible using Ollama, an open-source framework that lets you download, manage, and interact with LLMs entirely on your machine. This article guides you through installing Ollama, downloading and running DeepSeek offline, and integrating it into your PHP applications via a simple REST API. By the end, you’ll have a self-hosted LLM capable of powering chat experiences with privacy and no recurring costs.
What Is Ollama and Why Go Offline?
Ollama is an open-source CLI and service interface that enables developers to run LLMs on their local hardware, eliminating reliance on external APIs or cloud services (medium.com, kodeco.com). Going offline ensures the privacy of your data, removes usage quotas and API costs, and provides developers with full control over model versions and performance tuning (kodeco.com).
Installing Ollama on Your System
-
Download Ollama Installer: Visit the official site at https://ollama.com/download and select your operating system. Download the installer package for macOS, Windows, or Linux.
-
Run the Installer: On macOS and Windows, double-click the downloaded file and follow on-screen prompts. On Linux, extract the archive and place the
ollama
binary in your/usr/local/bin
directory. -
Verify Installation: Open a terminal or command prompt and run:
$ ollama help
You should see CLI usage instructions, confirming a successful install.
Downloading and Running DeepSeek Offline
-
List Available Models:
$ ollama list
-
Pull and Run DeepSeek:
$ ollama run deepseek-coder
-
The first run will download the model (~4GB) and start a local server at
http://localhost:11434
. -
Interact via CLI:
>>> How do I sort an array in PHP?
-
You’ll receive responses like ChatGPT, but served entirely from your local machine.
Using Ollama’s REST API
Ollama exposes a simple RESTful endpoint for completions at /completions
on port 11434
by default. You can integrate this into any application that supports HTTP requests.
Example PHP Client
Create a file named chat_api.php
with the following content:
<?php
$prompt = isset($_GET['prompt']) ? $_GET['prompt'] : 'Hello!';
$data = [
'model' => 'deepseek-coder',
'prompt' => $prompt,
'max_tokens' => 150
];
$ch = curl_init('http://localhost:11434/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json'
]);
$response = curl_exec($ch);
curl_close($ch);
header('Content-Type: application/json');
echo $response;
-
Start the PHP server:
$ php -S localhost:8000
-
Query Your Local Chat API:
Visithttp://localhost:8000/chat_api.php?prompt=Explain%20recursion
in your browser.
This simple PHP snippet sends a JSON payload to Ollama’s API and echoes the AI-generated response to clients, enabling seamless integration of local LLM capabilities into web projects.
Conclusion
By following these steps, you can harness powerful LLMs like DeepSeek on your own hardware without internet access, preserving privacy and reducing costs. The provided PHP code demonstrates how easy it is to expose Ollama’s capabilities as a web API, opening doors to local AI-powered applications in web development.