Level up your Python game: Unleashing Pyenv in Ubuntu!

If you’re a Python developer who finds themselves juggling multiple Python versions on an Ubuntu system, you may have noticed that the deadsnakes ppa doesn’t provide install candidates for kinetic (22.10). This can be a frustrating roadblock, especially if you’re trying to work with Tensorflow extended (TFX), which doesn’t cooperate smoothly with Python 3.10.

Fortunately, there’s a solution: pyenv. Pyenv is a remarkable tool that simplifies the installation, management, and switching between different Python versions effortlessly. In this blog post, we’ll walk you through the process of installing pyenv on Ubuntu, empowering you to effortlessly manage multiple Python versions on your system.

Step 1: Update System Packages

Before installing pyenv, we need to update the system packages to their latest version. Run the following command to update the system packages:

sudo apt update && sudo apt upgrade -y

Step 2: Install Dependencies

pyenv requires some dependencies to be installed on your system. Run the following command to install the dependencies:

sudo apt install -y 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

Step 3: Install pyenv

Once the dependencies are installed, we can proceed with the installation of pyenv. Run the following command to install pyenv [1]:

curl https://pyenv.run | bash

This command will download and install the latest version of pyenv on your system. Once the installation is complete, add the following lines to your ~/.bashrc file to set up pyenv [2]:

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Then, run the following command to reload your ~/.bashrc file:

source ~/.bashrc

Depending on your setup, you may have one or more of the following files: ~/.profile, ~/.bash_profile or ~/.bash_login. If any of these files exist, it is recommended to add the commands there. However, if none of these files are present, you can simply add the commands to ~/.profile for seamless integration.

Step 4: Verify Installation

To verify that pyenv is installed correctly, run the following command:

pyenv --version

This command should output the version number of pyenv installed on your system.

Step 5: Install Python

Now that pyenv is installed, you can use it to install any Python version you need. To install Python 3.9.0, for example, run the following command:

pyenv install 3.9.0

This will download and install Python 3.9.0 on your system. Once the installation is complete, you can set this version of Python as the default by running the following command:

pyenv global 3.9.0

Step 6: Set Up a Virtual Environment

Now that you have installed pyenv, you can use it to create and manage virtual environments for your Python projects. To create a virtual environment for your project, run the following command:

pyenv virtualenv 3.9.0 tfx_venv

This command will create a new virtual environment named tfx_venv, based on Python 3.9.0. The virtual environment will be stored at $HOME/.pyenv/versions/3.9.0/tfx_venv .

Pyenv’s standout feature lies in its ability to set specific environments based on individual folders. Imagine a scenario where you require Python 3.9 for your trading-gcp folder, while the rest of your projects calls for Python 3.10.

├── courses
│   ├── coursera
│   │   ├── trading-gcp
│   │   └── udemy - Algorithmic Trading with Machine Learning in Python

With pyenv, achieving this is a breeze. Simply navigate to the trading-gcp folder and run the command

pyenv local tfx_venv

Pyenv will automatically configure the environment to match your folder’s requirements. This is accomplished by creating a .python-version file within the folder, which references the tfx_venv environment.

(tfx_venv) trading-gcp$ cat .python-version 
tfx_venv

If we want to reset the python environment associated with this folder then we simply need to remove this file or run another pyenv local command.

Managing virtual environments becomes hassle-free with pyenv. You no longer need to worry about manual activation or deactivation. As you navigate to different folders, pyenv seamlessly adjusts the Python environment to suit the code contained within. When you leave the folder, the environment switches back automatically.

Conclusion

In this tutorial, we have gone through the steps to install pyenv on Ubuntu. pyenv is a useful tool that can help you manage multiple Python versions on your system with ease. With pyenv, you can switch between different Python versions and install the required packages for each version without any conflicts.

References

[1] pyenv installer, https://github.com/pyenv/pyenv-installer
[2] pyenv: imple Python Version Management: pyenv. https://github.com/pyenv/pyenv
[3] “Introduction to pyenv” by Real Python. https://realpython.com/intro-to-pyenv

Leave a comment