Python: Version Management with Pyenv and Pyenv-Virtualenv (Linux)
Pyenv is one of the coolest tool for managing multiple Python versions in your dev environment. Whether it’s your new machine learning venture or a full fledged product development initiative you will find pyenv
super handy to setup your Python environments super fast and efficiently. So lets hop right into it.
Setting up Pyenv and Installing Python
Here I will be setting up pyenv
on window’s linux subsystem running Ubuntu 18.04 and installing latest version of python i.e. 3.10.1
(version as of authoring date).
- Updating the System
If you use Ubuntu this is the first natural step you need to take before installing anything new. Copy the following command in your shell.
sudo apt update -y
sudo
is optional depending on the account and system requirement for admin access.
2. Installing Dependencies
Pyenv requires following dependencies so install them by copying the following command in your shell.
apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git
3. Getting the Git Repo
Clone the following repo. This will get you the latest pyenv
version from Github.
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
4. Configuring Environment Variables and the Environment
Use the following commands to setup the PYENV_ROOT
and PATH
variables in .bashrc
file of your Ubuntu. Secondly setup the pyenv init
to initialize it. Run the three commands in your shell.
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc
5. Restart Shell
Now you need to restart your shell with following command.
exec bash
Or
exec "$SHELL"
If you’re using zsh
then use the following command.
exec zsh
6. Verify that Pyenv is Installed
Check if your pyenv
is installed properly with the following command pyenv versions
and the corresponding output will look like as shown below.
user@ubuntu:/mnt/c/Users/user$ pyenv versions
* system
It shows that the current version of the python is selected to default.
7. Installing Python
To list all the available versions of python to be installed use the following command.
pyenv install --list
Now choose the version you want and install. Here I am installing the latest version available. This command generally takes a long time to run because the selected version of python is being built from source.
pyenv install 3.10.1
Once installation is done check by running following code.
user@ubuntu:/mnt/c/Users/user$ pyenv versions
* system (set by /mnt/c/Users/user/.python-version)
3.10.1
Now your python 3.10.1
is all set to go. Lets now setup the pyenv-virtualenv
.
Installing Pyenv-virtualenv
Now we will be installing pyenv-virtualenv
which helps us setup local dev environments without making the global installation of python dirty.
- Clone the Git Repo
Let us first clone the virtualenv
repo from Github.
git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
2. Environment Configuration
Run the following command to automatically enable environments when shell is run.
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
3. Restart the Shell
Now you need to restart your shell with following command.
exec bash
Or
exec "$SHELL"
If you’re using zsh
then use the following command.
exec zsh
4. Verify Installation
To verify if virtualenv
is installed properly, run the following command.
user@ubuntu:/mnt/c/Users/user$ pyenv virtualenv --version
pyenv-virtualenv 1.1.5 (python3.10 -m venv)
Now you have installed pyenv-virualenv
successfully.
Creating & Using Pyenv-Virtualenv
Now let us see how to create and use pyenv-virtualenv
.
- Creating Virtualenv
Run the following command to create a new virtualenv
. To create the environment we need to provide the python version from the list of pythons installed in the system. List of installed pythons can be accessed by pyenv versions
. Second argument is the name of the virtual environment.
pyenv virtualenv 3.10.1 venv-name-3.10.1
Now that the virtualenv
is created check if it’s properly created by running following command.
user@ubuntu:/mnt/c/Users/user$ pyenv versions
* system (set by /mnt/c/Users/user/.python-version)
3.10.1
3.10.1/envs/venv-name
venv-name
2. Activating & Deactivating Virtualenv
To activate the virtualenv
run the following command.
pyenv activate venv-name
To deactivate run the following command.
pyenv deactivate
To automatically activate local env when you enter your local development folder of your project, run the following command inside your folder.
pyenv local venv-name
Now this should result in the following,
(venv-name) user@ubuntu:/mnt/c/Users/user/project-folder$
and now if you cd
out of the current folder you will get following
(venv-name) user@ubuntu:/mnt/c/Users/user/project-folder$ cd..
user@ubuntu:/mnt/c/Users/user$
In this tutorial your learnt how to install pyenv
, different versions of python and pyenv-virtualevn
. Enjoy your next python project without any hassle.
I am a data engineer experienced in GCP, SQL and Python stack if you need help to setup data infrastructure for your startup or next business venture feel free to reach out to me on my Linkedin.