Easy-to-Follow Guide of How to Install PyENV on Ubuntu | by Andi Ashari | Medium

Source: Easy-to-Follow Guide of How to Install PyENV on Ubuntu | by Andi Ashari | Medium

Andi Ashari
Jan 27, 2024

PyENV is an exceptionally versatile tool designed to handle multiple Python versions on a single machine. It simplifies the process of switching between various Python versions, making it an ideal choice for projects with specific version needs. This feature is incredibly beneficial in development settings where testing and compatibility with multiple Python versions are critical.

The Advantages of Using PyENV

  1. Versatile Python Version Management: Effortlessly switch and manage various Python versions.
  2. Custom Python Versions for Each Project: Assign a unique Python version to individual projects.
  3. Ensure Code Compatibility: Test your code with different Python versions for consistent compatibility.
  4. Independence from System Python: PyENV functions without relying on the default Python installation of the system.

Let’s embark on the journey of installing PyENV on your Ubuntu system.

Step-by-Step Installation of PyENV on Ubuntu

These instructions will guide you through the process of installing PyENV on Ubuntu.

Step 1: Update System Packages

Start by updating the system’s package list to ensure access to the latest software versions:

sudo apt update

Step 2 (Optional): Install Required Dependencies

PyENV may require certain dependencies to function optimally. You can install these using the following command:

sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

Step 3: Installing PyENV

Use this command to download and execute the PyENV installer script:

curl https://pyenv.run | bash

Step 4: Setting Up Environment Variables

Post-installation, incorporate PyENV into your system’s PATH with these steps:

For Users of the Bash Shell

If your default shell is Bash, append these lines to your ~/.bashrc file:

echo -e 'export PYENV_ROOT="$HOME/.pyenv"\nexport PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo -e 'eval "$(pyenv init --path)"\neval "$(pyenv init -)"' >> ~/.bashrc

For Zsh Shell Users

For those using Zsh, include these lines in your ~/.zshrc file:

echo -e 'export PYENV_ROOT="$HOME/.pyenv"\nexport PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo -e 'eval "$(pyenv init --path)"\neval "$(pyenv init -)"' >> ~/.zshrc

Step 5: Refresh Your Shell

Activate the new settings by refreshing your shell:

exec "$SHELL"

Step 6: Confirming PyENV Installation

Ensure that PyENV is correctly installed:

pyenv --version

A successful installation will display the version of PyENV.

Adding a Specific Python Version Using PyENV

After setting up PyENV, you can install the Python versions of your choice.

Step 1: View Available Python Versions

To see the list of installable Python versions:

pyenv install --list

Select your desired version from this extensive list.

Step 2: Python Version Installation

For example, to install Python 3.8.10:

pyenv install 3.8.10

This will download and set up Python 3.8.10.

Step 3: Select the Installed Version as Default (Optional)

To make this version the default for Python commands:

pyenv global 3.8.10

Alternatively, set it as the local version for a specific project by running pyenv local 3.8.10 in the project’s directory.

Executing Python Scripts Using PyENV

Here’s how you can run a Python script with the version installed via PyENV.

Step 1: Crafting a Python Script

Create a test script, for instance, test.py, with the following content:

# test.py
print("Hello from Python", python_version())

Step 2: Running the Script with PyENV

Execute the script with the installed Python version. If set as global or local, simply run:

python test.py

If not set as default, use PyENV’s specific execution command:

pyenv exec python3.8.10 test.py

This command will run test.py using Python 3.8.10.

Leave a Reply

The maximum upload file size: 500 MB. You can upload: image, audio, video, document, spreadsheet, interactive, other. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here