From ec2645c5da7ec8bf70727a41ac9a9e254219e9c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Wed, 7 Oct 2026 17:44:16 +0200 Subject: [PATCH] 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. --- .../kosmos-mastodon/recipes/libretranslate.rb | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/site-cookbooks/kosmos-mastodon/recipes/libretranslate.rb b/site-cookbooks/kosmos-mastodon/recipes/libretranslate.rb index d06557d..3cb3e49 100644 --- a/site-cookbooks/kosmos-mastodon/recipes/libretranslate.rb +++ b/site-cookbooks/kosmos-mastodon/recipes/libretranslate.rb @@ -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: {