Setup Python on a server

Python is amazing, but a hell to manage between different versions, dependencies in particular if you work with data analysis.

Since a few version of some linux distributions, you are not allowed to install packages using pip. It’s a good thing as I broke my environment a few time due to inter-dependencies!

gabriel@GabSus:~$ sudo pip install venv
error: externally-managed-environment

You have to work with virtual environments. You have plenty of solutions, but I’ll focus on the one that work best for me. This tutorial is focused on Debian/Ubuntu, but can be transposed to other environments.

Install pyenv

PyEnv is a great way to manager different versions of python.

If you are not on Debian/Ubuntu, read this information.

Install dependencies (On Ubuntu / Debian)
sudo apt install libedit-dev curl build-essential libreadline-dev libssl-dev zlib1g-dev
libsqlite3-dev libffi-dev libbz2-dev liblzma-dev tk-dev (Source)

Next install pyenv

curl https://pyenv.run | bash

Edit the .bashrc file and add at the end

export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init - bash)"

Logout and login.

You can now install a specific version of python. As an example to install version 3.11.

pyenv install 3.11

Just copy the error in chatgpt if you see any issue 🙂

Check the version of python

As an example, if you have installed 3.11.

~/.pyenv/versions/3.11.11/bin/python --version

Install and test venv

Venv is a virtual environment where you can install your libraries and dependencies.

Create a folder with the name of your project (it’s easy to be lost!) and create a venv inside.

~/.pyenv/versions/3.11.11/bin/python -m venv venv

Activate this venv

Activate this virtual environment. It’s the only thing you will need to use in the future!

cd "folder of your project"
source venv/bin/activate

Install libraries

You can use a file called “requirements.txt” to install libraries in many projects.

pip install -r requirements.txt

Bonus: to create this file once your environment is setup correctly.

pip freeze > requirements.txt

If I want to install open-webui (a LLM web ui)

pip install open-webui
open-webui serve

I hope it answers many questions you had! Don’t forget that you have many different ways to do it, as an example by using conda, docket and other virtualization frameworks.

Leave a Reply

Your email address will not be published. Required fields are marked *