Logo
← Back to Blog

Development and optimization

Pyenv Notes

A practical reference for installing Pyenv, managing multiple Python versions, creating environments with venv or pyenv-virtualenv, and exporting Python package requirements.

These notes collect the Pyenv installation steps and the Python environment workflows I used for switching Python versions and creating isolated environments.

Install Pyenv

1. Install Python build dependencies

Terminal
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.

A

Recommended

Run the Pyenv installer directly with curl.

B

GitHub installer

Download the installer from the Pyenv installer repository.

C

Shell script

Save the installer script to a file and execute it manually.

Recommended
curl https://pyenv.run | bash
Alternative
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
Script method

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.

Run saved installer
bash pyenv.sh

Configure Pyenv in the shell

Add Pyenv to .bashrc

The source note contains two alternatives for the initialization line.

Option 1
echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init --path)"' >> ~/.bashrc
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
Option 2
echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
Virtualenv line

In the source note, the pyenv virtualenv-init line is added when using pyenv-virtualenv.

Reload the shell

Terminal
exec "$SHELL"

Manage Python versions

Update Pyenv

Terminal
pyenv update

Check the Pyenv version

Terminal
pyenv --version

List available Python versions

Terminal
pyenv install --list

Install a Python version

Terminal
pyenv install <python-version>

# Example from the source note
pyenv install 3.8.1

Set the global Python version

Terminal
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.

Terminal
rm -fr ~/.pyenv

Remove the Pyenv configuration from .bashrc

Remove these lines
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

Terminal
# 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

Terminal
python -m venv <environment-folder>

# Example from the note
python -m venv venv

4. Activate and deactivate

Terminal
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

Terminal
mkdir test
cd test

2. Set the local Python version

Terminal
pyenv local <version>

# Example
pyenv local 3.8.1

3. Create the virtual environment

Terminal
pyenv virtualenv <version> <environment-name>

# Example
pyenv virtualenv 3.8.1 env_3.8.1

4. Set the environment locally

Terminal
pyenv local <environment-name>

# Example
pyenv local env_3.8.1

5. Activate and deactivate

Terminal
pyenv activate <environment-name>
pyenv deactivate

Export and install packages

Export installed packages

Terminal
pip3 freeze > requirements.txt

Install packages from requirements.txt

Terminal
pip3 install -r requirements.txt