How to Deploy Local Dify for a Secure and Fast LLM Agentic Workflow

Posted by Riino on

What is Dify and Why depoly it locally

Dify is a low code tool that's allow you to create workflow with LLM nodes. Which means you can design and arrange a dataflow to deal with complex NLP tasks or automated NLP task in minutes without coding.

Although it provides cloud version and you can use most core features for free at anywhere, but you suffer from unstable respond time and , most importantly, data security concern.

Since Dify will connect your LLM API in its backend server and process all data you sent or get, it is a better idea to deploy it yourself and all data will be saved locally. In this post I will show you a full step-by-step guide to install it locally, take Windows + WSL as example.

image-20240721171632763

Image source: Dify

0x00 Before start, make your plan

Before any action, let's review what need to be in your machine to deploy a Dify with its source codes. Dify provides 2 ways of local deployment:

  1. All-in-docker-compose : Frontend, backend and middleware(DB, sandbox, etc.) will be composed in to a single docker compose file.
  2. From source code: You deploy a development environment for your frontend, backend, while everything else in a smaller docker compose.

I personally recommend last choice, because:

  • You can get latest update in no time: In this mode, any Dify code commit in Github can be pulled into your code workspace. If you use all-in-docker, you have to wait for image update.
  • You can modify your frontend or backend more easily by simply editing the codes.
  • A little effort is required compared with all-in-docker mode (to install python environment and node environment.)

And here is a diagram for all services with its default ports in your local Dify as a full picture, you may not be able to understand every components' name, but we will introduce them with installation instruction.

image-20240721171340884

0x01 All you need to do

Notes: all the commands below run in a Linux distro, if you are using windows, please install WSL (Windows Subsystem Linux) , open PowerShell and enter bash to jump to WSL.

The first thing you need is to run git clone https://github.com/langgenius/dify.git , after entering Dify folder in your disk, there's 3 main workstream waiting for you:

  1. In /api folder, get your python environment ready, which will be used to run backend API and Celery(distributed task queue)
  2. In /web folder, get your node environment ready, which will be used to run frontend UI.
  3. In /docker folder, get your docker compose ready, which will be running database and sandbox.

Moreover, you may suffer from network issue especially if you have limited web access, you may consider updating pip mirror , npm mirror and docker registry, and I will show you how in the 0x06 Troubleshooting

image-20240721173826978

0x02 Get your python environment ready and run API

Move to /api folder.

Dify require a >3.10 version Python, and suggest using pyenv (a tool helps you install and switch Python with different versions) to set a 3.10 version Python.

  1. Install pyenv:

    curl https://pyenv.run | bash
    

    After completion, try pyenv to verify if pyenv is installed, otherwise, add the $HOME/.pyenv to PATH to make your OS read pyenv command by add following command in ~/.bashrc (if you use bash, i.e. you use WSL) . For more info, visit https://github.com/pyenv/pyenv?tab=readme-ov-file#getting-pyenv

    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
    echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
    echo 'eval "$(pyenv init -)"' >> ~/.bashrc
    

    Note: You can always use exec "$SHELL" to refresh to let new command work.

  2. Install Python

    Use pyenv to install python with certain version

    pyenv install 3.10
    

    Activate your Python:

    pyenv global 3.10
    

    You can try python command to verify your Python.

  3. Install poetry

    Run

    curl -sSL https://install.python-poetry.org | python3 -
    

    Note: Poetry should be installed in a clean environment/machine.

  4. Install Dify backend requirments and celery

    cd to your api folder:

    cd api
    

    install requirements (make sure Python 3.10 is activated)

    poetry env use 3.10
    poetry install
    

    Notes: poetry will create a venv in your Dify api folder, you need to activate it , just like origin venv:

    poetry shell
    

    Before starting api, init the .env:

    cp .env.example .env
    

    Get a key using sed -i 's/SECRET_KEY=.*/SECRET_KEY=<your_value>/' .env and paste it into SECRET_KEY value of your .env file.

    And you can start flask and celery , if you run Dify at first time, run flask db upgrade

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

    In another shell, run:

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

0x03 Get your node environment ready and run Web

Move to /web folder.

Similar with python, we can use nvm to manage and install different node.js , to install nvm:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

Note: check the version number or just visit https://github.com/nvm-sh/nvm?tab=readme-ov-file#install--update-script

After installation, install Node.js and activate it:

nvm install 20
nvm use 20

You can use nvm list and nvm current to see what you have.

Then, jump to web, install yarn and requirements :

cd web
npm install -g yarn
yarn install

Note: yarn is a better choice compared with origin npm.

Then, compile the Dify website and serve it:

npm run build
yarn start

0x04 Get your docker environment ready and run middleware

Move to /docker folder.

If you are using windows, install Docker Desktop, and refer to https://docs.docker.com/engine/install/ubuntu/ to install docker.

In WSL, if you see docker deamon is not running , run:

sudo service docker start

Init the .env :

cp middleware.env.example middleware.env

Docker compose up:

docker compose -f docker-compose.middleware.yaml up -d

0x06 Troubleshooting

  1. Network and Proxy

    For node.js (npm), use:

    npm config set registry https://mirrors.tuna.tsinghua.edu.cn/
    

    For poetry install, use:

    export POETRY_PYPI_MIRROR_URL=https://mirrors.cloud.tencent.com/pypi/simple/
    

    For Docker, add file /etc/*docker*/daemon.json with:

    {
      "registry-mirrors": [
        "https://docker.riino.site"
      ]
    }
    

    For raw.githubusercontent.com connection issue , replace origin URL with raw.staticdn.net

  2. Docker Connectivity

    • If you cannot run sandbox and get connection error, clean up environment variable HTTP_PROXY and HTTPS_PROXY inside your docker container.
  3. I lost my password

    In your /api folder, with poetry shell activated, run flask reset-password . If you use all-in-docker mode, run docker exec -it docker-api-1 flask reset-password.

    Note: this requires origin admin email, i.e. you MUST enter correct email to reset password.

  4. I lost my email and password

    1. Start your Postgres database docker by running your all-in-docker compose or middleware docker compose.

    2. Use any DBMS tools, for instance, Pgadmin, to connect to your Postgres, the default connection value is :

      DB_USERNAME=postgres
      DB_PASSWORD=difyai123456
      DB_HOST=db
      DB_PORT=5432
      DB_DATABASE=dify
      
    3. After connection, run query:

      Select * from accounts
      

    And you can review your email this way.

    image-20240722143922266

  5. Open Dify in WSL to LAN or WAN

    In Windows 10, you need to build fowarding by using: (run netsh in windows powershell)

    netsh interface portproxy add v4tov4 listenport=3000 listenaddress=0.0.0.0 connectport=3000 connectaddress=(WSL IP)
    netsh interface portproxy add v4tov4 listenport=5001 listenaddress=0.0.0.0 connectport=5001 connectaddress=(WSL IP)
    

    Note: Get WSL IP via wsl.exe hostname -I

    The result can be checked via netsh interface portproxy show all:

    image-20240722144432407

  6. My Dify webpage keeps loading

    You need to run npm run build and yarn start rather than npm run dev, in the later situation the compiling continues when you visiting your Dify.