Creating a Virtual Environment for Python on Ubuntu 16.04
What Is Virtualenv?
Virtualenv is a tool that creates an isolated environment separate from other projects. In this instance, we will be installing different Python versions, including their dependencies. Creating a virtual environment allows us to work on a Python project without affecting other projects that also use Python. It will utilize Python's core files on the global environment to run, thus saving you disk space while providing the freedom to use different Python versions for separate apps or projects.
Preflight
- Pip installation is required this will install Python at the same time.
- Logged in as root or a user with admin privileges on an Ubuntu VPS LTS server. If logged in with a regular user with admin privileges be sure to use sudo before the commands discussed within this tutorial.
Step 1: Install Virtualenv
First, we will update our apt-get, then we will install the virtualenv module.
apt-get update
apt-get install python-virtualenv
Virtualenv works by creating a folder that houses the necessary Python executables in the bin directory. In this instance, we are installing Python 3.5 while also creating two folders, the virtualenvironment, and project_1 directory.
virtualenv -p /usr/bin/python3 virtualenvironment/project_1
Virtualenv will create the necessary directories in the project_1 directory. In this directory you’ll find bin, include, lib, local and share.
Step 3: Activate Your Virtual Environment
Navigate to the project_1/bin directory and activate your new environment from within that folder by using the source command below. Anytime you need to work on your project you will need to enable with the following command.
cd virtualenvironment/project_1/bin
source activate
Or if you are outside the bin directory, you can use the following command.
$ source virtualenvironment/project_1/bin/activate
You’ll see that you are now in this newly created environment by noting the change in the shell prompt, which reflects the name we created in Step 2.
(project_1) root@host2:~#
When Python packages are installed, they will live in the lib directory, project_1/lib/python3.5/site-packages.
Exit your virtual environment by typing:
(project_1) root@host2:~# deactivate
Want To Know More?
Using Virtualenv with Python allows you to take advantage of multiple, isolated environments in which you can freely create, modify, or remove your applications. Both our Dedicated servers or VPS Cloud servers can easily utilize this technology to improve your productivity and provide a stable development platform from which you can launch your apps!
Give us a call at 800.580.4985, or open a chat or ticket with us to speak with one of our knowledgeable Solutions or Experienced Hosting advisors to learn how you can take advantage of these techniques today!
Related Articles:
- 4 Methods for How to Install Yarn on Windows Server
- How to Install Bpytop Resource Monitoring Tool on AlmaLinux
- How to Fix “This Site Can’t Provide a Secure Connection” Error
- How to Install MongoDB on AlmaLinux
- How to Install PostgreSQL on AlmaLinux
- How to Use the WP Toolkit to Secure and Update WordPress
About the Author: Justin Palmer
Justin Palmer is a professional application developer with Liquid Web
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.
Latest Articles
What Is WebP and What Makes it Different from Other Image Formats?
Read ArticleTop 10 Password Security Standards
Read ArticleTop 10 Password Security Standards
Read ArticleHow to Install MongoDB on AlmaLinux
Read ArticleHow to Use the WP Toolkit to Secure and Update WordPress
Read Article