本地源码启动dify

1.下载源码

git clone https://github.com/langgenius/dify.git

在启用业务服务之前,需要先部署 PostgreSQL / Redis / Weaviate(如果本地没有的话),可以通过以下命令启动:
依赖安装高版本的docker-compose

cd docker
cp middleware.env.example middleware.env
docker compose -f docker-compose.middleware.yaml up -d

需要对docker-compose.middleware.yaml的镜像地址进行替换。

https://docker.aityp.com/

2. 安装python

dify依赖的python版本是 Python 3.12。下面是rocky linux安装Python 3.12的步骤
查看当前系统安装的Python版本

yum list python3

搜索Python 3.12

yum search python3.12

安装Python 3.12

yum install -y python3.12.x86_64

3. 后端启动服务

3.1 进入 api 目录

cd api

3.2 复制环境变量配置文件

cp .env.example .env

3.3 生成随机密钥,并替换 .env 中 SECRET_KEY 的值

awk -v key="$(openssl rand -base64 42)" '/^SECRET_KEY=/ {sub(/=.*/, "=" key)} 1' .env > temp_env && mv temp_env .env

提示是否覆盖,直接覆盖即可。

3.4 安装依赖包

Dify API 服务使用 Poetry 来管理依赖。你可以执行 poetry shell 来激活环境。

poetry env use 3.12
poetry install

3.5 执行数据库迁移

将数据库结构迁移至最新版本。

poetry shell
flask db upgrade

3.6 启动 API 服务

poetry shell
flask run --host 0.0.0.0 --port=5001 --debug

正确输出:

2025-03-12 06:15:07,607 INFO [utils.py:162]  NumExpr defaulting to 4 threads.
 * Debug mode: on
2025-03-12 06:15:29,759 INFO [_internal.py:97]  WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:5001
 * Running on http://10.0.2.21:5001
2025-03-12 06:15:29,759 INFO [_internal.py:97]  Press CTRL+C to quit
2025-03-12 06:15:29,769 INFO [_internal.py:97]   * Restarting with stat
2025-03-12 06:15:31,752 INFO [utils.py:162]  NumExpr defaulting to 4 threads.
2025-03-12 06:15:34,703 WARNING [_internal.py:97]   * Debugger is active!
2025-03-12 06:15:34,703 INFO [_internal.py:97]   * Debugger PIN: 119-829-848

3.7 启动 Worker 服务

用于消费异步队列任务,如知识库文件导入、更新知识库文档等异步操作。 Linux / MacOS 启动:

poetry shell
celery -A app.celery worker -P gevent -c 1 -Q dataset,generation,mail,ops_trace --loglevel INFO

如果使用 Windows 系统启动,请替换为该命令:

celery -A app.celery worker -P solo --without-gossip --without-mingle -Q dataset,generation,mail,ops_trace --loglevel INFO

正确输出:

2025-03-12 06:15:07,607 INFO [utils.py:162]  NumExpr defaulting to 4 threads.
/root/dify/dify-main/api/.venv/lib/python3.12/site-packages/celery/platforms.py:829: SecurityWarning: You're running the worker with superuser privileges: this is
absolutely not recommended!

Please specify a different user using the --uid option.

User information: uid=0 euid=0 gid=0 egid=0

  warnings.warn(SecurityWarning(ROOT_DISCOURAGED.format(
 
 -------------- celery@mk8s-master v5.4.0 (opalescent)
--- ***** ----- 
-- ******* ---- Linux-6.11.9-1.el9.elrepo.x86_64-x86_64-with-glibc2.34 2025-03-12 06:15:31
- *** --- * --- 
- ** ---------- [config]
- ** ---------- .> app:         app_factory:0x7fd60823c7d0
- ** ---------- .> transport:   redis://:**@localhost:6379/1
- ** ---------- .> results:     postgresql://postgres:**@localhost:5432/dify
- *** --- * --- .> concurrency: 1 (gevent)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** ----- 
 -------------- [queues]
                .> dataset          exchange=dataset(direct) key=dataset
                .> generation       exchange=generation(direct) key=generation
                .> mail             exchange=mail(direct) key=mail
                .> ops_trace        exchange=ops_trace(direct) key=ops_trace

[tasks]
   ....省略....
  
2025-03-12 06:15:31,511 INFO [connection.py:22]  Connected to redis://:**@localhost:6379/1
2025-03-12 06:15:31,524 INFO [mingle.py:40]  mingle: searching for neighbors
2025-03-12 06:15:32,623 INFO [mingle.py:49]  mingle: all alone
2025-03-12 06:15:32,676 INFO [worker.py:175]  celery@mk8s-master ready.
2025-03-12 06:15:32,683 INFO [pidbox.py:111]  pidbox: Connected to redis://:**@localhost:6379/1.

4. 前端页面部署

进入 https://nodejs.org/en/download,选择对应操作系统的 v18.x 以上的安装包下载并安装,建议 stable 版本。

4.1 进入 web 目录

cd web

4.2 安装依赖包

npm install pnpm -g --registry=https://mirrors.cloud.tencent.com/npm/
pnpm install --registry=https://mirrors.cloud.tencent.com/npm/

4.3 配置环境变量

在当前目录下创建文件 .env.local,并复制.env.example中的内容。根据需求修改这些环境变量的值。

# For production release, change this to PRODUCTION
NEXT_PUBLIC_DEPLOY_ENV=DEVELOPMENT
# The deployment edition, SELF_HOSTED
NEXT_PUBLIC_EDITION=SELF_HOSTED
# The base URL of console application, refers to the Console base URL of WEB service if console domain is
# different from api or web app domain.
# example: http://cloud.dify.ai/console/api
NEXT_PUBLIC_API_PREFIX=http://localhost:5001/console/api
# The URL for Web APP, refers to the Web App base URL of WEB service if web app domain is different from
# console or api domain.
# example: http://udify.app/api
NEXT_PUBLIC_PUBLIC_API_PREFIX=http://localhost:5001/api

# SENTRY
NEXT_PUBLIC_SENTRY_DSN=
NEXT_PUBLIC_SENTRY_ORG=
NEXT_PUBLIC_SENTRY_PROJECT=

4.4 构建代码

pnpm run build

这个需要等待的实际比较长

5. 启动web服务

pnpm start

正常启动后,终端会输出如下信息:

> dify-web@1.0.0 start /root/dify/dify-main/web
> cp -r .next/static .next/standalone/.next/static && cp -r public .next/standalone/public && cross-env PORT=$npm_config_port HOSTNAME=$npm_config_host node .next/standalone/server.js

  ▲ Next.js 14.2.24
  - Local:        http://localhost:3000
  - Network:      http://0.0.0.0:3000

 ✓ Starting...
 ✓ Ready in 1278ms

5. 访问 Dify

最后,访问 http://127.0.0.1:3000 即可使用本地部署的 Dify。

6. 总结

在安装、启动过程过程中会遇到不少确实依赖库的情况,多用以下命令进行依赖安装即可。

pip install
参考:https://docs.dify.ai/zh-hans/getting-started/install-self-hosted/local-source-code
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BUG弄潮儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值