LibreTranslate is a free and open‑source machine translation API that lets you host your own translation service—no reliance on proprietary APIs like Google Translate. In this comprehensive guide, you’ll learn how to set up LibreTranslate on a DigitalOcean VPS, configure Nginx to serve your domain, run LibreTranslate as a systemd service for auto‑start, and even integrate its Python API into your own projects.
Affiliate Tip:
💻 Get started with cloud or VPS hosting with DigitalOcean and receive a $200 credit for 60 days through our link 👇
https://watsontechworld.com/digitalocean
Introduction
Watch our full step-by-step video tutorial on setting up LibreTranslate 👇
In this guide, we cover everything from setting up a DigitalOcean droplet to running your own LibreTranslate service with an easy‑to‑use Python API. Whether you’re a developer looking to create your own translation website or a language enthusiast wanting to experiment with open‑source solutions, this tutorial provides all the essential steps.
LibreTranslate leverages the open‑source Argos Translate library and supports multiple languages—so you can run translations quickly on your own server.
Overview of LibreTranslate
LibreTranslate is a self‑hosted translation API that:
- Provides an alternative to proprietary machine translation services.
- Is powered by the open‑source Argos Translate engine.
- Allows both a web interface (similar to Google Translate) and an API for automated translations.
For more information, check out the LibreTranslate website and their GitHub repository.
Setting Up Your DigitalOcean VPS
Before installing LibreTranslate, you need to set up a VPS. Here’s a summary of the process:
- Create a Droplet on DigitalOcean
– Choose Ubuntu LTS as your operating system and a plan with at least 2GB of RAM. - Access the VPS via SSH:
ssh root@your_vps_ip_address
- Create a Non‑root User and Add to sudo Group:
adduser example_username usermod -aG sudo example_username reboot now # Then log in with: ssh example_username@your_vps_ip_address
- Update the Server:
sudo apt update && sudo apt upgrade -y
Installing LibreTranslate with pipx
LibreTranslate is installed using pipx (without sudo) so that it is isolated for your user. Follow these steps:
- Install pipx and pip:
sudo apt install pipx python3-pip -y pipx ensurepath source ~/.bashrc
- Install LibreTranslate:
pipx install libretranslate
- Run LibreTranslate (for first‑time setup):
libretranslate
Configuring Nginx for Domain Access
To serve your LibreTranslate service through your domain rather than the raw VPS IP, set up Nginx:
- Install Nginx (and optionally a text editor like nano or vim):
sudo apt update sudo apt install nginx vim -y
- Create a Configuration File for Your Subdomain:
Open a new config file for your domain (change libretranslate_subdomain.example.com to your actual domain or subdomain)sudo nano /etc/nginx/sites-available/libretranslate_subdomain.example.com
Paste in the following configuration (change libretranslate_subdomain.example.com to your actual domain or subdomain):
server { listen 80; server_name libretranslate_subdomain.example.com; location / { proxy_pass http://localhost:5000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } - Enable the Configuration and Reload Nginx: (change libretranslate_subdomain.example.com to your actual domain or subdomain)
sudo ln -s /etc/nginx/sites-available/libretranslate_subdomain.example.com /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx
- Set Up DNS:
Log in to your domain registrar (or Cloudflare) and add an A record pointinglibretranslate_subdomainto your VPS IP.
Running LibreTranslate as a systemd Service
To ensure your translation service is always running—even after reboots—you can set it up as a systemd service.
- Create the Service File:
sudo nano /etc/systemd/system/libretranslate.service
Paste the following (replace
example_usernamewith your actual username):[Unit] Description=LibreTranslate Server After=network.target [Service] User=example_username Group=example_username WorkingDirectory=/home/example_username ExecStart=/home/example_username/.local/bin/libretranslate --host 127.0.0.1 --port 5000 Restart=always Environment=PATH=/home/example_username/.local/bin:/usr/bin:/bin [Install] WantedBy=multi-user.target
- Reload and Enable the Service:
sudo systemctl daemon-reload sudo systemctl start libretranslate sudo systemctl enable libretranslate sudo systemctl status libretranslate
- Test Access:
– Verify that accessing your VPS IP directly (with port 5000) is forbidden, but your domain (e.g.,http://libretranslate_subdomain.example.com) works.
Using the Python Translation API
LibreTranslate exposes a RESTful API that you can use with Python. Below are some sample code snippets:
Example 1: Translate from English to Spanish
import requests
import json
url = "http://libretranslate_subdomain.example.com/translate"
headers = {"Content-Type": "application/json"}
data = {
"q": "I want to eat bread and drink tea.",
"source": "en",
"target": "es",
"format": "text",
"alternatives": 3,
"api_key": ""
}
response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.json())
Example 2: Batch Translation with Python and Pandas
import requests
import json
import pandas as pd
domain = 'libretranslate_subdomain.example.com'
url = f"http://{domain}/translate"
headers = {"Content-Type": "application/json"}
sentences = [
"I have 2 dogs and I like to play with them.",
"My favorite food is blueberries and I love Italian and Indian food.",
"I like to study Spanish.",
"I am a data scientist and an entrepreneur.",
"I might want to get a PhD in Colombia or Chile.",
"My favorite video game is StarCraft 2.",
"I want to learn to speak Spanish well."
]
translations = []
for sentence in sentences:
data = {
"q": sentence,
"source": "en",
"target": "es",
"format": "text",
"alternatives": 3,
"api_key": ""
}
response = requests.post(url, headers=headers, data=json.dumps(data))
translations.append(response.json().get('translatedText'))
df = pd.DataFrame({
'Source Sentence': sentences,
'Translated Sentence': translations
})
df.to_csv('translations.csv', sep='\t', index=False)
print(df)
This code demonstrates how to translate multiple sentences and write the results to a CSV file for further analysis.
Conclusion
By following this guide, you now have a fully functioning LibreTranslate instance running on your DigitalOcean VPS. You’ve learned how to install LibreTranslate, configure Nginx for your domain, run the service automatically with systemd, and interact with the translation API using Python. This self‑hosted solution not only gives you full control over your translation needs but also paves the way for scalable language projects.
Affiliate Reminder:
If you’re ready to host your own LibreTranslate service, consider using DigitalOcean. Get started with DigitalOcean and enjoy a $200 credit for 60 days — it’s a fantastic way to launch your project without breaking the bank.
🚀 Embark on an Epic Time-Travel Adventure with Watson! 🐕🦖🎨
Take a journey through time with Watson the Time-Travelling Dog: Dogs and Dinosaurs Adventure Coloring Book 1.
Join Watson and his dog brother Sherlock as they meet dinosaurs, make new friends, and embark on an unforgettable adventure! Perfect for kids, dog lovers, and coloring book enthusiasts alike.
Why you'll love it:
- Engaging, fun-filled story
- Beautiful illustrations to color
- Ideal for family fun or a thoughtful gift
Ready for an adventure that sparks creativity and fun? 📖 Grab your copy today and let the time-traveling excitement begin!
Get your book now 👇