{"id":2080,"date":"2024-12-26T13:52:17","date_gmt":"2024-12-26T12:52:17","guid":{"rendered":"https:\/\/nuage.ch\/site\/?p=2080"},"modified":"2024-12-26T13:52:18","modified_gmt":"2024-12-26T12:52:18","slug":"setup-python-on-a-server","status":"publish","type":"post","link":"https:\/\/nuage.ch\/site\/setup-python-on-a-server\/","title":{"rendered":"Setup Python on a server"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Python is amazing, but a hell to manage between different versions, dependencies in particular if you work with data analysis.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Since a few version of some linux distributions, you are not allowed to install packages using pip. It&#8217;s a good thing as I broke my environment a few time due to inter-dependencies!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>gabriel@GabSus:~$ sudo pip install venv<br>error: externally-managed-environment<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You have to work with virtual environments. You have plenty of solutions, but I&#8217;ll focus on the one that work best for me. This tutorial is focused on Debian\/Ubuntu, but can be transposed to other environments.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Install pyenv<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">PyEnv is a great way to manager different versions of python. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are not on Debian\/Ubuntu, <a href=\"https:\/\/github.com\/pyenv\/pyenv?tab=readme-ov-file#installation\">read this information<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Install dependencies (On Ubuntu \/ Debian)<br>sudo apt install libedit-dev curl build-essential libreadline-dev libssl-dev zlib1g-dev <\/code>libsqlite3-dev libffi-dev libbz2-dev liblzma-dev tk-dev<code> (<a href=\"https:\/\/github.com\/pyenv\/pyenv\/wiki\/Common-build-problems\">Source<\/a>)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next install pyenv<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>curl https:\/\/pyenv.run | bash<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Edit the .bashrc file and add at the end<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>export PYENV_ROOT=\"$HOME\/.pyenv\"<br>[[ -d $PYENV_ROOT\/bin ]] &amp;&amp; export PATH=\"$PYENV_ROOT\/bin:$PATH\"<br>eval \"$(pyenv init - bash)\"<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Logout and login. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can now install a specific version of python. As an example to install version 3.11.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>pyenv install 3.11<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Just copy the error in chatgpt if you see any issue \ud83d\ude42<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Check the version of python<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As an example, if you have installed 3.11.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>~\/.pyenv\/versions\/3.11.11\/bin\/python --version<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Install and test venv<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Venv is a virtual environment where you can install your libraries and dependencies.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create a folder with the name of your project (it&#8217;s easy to be lost!) and create a venv inside.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>~\/.pyenv\/versions\/3.11.11\/bin\/python -m venv venv<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Activate this venv<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Activate this virtual environment. It&#8217;s the only thing you will need to use in the future!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>cd \"folder of your project\"<br>source venv\/bin\/activate<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Install libraries<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can use a file called &#8220;requirements.txt&#8221; to install libraries in many projects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>pip install -r requirements.txt<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Bonus: to create this file once your environment is setup correctly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>pip freeze > requirements.txt<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If I want to install open-webui (a LLM web ui)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>pip install open-webui<br>open-webui serve<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I hope it answers many questions you had! Don&#8217;t forget that you have many different ways to do it, as an example by using conda, docket and other virtualization frameworks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s a good thing as I broke my environment a few time due to inter-dependencies! gabriel@GabSus:~$ sudo pip [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-2080","post","type-post","status-publish","format-standard","hentry","category-fun"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p50cYU-xy","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/nuage.ch\/site\/wp-json\/wp\/v2\/posts\/2080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nuage.ch\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nuage.ch\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nuage.ch\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nuage.ch\/site\/wp-json\/wp\/v2\/comments?post=2080"}],"version-history":[{"count":1,"href":"https:\/\/nuage.ch\/site\/wp-json\/wp\/v2\/posts\/2080\/revisions"}],"predecessor-version":[{"id":2081,"href":"https:\/\/nuage.ch\/site\/wp-json\/wp\/v2\/posts\/2080\/revisions\/2081"}],"wp:attachment":[{"href":"https:\/\/nuage.ch\/site\/wp-json\/wp\/v2\/media?parent=2080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nuage.ch\/site\/wp-json\/wp\/v2\/categories?post=2080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nuage.ch\/site\/wp-json\/wp\/v2\/tags?post=2080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}