181 lines
4.8 KiB
Markdown
181 lines
4.8 KiB
Markdown
---
|
|
title: Install LibreTranslate on a VM
|
|
summary: >
|
|
Summarized how I finally got LibreTranslate installed on my Archlinux based
|
|
local mastodon test-instance.
|
|
<small>I am not affiliated with LibreTranslate -- this post reflects
|
|
my own use case. The thumbnail is a trademark of
|
|
[LibreTranslate](https://github.com/LibreTranslate/LibreTranslate/blob/main/TRADEMARK.md).</small>
|
|
date: 2024-12-07T07:22:28+01:00
|
|
lastmod: 2025-01-06T15:16:13+0000
|
|
categories:
|
|
- computerstuff
|
|
tags:
|
|
- Archlinux
|
|
- command-line
|
|
- Linux
|
|
- Mastodon
|
|
- selfhost
|
|
- server
|
|
- Unraid
|
|
---
|
|
|
|
I usually run the mastodon instance on a <abbr title="virtual machine">VM</abbr>
|
|
with two cores and 4GB of RAM. That is plenty of power for the simple tasks a
|
|
single-user instance has to do.
|
|
|
|
## Preface / preparations
|
|
|
|
But for the installation of LibreTranslate I enhanced (after powering off the VM)
|
|
the cores to 4 and the RAM to 10GB.
|
|
|
|
While installing the needed python packages I ran out of space on the <kbd>/tmp</kbd>
|
|
filesystem (which was just a <kbd>tmpfs</kbd> filesystem).
|
|
|
|
### Modify <kbd>/etc/fstab</kbd> and increase <kbd>/tmp</kbd>
|
|
|
|
To increase <kbd>/tmp</kbd> we log into the VM and modify the file <kbd>/etc/fstab</kbd>.
|
|
|
|
```fstab
|
|
tmpfs /tmp tmpfs size=10G 0 0
|
|
```
|
|
|
|
Alternatively you can use another directory with more space as tmp storage:
|
|
|
|
```console
|
|
$ TMPDIR=/var/lib/libretranslate/tmp pip install --prefer-binary libretranslate
|
|
```
|
|
|
|
### Install some needed packages
|
|
|
|
I had much prepared/installed already, but `cmake` was still missing.
|
|
|
|
```console
|
|
$ paru -S cmake
|
|
```
|
|
|
|
## The installation of LibreTranslate
|
|
|
|
### Create a dedicated user for the service
|
|
|
|
```console
|
|
$ sudo useradd --create-home --home-dir /var/lib/libretranslate libretranslate
|
|
```
|
|
|
|
Change to the new user and install libretranslate in a virtual environment:
|
|
|
|
(I had problems with Python 3.12 so I'll stick with 3.11 for now)
|
|
|
|
```console
|
|
$ sudo su - libretranslate
|
|
$ python3.11 -m venv venv
|
|
$ source venv/bin/activate
|
|
$ pip install --prefer-binary libretranslate
|
|
$ argospm update
|
|
```
|
|
|
|
**Update:** You get an error similar to this one?
|
|
|
|
```console
|
|
(venv) [libretranslate@mastodon ~]$ pip install --prefer binary libretranslate
|
|
Traceback (most recent call last):
|
|
File "/var/lib/libretranslate/venv/bin/pip", line 5, in <module>
|
|
from pip._internal.cli.main import main
|
|
ModuleNotFoundError: No module named 'pip'
|
|
```
|
|
|
|
Most of the time this is because symlinks of python got updated and you haven't
|
|
installed all the modules on the new versions venv.
|
|
I solved this with a new installation on the new version, there might be easier
|
|
ways but I haven't found the time to look into that yet.
|
|
|
|
So I install pip again and repeat the installation within the new python versions venv.
|
|
|
|
```console
|
|
$ python -m ensurepip
|
|
```
|
|
|
|
### Install every language possible
|
|
|
|
```console
|
|
$ for lang in $(argospm search | cut -d: -f1 ); do echo argospm install $lang;done
|
|
```
|
|
|
|
Remove `echo` from that command to actually install them (the command above prints the commands
|
|
needed to install the languages; you can only install one at a time).
|
|
|
|
#### If you only want to translate to English, use this instead
|
|
|
|
```console
|
|
$ for lang in $(argospm search | grep -E "^translate-.._en.*"| awk '{ print $2 }' | xargs); do echo argospm install translate-${lang}_en; done
|
|
```
|
|
|
|
Also remove `echo` to actually install these translation packages.
|
|
|
|
### Let mastodon know of the LibreTranslate installation
|
|
|
|
```console
|
|
$ cd /var/lib/mastodon
|
|
```
|
|
|
|
Add to .env.production:
|
|
|
|
```env
|
|
ALLOWED_PRIVATE_ADDRESSES=127.0.0.1
|
|
LIBRE_TRANSLATE_ENDPOINT=http://127.0.0.1:5000
|
|
```
|
|
|
|
### Create a systemd unit file to automatically start LibreTranslate
|
|
|
|
File: <kbd>/etc/systemd/system/libretranslate.service</kbd>
|
|
|
|
```systemd
|
|
[Unit]
|
|
Description=LibreTranslate
|
|
After=network.target
|
|
|
|
[Service]
|
|
User=libretranslate
|
|
Group=libretranslate
|
|
WorkingDirectory=/var/lib/libretranslate/
|
|
ExecStart=/var/lib/libretranslate/venv/bin/python /var/lib/libretranslate/venv/bin/libretranslate --host 0.0.0.0 --port 5000 --suggestions --req-limit 20 --update-models
|
|
Restart=always
|
|
#CPUQuota=50%
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
```
|
|
|
|
### Start LibreTranslate
|
|
|
|
First, tell systemd of the new unit file:
|
|
|
|
```console
|
|
$ sudo systemctl daemon-reload
|
|
```
|
|
|
|
Enable and start the service:
|
|
|
|
```console
|
|
$ sudo systemctl enable --now libretranslate.service
|
|
```
|
|
|
|
## Restart Mastodon to let it know of the translation service
|
|
|
|
```console
|
|
$ sudo systemctl restart mastodon-web.service
|
|
```
|
|
|
|
## Remove the extra space from <kbd>/tmp</kbd>
|
|
|
|
Comment or remove the line in <kbd>/etc/fstab</kbd>
|
|
|
|
```fstab
|
|
#tmpfs /tmp tmpfs size=10G 0 0
|
|
```
|
|
|
|
## Reduce the VMs specs back to normal
|
|
|
|
I set the VM back to two cores and 4GB of RAM. Poweroff the VM
|
|
with `sudo poweroff` or `sudo systemctl poweroff` and change the values
|
|
back to normal. Start the VM again and all services should come online.
|