Pyenv is an outstanding tool for managing multiple Python installations. Pyenv-virtualenv is a pyenv plugin that facilitates the creation and management of Python virtual environments with pyenv. This is a compelling proposition, making it possible to manage multiple Python versions with pyenv and provide the means to control the Python environment in a more granular manner.
These instructions are executed as the root user on a Liquid Web Self-Managed Ubuntu 18.04 server. It is not required to be logged in as the root user to run these commands though some may need sudo permissions.
These instructions assume pyenv is already installed and that pyenv has been used to install some version of Python. Looking for how to install pyenv? Check out our article How to Install Pyenv on Ubuntu 18.04.
Step 1: Update and Clone the Repository
It’s always a good idea before getting started installing new packages or software to make sure the system is up to date. To accomplish this, run the following command.
root@ubuntu:~# apt update -y
Once that task has finished, we can go ahead and clone the pyenv-virtualenv repository. The following command will clone the repository into the pyenv plugins directory:
root@ubuntu:~# git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
Step 2: Configuration
This step is optional; however, it has been included because of the additional value it adds when using pyenv and pyenv-virtualenv. Running the following command will allow pyenv-virtualenv to automatically activate a virtual environment if it has been set up to do so.
root@ubuntu:~# echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
Now to enable pyenv-virtualenv, we need to restart the shell:
root@ubuntu:~# exec “$SHELL”
Step 3: Verify the Installation
To verify that pyenv-virtualenv is working as expected, we will create a project directory, and change directories it into it.
root@ubuntu:~# mkdir python_project root@ubuntu:~# cd python_project/ root@ubuntu:~/python_project#
Once inside the project directory, it’s time to create a virtual environment. The syntax to create a virtual environment with pyenv-virtualenv is as follows.
To verify what versions of Python can be installed, run the following command.
root@ubuntu:~/python_project# pyenv versions * system (set by /root/.pyenv/version) 3.8.3
Create a Virtual Environment
Now, to create a virtual environment we run this command.
root@ubuntu:~/python_project# pyenv virtualenv 3.8.3 python-project-3.8.3 Looking in links: /tmp/tmpuwmdca68 Requirement already satisfied: setuptools in /root/.pyenv/versions/3.8.3/envs/python-project-3.8.3/lib/python3.8/site-packages (41.2.0) Requirement already satisfied: pip in /root/.pyenv/versions/3.8.3/envs/python-project-3.8.3/lib/python3.8/site-packages (19.2.3)
That’s it! Now recheck the available versions and the newly created virtual environment should be in the list:
root@ubuntu:~/python_project# pyenv versions * system (set by /root/.pyenv/version) 3.8.3 3.8.3/envs/python-project-3.8.3 python-project-3.8.3
Activating and Deactivating the Virtual Environment
To make things convenient in terms of activating and deactivating this virtual environment, run the following command.
root@ubuntu:~/python_project# pyenv local python-project-3.8.3 (python-project-3.8.3) root@ubuntu:~/python_project#
You should notice that the command prompt has changed, displaying the currently activated virtual environment. The pyenv local command adds a .python-version file to the directory and lets pyenv know which python environment to use whenever the directory is entered. Changing out of this directory will automatically deactivate the virtual environment.
(python-project-3.8.3) root@ubuntu:~/python_project# cd root@ubuntu:~#
Similarly, changing back into the directory automatically activates the virtual environment.
root@ubuntu:~# cd python_project/ (python-project-3.8.3) root@ubuntu:~/python_project#
However, this virtual environment can be used outside the project directory by invoking the pyenv activate command like so.
(python-project-3.8.3) root@ubuntu:~/python_project# cd root@ubuntu:~# pyenv activate python-project-3.8.3 pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1’ to simulate the behavior. (python-project-3.8.3) root@ubuntu:~#
It can then be deactivated manually by running this command.
(python-project-3.8.3) root@ubuntu:~# pyenv deactivate root@ubuntu:~#
That’s all there is to it! Pyenv, in combination with pyenv-virtualenv, makes the management of Python versions and environments a breeze. Installing multiple versions of Python on an Ubuntu system is simple and straightforward.
Having access to a tool that centralizes both the management of the Python version and the discreet environments, each of those versions has is something that saves time and makes the development process that much easier.
We pride ourselves on being The Most Helpful Humans In Hosting™!
Our support staff is always available to assist with any issues related to this article, 24 hours a day, 7 days a week 365 days a year.
We are available, via our ticketing systems at email@example.com, by phone (at 800-580-4986) or via a LiveChat or whatever method you prefer. We work hard for you so you can relax.
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.