Posted: November 08, 2022

@SecFails

Photo Credit: @SecFails

Rootless python: Install python the safe way

.

The infamous /opt folder

Imagine it's Monday, and you're excited to start working on your new project. The first step is to set up your environment, so you turn to Google for guidance on how to install your favorite software. However, what you find is a flood of tutorials suggesting that you install it in the infamous /opt folder.
After you finally manage to install it, you realize that you can't use it without adding our possessive friend sudo on every call.
It's frustrating, isn't it?

The reason behind this predicament lies in the fact that the /opt folder is system-owned. This convention of installing third-party software in the /opt folder comes from the FHS convention from 1994 and it's outdated. Softwares like IntelliJ and PyCharm, which are installed in the /opt folder by default, have root access to your files.

However, there is a newer and better alternative!

The .local folder

This convention was introduced in 2007 as part of the XDG Base Directory Specification. The .local folder is user-owned and specifically designed for user-specific files and applications. It even mirrors the root folder / structure, making it intuitive to navigate.

In case of pyenv, most tutorials rely on installing at $HOME under the .pyenv folder, which is nice in terms of security, but since we have the XDG convention, why not follow it and install on the .local folder along with other softwares, making it easier to backup your stuff?

Installation

  1. First clone the pyenv project from https://github.com/pyenv/pyenv
git clone https://github.com/pyenv/pyenv.git
cp -r pyenv/ ~/.local/
# install dependencies to allow pyenv to build python versions from scratch
# for fedora:
dnf install make gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel tk-devel libffi-devel xz-devel patch

# for other OS versions check https://github.com/pyenv/pyenv/wiki#suggested-build-environment
# if you haven't previously done yet
echo PATH=\"\$HOME/.local/bin:\$PATH\" >> ${HOME}/.bashrc

# mandatory
echo 'export PYENV_ROOT="$HOME/.local/pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init --path)"' >> ~/.bashrc

# to reload .bashrc env into this terminal
source ~/.bashrc
# DONE!! to change python version execute
pyenv global [PYTHON_VERSION]
© 2025 — Matheus Toazza TuraLegal Information