update local-source-code.mdx

This commit is contained in:
Bowen Liang
2025-04-29 17:45:32 +08:00
parent bcebc1a73b
commit f4b340a294

View File

@@ -2,54 +2,31 @@
title: Local Source Code Start
---
## Prerequisites
### Setup Docker and Docker Compose
> Before installing Dify, make sure your machine meets the following minimum system requirements:
> - CPU >= 2 Core
> - RAM >= 4 GiB
<table>
<thead>
<tr>
<th>Operating System</th>
<th>Software</th>
<th>Explanation</th>
</tr>
</thead>
<tbody>
<tr>
<td>macOS 10.14 or later</td>
<td>Docker Desktop</td>
<td>Set the Docker virtual machine (VM) to use a minimum of 2 virtual CPUs (vCPUs) and 8 GB of initial memory. Otherwise, the installation may fail. For more information, please refer to the <a href="https://docs.docker.com/desktop/mac/install/">Docker Desktop installation guide for Mac</a>.</td>
</tr>
<tr>
<td>Linux platforms</td>
<td>
<p>Docker 19.03 or later</p>
<p>Docker Compose 1.25.1 or later</p>
</td>
<td>Please refer to the <a href="https://docs.docker.com/engine/install/">Docker installation guide</a> and <a href="https://docs.docker.com/compose/install/">the Docker Compose installation guide</a> for more information on how to install Docker and Docker Compose, respectively.</td>
</tr>
<tr>
<td>Windows with WSL 2 enabled</td>
<td>
<p>Docker Desktop</p>
<p></p>
</td>
<td>We recommend storing the source code and other data that is bound to Linux containers in the Linux file system rather than the Windows file system. For more information, please refer to the <a href="https://docs.docker.com/desktop/windows/install/#wsl-2-backend">Docker Desktop installation guide for using the WSL 2 backend on Windows.</a></td>
</tr>
</tbody>
</table>
| Operating System | Software | Explanation |
| -------------------------- | -------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| macOS 10.14 or later | Docker Desktop | Set the Docker virtual machine (VM) to use a minimum of 2 virtual CPUs (vCPUs) and 8 GB of initial memory. Otherwise, the installation may fail. For more information, please refer to the [Docker Desktop installation guide for Mac](https://docs.docker.com/desktop/mac/install/). |
| Linux platforms | <p>Docker 19.03 or later<br>Docker Compose 1.25.1 or later</p> | Please refer to the [Docker installation guide](https://docs.docker.com/engine/install/) and [the Docker Compose installation guide](https://docs.docker.com/compose/install/) for more information on how to install Docker and Docker Compose, respectively. |
| Windows with WSL 2 enabled | <p>Docker Desktop<br></p> | We recommend storing the source code and other data that is bound to Linux containers in the Linux file system rather than the Windows file system. For more information, please refer to the [Docker Desktop installation guide for using the WSL 2 backend on Windows.](https://docs.docker.com/desktop/windows/install/#wsl-2-backend) |
> If you need to use OpenAI TTS, `FFmpeg` must be installed on the system for it to function properly. For more details, refer to: [Link](https://docs.dify.ai/getting-started/install-self-hosted/install-faq#id-14.-what-to-do-if-this-error-occurs-in-text-to-speech).
### Clone Dify
### Clone Dify Repository
Run the git command to clone the [Dify repository](https://github.com/langgenius/dify).
```Bash
git clone https://github.com/langgenius/dify.git
```
Before enabling business services, we need to first deploy PostgreSQL / Redis / Weaviate (if not locally available). We can start them with the following commands:
### Start Middlewares with Docker Compose
A series of middlewares for storage (e.g. PostgreSQL / Redis / Weaviate (if not locally available)) and extended capabilities (e.g. Dify's [sandbox](https://github.com/langgenius/dify-sandbox) and [plugin-daemon](https://github.com/langgenius/dify-plugin-daemon) services) are required by Dify backend services. Start the middlewares with Docker Compose by running these commands:
```Bash
cd docker
@@ -59,14 +36,16 @@ docker compose -f docker-compose.middleware.yaml up -d
---
### Server Deployment
## Setup Backend Services
- API Interface Service
- Worker Asynchronous Queue Consumption Service
The backend services include
#### Installation of the basic environment:
1. API Service: serving API requests for Frontend service and API accessing
2. Worker Service: serving the aync tasks for datasets processing, workspaces, cleaning-ups etc.
Server startup requires Python 3.12. It is recommended to use [pyenv](https://github.com/pyenv/pyenv) for quick installation of the Python environment.
### Environment Preparation
Python 3.12 is required. It is recommended to use [pyenv](https://github.com/pyenv/pyenv) for quick installation of the Python environment.
To install additional Python versions, use pyenv install.
@@ -74,59 +53,56 @@ To install additional Python versions, use pyenv install.
pyenv install 3.12
```
To switch to the "3.12" Python environment, use the following command:
To switch to the "3.12" Python environment globally, use the following command:
```Bash
pyenv global 3.12
```
#### Follow these steps :
### Start API service
1. Navigate to the "api" directory:
1. Navigate to the `api` directory:
```
cd api
```
> For macOS: install libmagic with `brew install libmagic`.
1. Copy the environment variable configuration file:
2. Prepare the environment variable config file
```
cp .env.example .env
```
2. Generate a random secret key and replace the value of SECRET_KEY in the .env file:
3. Generate a random secret key and replace the value of SECRET_KEY in the .env file
```
awk -v key="$(openssl rand -base64 42)" '/^SECRET_KEY=/ {sub(/=.*/, "=" key)} 1' .env > temp_env && mv temp_env .env
```
3. Install the required dependencies:
4. Dependencies installation
Dify API service uses [Poetry](https://python-poetry.org/docs/) to manage dependencies.
[uv](https://docs.astral.sh/uv/getting-started/installation/) is used to manage dependencies.
Install the required dependencies with `uv` by running:
```
uv sync
```
> For macOS: install libmagic with `brew install libmagic`.
5. Perform the database migration
Perform database migrations to the latest version:
```
poetry env use 3.12
poetry install
uv run flask db upgrade
```
4. Perform the database migration:
Perform database migration to the latest version:
6. Start the API service
```
poetry run flask db upgrade
uv run flask run --host 0.0.0.0 --port=5001 --debug
```
5. Start the API server:
```
poetry run flask run --host 0.0.0.0 --port=5001 --debug
```
output
Expected output:
```
* Debug mode: on
INFO:werkzeug:WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
@@ -138,79 +114,112 @@ pyenv global 3.12
INFO:werkzeug: * Debugger PIN: 695-801-919
```
6. Start the Worker service
### Start the Worker service
To consume asynchronous tasks from the queue, such as dataset file import and dataset document updates, follow these steps to start the Worker service on Linux or macOS:
To consume asynchronous tasks from the queue, such as dataset file import and dataset document updates, follow these steps to start the Worker service
- for macOS or Linux
```
poetry run celery -A app.celery worker -P gevent -c 1 --loglevel INFO -Q dataset,generation,mail,ops_trace
uv run celery -A app.celery worker -P gevent -c 1 --loglevel INFO -Q dataset,generation,mail,ops_trace
```
If you are using a Windows system to start the Worker service, please use the following command instead:
- for Windows
```
poetry run celery -A app.celery worker -P solo --without-gossip --without-mingle -Q dataset,generation,mail,ops_trace --loglevel INFO
uv run celery -A app.celery worker -P solo --without-gossip --without-mingle -Q dataset,generation,mail,ops_trace --loglevel INFO
```
output:
Expected output:
```
-------------- celery@TAKATOST.lan v5.2.7 (dawn-chorus)
-------------- celery@bwdeMacBook-Pro-2.local v5.4.0 (opalescent)
--- ***** -----
-- ******* ---- macOS-10.16-x86_64-i386-64bit 2023-07-31 12:58:08
-- ******* ---- macOS-15.4.1-arm64-arm-64bit 2025-04-28 17:07:14
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app: app:0x7fb568572a10
- ** ---------- .> app: app_factory:0x1439e8590
- ** ---------- .> 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
-- ******* ---- .> 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]
. tasks.add_document_to_index_task.add_document_to_index_task
. tasks.clean_dataset_task.clean_dataset_task
. tasks.clean_document_task.clean_document_task
. tasks.clean_notion_document_task.clean_notion_document_task
. tasks.create_segment_to_index_task.create_segment_to_index_task
. tasks.deal_dataset_vector_index_task.deal_dataset_vector_index_task
. tasks.document_indexing_sync_task.document_indexing_sync_task
. tasks.document_indexing_task.document_indexing_task
. tasks.document_indexing_update_task.document_indexing_update_task
. tasks.enable_segment_to_index_task.enable_segment_to_index_task
. tasks.generate_conversation_summary_task.generate_conversation_summary_task
. tasks.mail_invite_member_task.send_invite_member_mail_task
. tasks.remove_document_from_index_task.remove_document_from_index_task
. tasks.remove_segment_from_index_task.remove_segment_from_index_task
. tasks.update_segment_index_task.update_segment_index_task
. tasks.update_segment_keyword_index_task.update_segment_keyword_index_task
. schedule.clean_embedding_cache_task.clean_embedding_cache_task
. schedule.clean_messages.clean_messages
. schedule.clean_unused_datasets_task.clean_unused_datasets_task
. schedule.create_tidb_serverless_task.create_tidb_serverless_task
. schedule.mail_clean_document_notify_task.mail_clean_document_notify_task
. schedule.update_tidb_serverless_status_task.update_tidb_serverless_status_task
. tasks.add_document_to_index_task.add_document_to_index_task
. tasks.annotation.add_annotation_to_index_task.add_annotation_to_index_task
. tasks.annotation.batch_import_annotations_task.batch_import_annotations_task
. tasks.annotation.delete_annotation_index_task.delete_annotation_index_task
. tasks.annotation.disable_annotation_reply_task.disable_annotation_reply_task
. tasks.annotation.enable_annotation_reply_task.enable_annotation_reply_task
. tasks.annotation.update_annotation_to_index_task.update_annotation_to_index_task
. tasks.batch_clean_document_task.batch_clean_document_task
. tasks.batch_create_segment_to_index_task.batch_create_segment_to_index_task
. tasks.clean_dataset_task.clean_dataset_task
. tasks.clean_document_task.clean_document_task
. tasks.clean_notion_document_task.clean_notion_document_task
. tasks.deal_dataset_vector_index_task.deal_dataset_vector_index_task
. tasks.delete_account_task.delete_account_task
. tasks.delete_segment_from_index_task.delete_segment_from_index_task
. tasks.disable_segment_from_index_task.disable_segment_from_index_task
. tasks.disable_segments_from_index_task.disable_segments_from_index_task
. tasks.document_indexing_sync_task.document_indexing_sync_task
. tasks.document_indexing_task.document_indexing_task
. tasks.document_indexing_update_task.document_indexing_update_task
. tasks.duplicate_document_indexing_task.duplicate_document_indexing_task
. tasks.enable_segments_to_index_task.enable_segments_to_index_task
. tasks.mail_account_deletion_task.send_account_deletion_verification_code
. tasks.mail_account_deletion_task.send_deletion_success_task
. tasks.mail_email_code_login.send_email_code_login_mail_task
. tasks.mail_invite_member_task.send_invite_member_mail_task
. tasks.mail_reset_password_task.send_reset_password_mail_task
. tasks.ops_trace_task.process_trace_tasks
. tasks.recover_document_indexing_task.recover_document_indexing_task
. tasks.remove_app_and_related_data_task.remove_app_and_related_data_task
. tasks.remove_document_from_index_task.remove_document_from_index_task
. tasks.retry_document_indexing_task.retry_document_indexing_task
. tasks.sync_website_document_indexing_task.sync_website_document_indexing_task
[2023-07-31 12:58:08,831: INFO/MainProcess] Connected to redis://:**@localhost:6379/1
[2023-07-31 12:58:08,840: INFO/MainProcess] mingle: searching for neighbors
[2023-07-31 12:58:09,873: INFO/MainProcess] mingle: all alone
[2023-07-31 12:58:09,886: INFO/MainProcess] pidbox: Connected to redis://:**@localhost:6379/1.
[2023-07-31 12:58:09,890: INFO/MainProcess] celery@TAKATOST.lan ready.
2025-04-28 17:07:14,681 INFO [connection.py:22] Connected to redis://:**@localhost:6379/1
2025-04-28 17:07:14,684 INFO [mingle.py:40] mingle: searching for neighbors
2025-04-28 17:07:15,704 INFO [mingle.py:49] mingle: all alone
2025-04-28 17:07:15,733 INFO [worker.py:175] celery@bwdeMacBook-Pro-2.local ready.
2025-04-28 17:07:15,742 INFO [pidbox.py:111] pidbox: Connected to redis://:**@localhost:6379/1.
```
---
## Deploy the frontend page
## Setup Web Service
Start the web frontend client page service
Start the web service is built for frontend pages .
#### Installation of the basic environment:
### Environment Preparation
To start the web frontend service, you will need [Node.js v18.x (LTS)](http://nodejs.org/) and [NPM version 8.x.x](https://www.npmjs.com/) or [Yarn](https://yarnpkg.com/).
To start the web frontend service, [Node.js v22 (LTS)](http://nodejs.org/) and [PNPM v10](https://pnpm.io/) are requied.
- Install NodeJS + NPM
- Install NodeJS
Please visit [https://nodejs.org/en/download](https://nodejs.org/en/download) and choose the installation package for your respective operating system that is v18.x or higher. It is recommended to download the stable version, which includes NPM by default.
Please visit [https://nodejs.org/en/download](https://nodejs.org/en/download) and choose the installation package for your respective operating system that is v18.x or higher. LTS version is recommanded for common usages.
#### Follow these steps :
- Install PNPM
Follow the [the installation guidance](https://pnpm.io/installation) to install PNPM. Or just run this command to install `pnpm` with `npm`.
```
npm i -g pnpm
```
### Start Web Service
1. Enter the web directory
@@ -218,14 +227,15 @@ Please visit [https://nodejs.org/en/download](https://nodejs.org/en/download) an
cd web
```
2. Install the dependencies.
2. Dependencies installation
```
npm i -g pnpm
pnpm install
pnpm install --frozen-lockfile
```
3. Configure the environment variables. Create a file named .env.local in the current directory and copy the contents from .env.example. Modify the values of these environment variables according to your requirements:
3. Prepare the environment variable config file
Create a file named `.env.local` in the current directory and copy the contents from `.env.example`. Modify the values of these environment variables according to your requirements:
```
# For production release, change this to PRODUCTION
@@ -247,31 +257,29 @@ Please visit [https://nodejs.org/en/download](https://nodejs.org/en/download) an
NEXT_PUBLIC_SENTRY_PROJECT=
```
4. Build the code
4. Build the web service
```
npm run build
pnpm build
```
5. Start the web service
```
npm run start
# or
yarn start
# or
pnpm start
```
After successful startup, the terminal will output the following information
Expected output
```
▲ Next.js 15
- Local: http://localhost:3000
- Network: http://0.0.0.0:3000
```
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
warn - You have enabled experimental feature (appDir) in next.config.js.
warn - Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use at your own risk.
info - Thank you for testing `appDir` please leave your feedback at https://nextjs.link/app-feedback
```
✓ Starting...
✓ Ready in 73ms
```
### Access Dify
Finally, access [http://127.0.0.1:3000](http://127.0.0.1:3000/) to use the locally deployed Dify.
Access [http://127.0.0.1:3000](http://127.0.0.1:3000/) via browsers to enjoy all the exciting features of Dify.
Cheers ! 🍻