Install LibreTranslate in a virtualenv

Ubuntu 24.04's system Python is externally managed (PEP 668) and refuses pip installs. Also bump LibreTranslate from 1.3.8 to 1.9.6, since the former pulls ctranslate2 2.24.0 which has no Python 3.12 wheels.
This commit is contained in:
raucao committed 2026-10-07 17:44:16 +02:00
1 parent b09808b12e
commit ec2645c5da
1 file changed
+15 -7
@@ -5,9 +5,10 @@
build_essential
version = "1.3.8"
version = "1.9.6"
venv = "/opt/libretranslate/venv"
%w{ python3 python3-pip python3-setuptools python3-dev }.each do |pkg|
%w{ python3 python3-pip python3-setuptools python3-dev python3-venv }.each do |pkg|
apt_package pkg
end
@@ -17,19 +18,26 @@ user "libretranslate" do
manage_home true
end
# LibreTranslate is installed into a dedicated virtualenv. Ubuntu 24.04's
# system Python is externally managed (PEP 668) and refuses `pip install`.
bash "create_libretranslate_venv" do
code "sudo -u libretranslate python3 -m venv #{venv}"
not_if { ::File.exist?("#{venv}/bin/pip") }
end
bash "install_libretranslate" do
code "sudo -u libretranslate pip3 install --user --prefer-binary libretranslate==#{version}"
code "sudo -u libretranslate #{venv}/bin/pip install --prefer-binary libretranslate==#{version}"
action :run
not_if { `sudo -u libretranslate pip3 list |grep libretranslate`.split(' ')[1] == version rescue false }
not_if "#{venv}/bin/pip show libretranslate 2>/dev/null | grep -q 'Version: #{version}'"
notifies :restart, "service[libretranslate]", :delayed
end
languages = `sudo -u libretranslate /opt/libretranslate/.local/bin/argospm search`
languages = `sudo -u libretranslate #{venv}/bin/argospm search`
languages.each_line do |line|
lang = line.split(':').first
bash "install_lt_#{lang}" do
code "sudo -u libretranslate /opt/libretranslate/.local/bin/argospm install #{lang}"
code "sudo -u libretranslate #{venv}/bin/argospm install #{lang}"
action :nothing
end
end
@@ -46,7 +54,7 @@ systemd_unit "libretranslate.service" do
User: "libretranslate",
Group: "libretranslate",
WorkingDirectory: "/opt/libretranslate/",
ExecStart: "/opt/libretranslate/.local/bin/libretranslate --host 127.0.0.1 --port 5000 --disable-files-translation",
ExecStart: "#{venv}/bin/libretranslate --host 127.0.0.1 --port 5000 --disable-files-translation",
Restart: "always"
},
Install: {