Self-Host
Learn about how to self-host Khoj on your own machine.
Benefits to self-hosting:
- Privacy: Your data will never have to leave your private network. You can even use Khoj without an internet connection if deployed on your personal computer.
- Customization: You can customize Khoj to your liking, from models, to host URL, to feature enablement.
Setup
These are the general setup instructions for self-hosted Khoj. You can install the Khoj server using either Docker or Pip.
If you want to use the offline chat model and you have a GPU, you should use Installation Option 2 - local setup via the Python package directly. Our Docker image doesn't currently support running the offline chat model on GPU, making inference times really slow.
1A. Install Method 1: Docker
Prerequisites
- Install Docker Engine. See official instructions.
- Ensure you have Docker Compose. See official instructions.
Setup
- Get the sample docker-compose file from Github.
- Configure the environment variables in the docker-compose.yml to your choosing.
Note: Your admin account will automatically be created based on the admin credentials in that file, so pay attention to those. - Now start the container by running the following command in the same directory as your docker-compose.yml file. This will automatically setup the database and run the Khoj server.
docker-compose up
Khoj should now be running at http://localhost:42110! You can see the web UI in your browser.
1B. Install Method 2: Pip
Prerequisites
Install Postgres (with PgVector)
Khoj uses Postgres DB for all server configuration and to scale to multi-user setups. It uses the pgvector package in Postgres to manage your document embeddings. Both Postgres and pgvector need to be installed for Khoj to work.
- MacOS
- Windows
- Linux
- From Source
Install Postgres.app. This comes pre-installed with pgvector
and relevant dependencies.
For detailed instructions and troubleshooting, see this section.
- Use the recommended installer.
- Follow instructions to Install PgVector in case you need to manually install it. Windows support is experimental for pgvector currently, so we recommend using Docker.
- Follow instructions to Install Postgres
- Follow instructions to Install PgVector in case you need to manually install it.
Create the Khoj database
Make sure to update your environment variables to match your Postgres configuration if you're using a different name. The default values should work for most people. When prompted for a password, you can use the default password postgres
, or configure it to your preference. Make sure to set the environment variable POSTGRES_PASSWORD
to the same value as the password you set here.
- MacOS
- Windows
- Linux
createdb khoj -U postgres --password
createdb -U postgres khoj --password
sudo -u postgres createdb khoj --password
Install Khoj server
Install Khoj Server
- Make sure python and pip are installed on your machine
- Check llama-cpp-python setup if you hit any llama-cpp issues with the installation
Run the following command in your terminal to install the Khoj server.
- MacOS
- Windows
- Linux
# ARM/M1+ Machines
MAKE_ARGS="-DLLAMA_METAL=on" python -m pip install khoj
# Intel Machines
python -m pip install khoj
In PowerShell on Windows
# 1. (Optional) To use NVIDIA (CUDA) GPU
$env:CMAKE_ARGS = "-DLLAMA_OPENBLAS=on"
# 1. (Optional) To use AMD (ROCm) GPU
$env:CMAKE_ARGS = "-DLLAMA_HIPBLAS=on"
# 1. (Optional) To use VULCAN GPU
$env:CMAKE_ARGS = "-DLLAMA_VULKAN=on"
# 2. Install Khoj
py -m pip install khoj
# CPU
python -m pip install khoj
# NVIDIA (CUDA) GPU
CMAKE_ARGS="DLLAMA_CUDA=on" FORCE_CMAKE=1 python -m pip install khoj
# AMD (ROCm) GPU
CMAKE_ARGS="-DLLAMA_HIPBLAS=on" FORCE_CMAKE=1 python -m pip install khoj
# VULCAN GPU
CMAKE_ARGS="-DLLAMA_VULKAN=on" FORCE_CMAKE=1 python -m pip install khoj
Start Khoj Server
Before getting started, configure the following environment variables in your terminal for the first run
- MacOS
- Windows
- Linux
export KHOJ_ADMIN_EMAIL=<your-email>
export KHOJ_ADMIN_PASSWORD=<your-password>
If you're using PowerShell:
$env:KHOJ_ADMIN_EMAIL="<your-email>"
$env:KHOJ_ADMIN_PASSWORD="<your-password>"
export KHOJ_ADMIN_EMAIL=<your-email>
export KHOJ_ADMIN_PASSWORD=<your-password>
Run the following command from your terminal to start the Khoj backend and open Khoj in your browser.
khoj --anonymous-mode
--anonymous-mode
allows you to run the server without setting up Google credentials for login. This allows you to use any of the clients without a login wall. If you want to use Google login, you can skip this flag, but you will have to add your Google developer credentials.
On the first run, you will be prompted to input credentials for your admin account and do some basic configuration for your chat model settings. Once created, you can go to http://localhost:42110/server/admin and login with the credentials you just created.
Khoj should now be running at http://localhost:42110. You can see the web UI in your browser.
Note: To start Khoj automatically in the background use Task scheduler on Windows or Cron on Mac, Linux (e.g. with @reboot khoj
)
2. Configure
Login to the Khoj Admin Panel
Go to http://localhost:42110/server/admin and login with the admin credentials you setup during installation.
Ensure you are using localhost, not 127.0.0.1, to access the admin panel to avoid the CSRF error.
You may hit this if you try access Khoj exposed on a custom domain (e.g. 192.168.12.3 or example.com) or over HTTP. Set the environment variables KHOJ_DOMAIN=your-domain and KHOJ_NO_HTTPS=false if required to avoid this error.
Using Safari on Mac? You might not be able to login to the admin panel. Try using Chrome or Firefox instead.
Configure Chat Model
Setup which chat model you'd want to use. Khoj supports local and online chat models.
Add a ServerChatSettings
with Default
and Summarizer
fields set to your preferred chat model via the admin panel. Otherwise Khoj defaults to use the first chat model in your ChatModelOptions for all non chat response generation tasks.
Configure OpenAI Chat
Using Ollama? See the Ollama Integration section for more custom setup instructions.
- Go to the OpenAI settings in the server admin settings to add an OpenAI processor conversation config. This is where you set your API key and server API base URL. The API base URL is optional - it's only relevant if you're using another OpenAI-compatible proxy server.
- Go over to configure your chat model options. Set the
chat-model
field to a supported chat model1 of your choice. For example, you can specifygpt-4o
if you're using OpenAI.- Make sure to set the
model-type
field toOpenAI
. - The
tokenizer
andmax-prompt-size
fields are optional. Set them only if you're sure of the tokenizer or token limit for the model you're using. Contact us if you're unsure what to do here. - If your model supports vision, set the
vision enabled
field totrue
. This is currently only supported for OpenAI models with vision capabilities.
- Make sure to set the
Configure Anthropic Chat
- Go to the OpenAI settings in the server admin settings to add an OpenAI processor conversation config. This is kind of a misnomer, we know. Do not configure the API base url. Just add your API key and give the configuration a friendly name.
- Go over to configure your chat model options. Set the
chat-model
field to a supported chat model by Anthropic of your choice. For example, you can specifyclaude-3-5-sonnet-20240620
.- Make sure to set the
model-type
field toAnthropic
. - The
tokenizer
andmax-prompt-size
fields are optional. Set them only if you're sure of the tokenizer or token limit for the model you're using. Contact us if you're unsure what to do here.
- Make sure to set the
Configure Offline Chat
Offline chat stays completely private and can work without internet using open-source models.
System Requirements:
- Minimum 8 GB RAM. Recommend 16Gb VRAM
- Minimum 5 GB of Disk available
- A CPU supporting AVX or AVX2 instructions is required
- An Nvidia, AMD GPU or a Mac M1+ machine would significantly speed up chat response times
Any chat model on Huggingface in GGUF format can be used for local chat. Here's how you can set it up:
- No need to setup a conversation processor config!
- Go over to configure your chat model options. Set the
chat-model
field to a supported chat model1 of your choice. For example, we recommendbartowski/Meta-Llama-3.1-8B-Instruct-GGUF
, but any gguf model on huggingface should work.
- Make sure to set the
model-type
toOffline
. Do not setopenai config
. - The
tokenizer
andmax-prompt-size
fields are optional. You can set these for non-standard models (i.e not Mistral or Llama based models) or when you know the token limit of the model to improve context stuffing.
Share your data
You can sync your files and folders with Khoj using the Desktop, Obsidian, or Emacs clients or just drag and drop specific files on the website. You can also directly sync your Notion workspace.
3. Use Khoj 🚀
Now open http://localhost:42110 to start interacting with Khoj!
4. Install Khoj Clients (Optional)
Khoj exposes a web interface to search, chat and configure by default.
You can install a Khoj client to sync your documents or to easily access Khoj from within Obsidian, Emacs or your OS.
-
Khoj Desktop:
Install the Khoj Desktop app. -
Khoj Obsidian:
Install the Khoj Obsidian plugin. -
Khoj Emacs:
Install khoj.el
Setup host URL
Set the host URL on your clients settings page to your Khoj server URL. By default, use http://127.0.0.1:42110
or http://localhost:42110
. Note that localhost
may not work in all cases.
Upgrade
- Local Setup
- Docker
- Emacs
- Obsidian
pip install --upgrade khoj
Note: To upgrade to the latest pre-release version of the khoj server run below command
From the same directory where you have your docker-compose
file, this will fetch the latest build and upgrade your server.
docker-compose up --build
- Use your Emacs Package Manager to Upgrade
- See khoj.el package setup for details
- Upgrade via the Community plugins tab on the settings pane in the Obsidian app
- See the khoj plugin setup for details
Uninstall
- Local Setup
- Docker
- Emacs
- Obsidian
# uninstall khoj server
pip uninstall khoj
# delete khoj postgres db
dropdb khoj -U postgres
From the same directory where you have your docker-compose
file, run the command below to remove the server to delete its containers, networks, images and volumes.
docker-compose down --volumes
Uninstall the khoj Emacs, or desktop client in the standard way from Emacs or your OS respectively
You can also rm -rf ~/.khoj
to remove the Khoj data directory if did a local install.
Uninstall the khoj Obisidan, or desktop client in the standard way from Obsidian or your OS respectively
You can also rm -rf ~/.khoj
to remove the Khoj data directory if did a local install.
Troubleshoot
Dependency conflict when trying to install Khoj python package with pip
- Reason: When conflicting dependency versions are required by Khoj vs other python packages installed on your system
- Fix: Install Khoj in a python virtual environment using venv or pipx to avoid this dependency conflicts
- Process:
- Install pipx
- Use
pipx
to install Khoj to avoid dependency conflicts with other python packages.pipx install khoj
- Now start
khoj
using the standard steps described earlier
Install fails while building Tokenizer dependency
- Details:
pip install khoj
fails while building thetokenizers
dependency. Complains about Rust. - Fix: Install Rust to build the tokenizers package. For example on Mac run:
brew install rustup
rustup-init
source ~/.cargo/env - Refer: Issue with Fix for more details
Khoj in Docker errors out with "Killed" in error message
- Fix: Increase RAM available to Docker Containers in Docker Settings
- Refer: StackOverflow Solution, Configure Resources on Docker for Mac
Advanced
Self Host on Custom Domain
You can self-host Khoj behind a custom domain as well. To do so, you need to set the KHOJ_DOMAIN
environment variable to your domain (e.g., export KHOJ_DOMAIN=my-khoj-domain.com
or add it to your docker-compose.yml
). By default, the Khoj server you set up will not be accessible outside of localhost
or 127.0.0.1
.
To expose Khoj on a custom domain over the public internet, use of an SSL certificate is strongly recommended. You can use Let's Encrypt to get a free SSL certificate for your domain.
To disable HTTPS, set the KHOJ_NO_HTTPS
environment variable to True
. This can be useful if Khoj is only accessible behind a secure, private network.
Footnotes
-
Khoj, by default, can use OpenAI GPT3.5+ chat models or GGUF chat models. See this section on how to locally use OpenAI-format compatible proxy servers. ↩ ↩2