Install Pyenv
1. Install Python build dependencies
sudo apt-get install --no-install-recommends \
make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev \
xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
2. Install Pyenv
The source note contains three installation approaches.
Recommended
Run the Pyenv installer directly with curl.
GitHub installer
Download the installer from the Pyenv installer repository.
Shell script
Save the installer script to a file and execute it manually.
curl https://pyenv.run | bash
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
The original note also contains the complete
pyenv.sh installer script and
runs it with bash pyenv.sh.
The shorter installer commands above represent the same setup
workflow more compactly.
bash pyenv.sh
Configure Pyenv in the shell
Add Pyenv to .bashrc
The source note contains two alternatives for the initialization line.
echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init --path)"' >> ~/.bashrc
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
In the source note, the
pyenv virtualenv-init line is
added when using pyenv-virtualenv.
Reload the shell
exec "$SHELL"
Manage Python versions
Update Pyenv
pyenv update
Check the Pyenv version
pyenv --version
List available Python versions
pyenv install --list
Install a Python version
pyenv install <python-version>
# Example from the source note
pyenv install 3.8.1
Set the global Python version
pyenv global <python-version>
# Example
pyenv global 3.8.1
Uninstall Pyenv
The source note states that Pyenv is installed in
$PYENV_ROOT, which defaults to
~/.pyenv.
rm -fr ~/.pyenv
Remove the Pyenv configuration from .bashrc
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"
Environment with venv
The source note uses this workflow for Python 3.3 and later.
1. Choose the Python version
# Global
pyenv global <version>
# Local to the current project
pyenv local <version>
2. Create a project folder
Create a folder for the project and move into it before creating the virtual environment.
3. Create the virtual environment
python -m venv <environment-folder>
# Example from the note
python -m venv venv
4. Activate and deactivate
source venv/bin/activate
deactivate
Environment with pyenv-virtualenv
The source note uses pyenv-virtualenv for environments that can be associated directly with a Pyenv Python version.
1. Create a project folder
mkdir test
cd test
2. Set the local Python version
pyenv local <version>
# Example
pyenv local 3.8.1
3. Create the virtual environment
pyenv virtualenv <version> <environment-name>
# Example
pyenv virtualenv 3.8.1 env_3.8.1
4. Set the environment locally
pyenv local <environment-name>
# Example
pyenv local env_3.8.1
5. Activate and deactivate
pyenv activate <environment-name>
pyenv deactivate
Export and install packages
Export installed packages
pip3 freeze > requirements.txt
Install packages from requirements.txt
pip3 install -r requirements.txt