How to Install Pyenv-virtualenv on Ubuntu 18.04

Reading Time: 3 minutes
python-logo

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.

Preflight Check

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.

pyenv-virtualenv

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 support@liquidweb.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.

How Has Containerization Changed the World?

Reading Time: 5 minutes
container port stacked

The arrival of containerized software has drastically changed the landscape of web hosting, and web application provides. The simplification and speed that comes with containers make deploying services like websites so efficient that the traditional model of dedicated servers running specific web-based software is almost obsolete. This being said, there will always be a place for virtual server projects and dedicated servers handling specific tasks like HIPAA compliant hosting services.

Continue reading “How Has Containerization Changed the World?”

How To Install Git on Ubuntu 16.04 LTS

Reading Time: 2 minutes

Git is one of the most popular tools used as a distributed version control system (VCS). Git is commonly used for source code management (SCM) and has become more used than old VCS systems like SVN. In this article, we’ll show you how to install Git on your Ubuntu 16.04 cloud dedicated server.

Continue reading “How To Install Git on Ubuntu 16.04 LTS”

How to Install Pyenv on Ubuntu 18.04

Reading Time: 3 minutes

What is Pyenv?

Pyenv is a fantastic tool for installing and managing multiple Python versions. It enables a developer to quickly gain access to newer versions of Python and keeps the system clean and free of unnecessary package bloat. It also offers the ability to quickly switch from one version of Python to another, as well as specify the version of Python a given project uses and can automatically switch to that version. This tutorial covers how to install pyenv on Ubuntu 18.04.

Continue reading “How to Install Pyenv on Ubuntu 18.04”

How to Uninstall Software in Ubuntu 18.04

Reading Time: 6 minutes

Introduction

In this tutorial, we will demonstrate how to quickly and easily uninstall software from Ubuntu. Uninstalling software can be done using several techniques. In this article, we will show you how to uninstall software using these various methods.

Continue reading “How to Uninstall Software in Ubuntu 18.04”

Docker For Beginners

Reading Time: 16 minutes

What is Docker?

31920.docker

Docker is a containerization software that is used for automating the deployment and management of applications within an isolated environment. This software allows us to “pack” and ship an application, along with all of its needed files, libraries, and dependencies, into a “docker container“. That container can then be easily ported to any Linux system that contain cgroups support within the kernel, and provides a container management environment. Docker is one of several containerization implementations (not to be confused with virtualization) based on this cgroups mechanisms built into the Linux kernel.

Continue reading “Docker For Beginners”

How to Install Yarn on Ubuntu 18.04

Reading Time: 5 minutes

What is Yarn?

Yarn is a package management tool expressly designed to speed up the installation of packages from the npm registry by running operations in parallel to increase speed, improve reliability, and boost security. Yarn allows us better to automate the installation processes of npm software packages and is also advantageous when upgrading, making configuration changes, or removing npm packages. 

Continue reading “How to Install Yarn on Ubuntu 18.04”

How to Install Docker on CentOS 8

Reading Time: 6 minutes

What is Docker?

Before we begin, let’s describe what Docker is. Docker is a set of virtualization tools that allows us to create, test, and deploy containerized applications quickly and easily on a dedicated server. It has become very popular and used almost everywhere in our daily lives. Thanks to containerization, we can quickly launch applications on different private cloud host platforms utilizing small bundles which contain all the needed packages, libraries and configuration file to run an application. These docker packages communicate via established network channels.

Docker Silhouette
Docker Silhouette
Continue reading “How to Install Docker on CentOS 8”

Vagrant: An Overview

Reading Time: 13 minutes

Setting up Development Environment with Vagrant

In this tutorial, we will demonstrate how to install Vagrant on multiple operating systems.

Continue reading “Vagrant: An Overview”