Chroma DB环境搭建:Ubuntu 20.04平台最佳实践

云信安装大师
90
AI 质量分
10 5 月, 2025
2 分钟阅读
0 阅读

Chroma DB环境搭建:Ubuntu 20.04平台最佳实践

引言

Chroma DB是一个开源的向量数据库,专门为AI应用设计,特别适合存储和检索嵌入向量。本文将详细介绍在Ubuntu 20.04系统上搭建Chroma DB环境的完整流程,包括依赖安装、环境配置和基础使用示例。

准备工作

在开始之前,请确保你的Ubuntu 20.04系统满足以下要求:

  • Ubuntu 20.04 LTS (已更新到最新补丁)
  • Python 3.8或更高版本
  • pip包管理工具
  • 至少4GB内存(推荐8GB以上)
  • 10GB可用磁盘空间

步骤1:系统更新与基础依赖安装

首先更新系统并安装必要的依赖:

代码片段
# 更新软件包列表
sudo apt update

# 升级已安装的软件包
sudo apt upgrade -y

# 安装基础开发工具和Python环境
sudo apt install -y python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools

# 安装其他可能需要的依赖
sudo apt install -y git curl wget unzip

注意事项
-y参数自动确认所有提示,适合脚本化安装
– 如果系统已经安装了部分软件包,apt会跳过它们

步骤2:创建Python虚拟环境

推荐使用虚拟环境来隔离Chroma DB的Python依赖:

代码片段
# 安装virtualenv工具
sudo pip3 install virtualenv

# 创建项目目录并进入
mkdir chroma_db_project && cd chroma_db_project

# 创建Python虚拟环境
virtualenv venv -p python3

# 激活虚拟环境
source venv/bin/activate

激活后,你的命令行提示符前应该会出现(venv)标记。

步骤3:安装Chroma DB

在激活的虚拟环境中安装Chroma DB:

代码片段
pip install chromadb

可选:如果你想使用最新的开发版本,可以从GitHub直接安装:

代码片段
pip install git+https://github.com/chroma-core/chroma.git@main

实践经验
– Chroma DB会同时安装一些机器学习相关的依赖(如numpy等),可能需要一些时间
– 如果遇到网络问题,可以考虑使用国内镜像源:

代码片段
pip install chromadb -i https://pypi.tuna.tsinghua.edu.cn/simple/

步骤4:验证安装

创建一个简单的Python脚本来验证Chroma DB是否正常工作:

代码片段
import chromadb

# 创建客户端实例(内存模式)
client = chromadb.Client()

# 创建一个集合(类似表)
collection = client.create_collection("test_collection")

# 添加一些文档和对应的向量(这里使用随机向量)
collection.add(
    documents=["这是第一个文档", "这是第二个文档"],
    metadatas=[{"source": "doc1"}, {"source": "doc2"}],
    ids=["id1", "id2"]
)

# 查询相似文档(使用随机查询向量)
results = collection.query(
    query_texts=["查询相关文档"],
    n_results=1
)

print("查询结果:", results)

将上述代码保存为test_chroma.py并运行:

代码片段
python test_chroma.py

如果看到类似以下的输出,说明安装成功:

代码片段
查询结果: {'ids': [['id1']], 'distances': [[0.123456]], 'metadatas': [[{'source': 'doc1'}]], 'embeddings': None, 'documents': [['这是第一个文档']]}

步骤5:持久化存储配置(可选)

默认情况下,上面的例子使用的是内存模式。要启用持久化存储:

代码片段
import chromadb

# 指定持久化目录(会自动创建)
client = chromadb.PersistentClient(path="/path/to/chroma/storage")

collection = client.create_collection("persistent_collection")

collection.add(
    documents=["持久化存储的文档"],
    ids=["persistent_id"]
)

注意事项
/path/to/chroma/storage需要有写入权限
– Chroma目前不支持多进程同时写入同一存储目录

常见问题解决

Q1: pip安装时出现SSL错误

解决方案:

代码片段
pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org chromadb

Q2: Python版本不兼容错误

确保使用Python ≥3.8:

代码片段
python3 --version #检查版本号 

Q3: Chroma服务无法启动或崩溃

尝试升级依赖:

代码片段
pip install --upgrade chromadb numpy hnswlib sqlite3 

Q4: Linux系统文件描述符限制问题

对于生产环境,可能需要提高限制:

代码片段
ulimit -n 65536 #临时生效 
echo "* soft nofile 65536" | sudo tee -a /etc/security/limits.conf #永久生效 

Docker部署方式(备选)

如果你更喜欢使用Docker容器运行Chroma DB:

代码片段
docker pull ghcr.io/chroma-core/chroma:latest 

docker run -p 8000:8000 ghcr.io/chroma-core/chroma 

然后可以通过http://localhost:8000访问服务端。

API服务模式启动

要启动Chroma作为HTTP API服务:

代码片段
from chromadb.utils import embedding_functions 

default_ef = embedding_functions.DefaultEmbeddingFunction() 

server = chromadb.Server(settings=chromadb.Settings(chroma_api_impl="rest", 
                                                  chroma_server_host="0.0.0.0", 
                                                  chroma_server_http_port=8000)) 

server.start() 

现在可以通过其他客户端连接http://your-server-ip:8000访问。

Python客户端连接远程服务示例

代码片段
import chromadb 

remote_client = chromadb.HttpClient(host="your-server-ip", port=8000) 

print(remote_client.heartbeat()) #检查连接状态 

Chroma与LangChain集成示例

如果你计划将Chroma与LangChain一起使用:

代码片段
from langchain.vectorstores import Chroma 
from langchain.embeddings import OpenAIEmbeddings 

embeddings = OpenAIEmbeddings() #需要OPENAI_API_KEY环境变量 

vectorstore = Chroma.from_texts( 
    texts=["LangChain集成测试"],  
    embedding=embeddings,  
    collection_name="langchain_test" 
) 

result = vectorstore.similarity_search("测试") 
print(result) 

Chrome Web UI界面 (实验性)

社区提供的Web界面可以更方便地浏览数据:

  1. Clone仓库:

    代码片段
    git clone https://github.com/shellcodesniper/chromadb-webui.git && cd chromadb-webui  
    
  2. 修改配置指向你的Chrome实例地址:

    代码片段
    // src/config.js中修改API_BASE_URL  
    
  3. Install and run:

    代码片段
    npm install && npm start  
    

现在可以访问http://localhost:3000查看Web界面。


通过以上步骤,你应该已经在Ubuntu20.04上成功搭建了功能完整的ChromeDB环境。根据你的应用场景选择合适的使用方式——内存模式适合快速原型开发,而持久化模式和API服务器更适合生产部署。

原创 高质量