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 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”

How to Install and Correct Dependencies Issues in Ubuntu

Reading Time: 6 minutes

What is a Dependency?

A dependency is defined as a file, component, or software package that a program needs to work correctly. Almost every software package we install depends on another piece of code or software to work as expected. Because the overall theme of Linux has always been to have a program do one specific thing, and do it well, many software titles utilize other pieces of software to run correctly.

Introduction

Let’s review what dependencies are and why they are required. We all have, at one point or another, most certainly seen a message from our system when we were installing software regarding “missing dependencies.” This error denotes that a required part of the software package is outdated, unavailable or missing. Let’s review how to address those issues when we come across them on Ubuntu.

Continue reading “How to Install and Correct Dependencies Issues in Ubuntu”

How to Add or Remove a PPA in Ubuntu 18.04

Reading Time: 5 minutes

What is a PPA?

A PPA (or Personal Package Archive) is a software repository provided by members of the Ubuntu Linux community. Software contained in a PPA can be downloaded and installed via apt, Ubuntu’s defacto package management system.

Typically, PPA’s will contain new or updated software to existing packages that may not be available from the official Ubuntu package repositories. This allows users to have more granular control over when specific software packages are updated on their systems. It is important to be discerning when utilizing PPA’s and to only add a PPA from a trusted source.

The best reason for adding software via a PPA, is the server will get automatic updates to the installed software when updates are run.

Continue reading “How to Add or Remove a PPA in Ubuntu 18.04”

How to Install Wireguard on Ubuntu 18

Reading Time: 9 minutes

A Fast, Modern and Secure VPN Tunnel

Objective

In this tutorial we will learn what Wireguard is, what it is used for, how to install and configure it, and lastly, how to use it to it wisely. 

What is Wireguard? 

Wireguard is an open-source, dependable, advanced, VPN tunneling software you can install and use right now to create a secure, point-to-point connection to a server.

Continue reading “How to Install Wireguard on Ubuntu 18”

How To Install The Latest Kernel Version

Reading Time: 7 minutes

In this tutorial, we will learn how to install the latest kernel version on multiple Linux distributions. 

What Is A Kernel

terminal.cli.2.20.20

First, let’s define what a kernel is defined as. The Linux kernel is basically the brain of your hardware. Its main purpose is to facilitate communications between your hardware and software. As an example, if an application needs to make a change (say switching the screen resolution of your monitor), the software submits a request to the kernel, and the kernel uses the available video driver options to modify the resolution. 

Continue reading “How To Install The Latest Kernel Version”

How To Install Software From Source on Ubuntu

Reading Time: 6 minutes

In this article, we are going to cover the available options for installing software on a Dedicated Ubuntu server. By the end of this article, you’ll be equipped with an arsenal of tools for installing the exact software you want on your Ubuntu server! We will be installing three different software packages from source, deb, and flatpack.

Continue reading “How To Install Software From Source on Ubuntu”