How to setup and start Seafile with a python virtual environment?

Following the installation manual of Seafile CE v9 I have to install several python dependencies with pip3. Now I am not a friend of installing system wide packages with an other package manager than the one of my operating system (Debian 11). Here a solution would be using a virtual environment but I can’t find any documentation on how to configure Seafile to use an existing one when starting.

Hello, i had the same wish as you do, and i inspired myself from “Installation with SQLite - Seafile Admin Manual” (cannot add links as a new member) and applied it to a Debian 11 (i’m using sqlite as i have a handful of users, and i decided to skip memcached, just because)

Install system base packages :

  • apt-get install -y python3-venv python3 python3-setuptools python3-pip pwgen sqlite3

Install system packages to be able to build some pip packages

  • apt-get install -y build-essential python3-dev libffi-dev

Then for the install itself :

  • mkdir -p ~/example
  • cd ~/example
  • tar xf seafile-server_9.0.9_x86-64.tar.gz
  • python3 -m venv venv
  • source venv/bin/activate

Install (or compile) pip packages

  • pip3 install --timeout=3600 django==2.2.* future Pillow pylibmc captcha jinja2 psd-tools django-pylibmc django-simple-captcha pycryptodome==3.12.0 cffi==1.14.0

Run the rest of the install according to the documentation…

Then finally stop everything, both seahub and seafile.
You were still using the venv settings implicitely
Now we want to work without it, so we “deactivate”

  • deactivate

This clears the venv environment variables, and ensure the wrapper script actually does its job properly (i.e. applying the venv settings for each command) instead of using your current shell “venv” environment variables, which is what we have been doing so far

Then go back to base folder :

  • cd ~/example

Now prepare a wrapper script to apply the venv before each command :

#!/usr/bin/env bash

[ $# -ne 2 ] && exit
[[ "$1" == "seafile.sh" || "$1" == "seahub.sh" ]] || exit
[[ "$2" == "start" || "$2" == "stop" || "$2" == "restart" ]] || exit

BASE=<<set to the absolute path to the folder containing seafile-server-latest and venv>>
source $BASE/venv/bin/activate
$BASE/seafile-server-latest/$1 $2

And put this script into the $BASE folder too (with venv and seafile-server-latest)

Then follow “Start Seafile at System Bootup - Seafile Admin Manual” (again, i cannot link as a new member) :

  • Just change ${seafile_dir} in these script as explaing, and set it to the same $BASE folder above (where venv and seafile-server-latest are)

Create the service files :

  • /etc/systemd/system/seafile.service


    ExecStart=${seafile_dir}/venv-wrapper.sh seafile.sh start
    ExecStop=${seafile_dir}/venv-wrapper.sh seafile.sh stop

  • /etc/systemd/system/seahub.service


    ExecStart=${seafile_dir}/venv-wrapper.sh seahub.sh start
    ExecStop=${seafile_dir}/venv-wrapper.sh seahub.sh stop

And finally use and test them :

  • sudo systemctl daemon-reload
  • sudo systemctl start seafile.service
  • ps auxf # to check that it is running
  • sudo systemctl start seahub.service
  • ps auxf # to check that it is running
  • sudo systemctl stop seahub.service
  • ps auxf # to check that it stopped
  • sudo systemctl stop seafile.service
  • ps auxf # to check that it stopped

Now you know the venv-wrapper is working correctly, enable auto-startup :

  • sudo systemctl enable seafile.service
  • sudo systemctl enable seahub.service

And reboot to verify that it is started automatically

Cheers,
Nicolas

Here i provide a very basic proof of concept using docker, to verify that it actually works and to see the bare minimum packages needed :slight_smile:

You can find the github repo (still cannot post links) : nipil/seafile-virtualenv

Considering that this post is nearly a year old and there is no mention of virtual (Python) environments in the Seafile documentation, I’m assuming that Seafile are not really supporting them.

https://manual.seafile.com/upgrade/upgrade_notes_for_11.0.x/


New Python libraries

Note, you should install Python libraries system wide using root user or sudo mode.