VPS for Python: environment, autostart, memory and logs
A Python script on a server does not need much: an interpreter of the right version, isolated dependencies, a way of starting it that survives a reboot, and a log that shows what happened overnight. Below is how to set this up on a rented VPS and how to estimate memory so that you neither overpay nor run into the OOM killer. The table above lists plans with 1 GB of memory or more on Ubuntu, with hourly billing and sorting by price per core.
Python version and virtual environment
The system Python belongs to the distribution: the operating system's own utilities run on it. In Ubuntu 24.04 it is Python 3.12, in Debian 13 it is 3.13. If the project needs another branch, install it alongside with pyenv, uv or maintainer packages, but do not replace the system one. On recent Debian and Ubuntu the pip install command outside an environment fails with externally-managed-environment: this is a safeguard, not a fault, and there is no need to switch it off with the --break-system-packages flag.
A working layout for each project:
- a separate user without root rights and a directory such as
/opt/project-name; - an environment created with
python3 -m venv .venvoruv venvinside the directory; - dependencies with pinned versions in
requirements.txtor a lock file, so that reinstalling gives the same result; - tokens and passwords in a
.envfile with 600 permissions, not in the code or the repository.
The environment does not need to be "activated" in services and cron: it is enough to call the interpreter by its full path, for example /opt/app/.venv/bin/python main.py.
A permanent process or a scheduled task
A bot, a queue listener or an API service must run continuously, so they are set up as a systemd unit. It specifies User=, WorkingDirectory=, EnvironmentFile= with the path to .env, ExecStart= with the interpreter from the venv, and Restart=on-failure. After systemctl enable --now the process starts at boot and comes back after a crash. Add Environment=PYTHONUNBUFFERED=1, otherwise print output accumulates in a buffer and reaches the journal late.
A script that does a job and exits (exporting a report, collecting data once an hour, cleaning the database) is easier to run on a schedule. The classic way is cron, and it has three typical traps: a different environment (your variables and PATH are missing), relative paths that resolve from the home directory, and overlapping runs when the previous one has not finished yet. Write full paths, redirect output to a file and wrap the command in flock -n so that a second copy does not start. The alternative is systemd timers: they share a journal with the services, have protection against parallel runs out of the box, and the Persistent=true parameter runs a missed job after the server has been down.
How much memory a process takes
The bare interpreter takes about 10 MB, but the real figure is determined by libraries. Guidelines for a single process:
- a script on requests or httpx with a little logic: 30–60 MB;
- an asynchronous bot or a FastAPI web service: 60–150 MB per worker;
- importing pandas and numpy: 100 MB or more extra even before loading data, and a table in memory is usually several times heavier than the CSV file;
- a headless browser through Playwright or Selenium: from 300 MB per instance.
Multiply by the number of processes: multiprocessing and gunicorn workers each hold their own copy of the application. Two or three light scripts or bots live comfortably on 1 GB; for pandas, browsers and several workers take 2–4 GB. Enable a small swap file as insurance against a rare peak and set MemoryMax= in the unit so that a leak in one script does not bring down the others. Check actual consumption in systemctl status or with ps -o rss. There is a separate selection for bots, VPS for a Telegram bot, and one-off heavy computations are cheaper to run on a VPS with hourly billing, deleting the server when the job is done.
Logs and scrapers
Use the logging module with levels and a timestamp on every line instead of print. Service output is collected by journald: read it with journalctl -u service-name and limit its size with the SystemMaxUse parameter. If a script writes its own files, add RotatingFileHandler or logrotate: a forgotten log of several gigabytes is a common cause of a full disk on a small plan.
Python scrapers follow the same rules plus courtesy towards other people's sites. Check robots.txt (the standard library has urllib.robotparser) and the site's terms of use, keep pauses between requests, send an honest User-Agent with contact details, cache what you have already downloaded and do not collect personal data. If the site has an API or a data export, take the data from there. Bot protection and captchas are not to be bypassed: it breaks the resource's rules, and the host may stop the server after a complaint. Choosing a server for data collection is covered on the VPS for web scraping page.