1. Home
  2. Linux
  3. Ubuntu
  4. How to Install Virtual Environment on Ubuntu 16.04

How to Install Virtual Environment on Ubuntu 16.04

How to Install Virtual Environment on Ubuntu 16.04
How to Install Virtual Environment on Ubuntu 16.04

In today’s tutorial, we will show you how to install Virtual Environment on Ubuntu 16.04. We may face issues if our Linux distribution only offers certain versions of Python and its packages since we actually need newer versions.
Check out Our Best VPS Hosting for scaling your cloud-based applications and processes.

Python

We can install new versions of Python on the server, of course, but this will make things more complicated because we will have some dependency issues when trying to compile everything we need.

Virtual Environment

Virtual environments make this very easy to manage and set up, as we can have different versions of Python in each environment, and they will be isolated from the main system. Installing Virtual Environment on Ubuntu 16.04 is a fairly easy task, and it shouldn’t take more than 10 minutes to finish.

Install dependencies

sudo apt install python-pip

Create virtual environment directory

mkdir /home/dreamvps/python

Now go to the directory you just created:

cd /home/dreamvps/python

Install virtualenv

sudo pip install virtualenv

Create a virtual environment

virtualenv dreamvps

The name of the virtual environment is “rose”. We can see some subdirectories inside “rose”. We want to focus on the bin directory. The bin directory contains a local copy of the python binary and the pip installer.
For example, if we want to install the “requests” package there, we can invoke:

bin/pip install requests
Collecting requests
Downloading requests-2.18.4-py2.py3-none-any.whl (88kB)
100% |████████████████████████████████| 92kB 2.0MB/s
Collecting certifi>=2017.4.17 (from requests)
Downloading certifi-2018.1.18-py2.py3-none-any.whl (151kB)
100% |████████████████████████████████| 153kB 2.5MB/s
Collecting chardet<3.1.0,>=3.0.2 (from requests)
Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB)
100% |████████████████████████████████| 143kB 2.6MB/s
Collecting idna<2.7,>=2.5 (from requests)
Downloading idna-2.6-py2.py3-none-any.whl (56kB)
100% |████████████████████████████████| 61kB 3.0MB/s
Collecting urllib3<1.23,>=1.21.1 (from requests)
Downloading urllib3-1.22-py2.py3-none-any.whl (132kB)
100% |████████████████████████████████| 133kB 2.7MB/s
Installing collected packages: certifi, chardet, idna, urllib3, requests
Successfully installed certifi-2018.1.18 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22

As you can see, we don’t use sudo, because we are installing on a virtual environment, not server-wide.
Run python shell inside the virtual environment
To run python shell inside the virtual environment, we can do this:

bin/python
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> requests.get('https://rosehosting.com')
>>> quit()

To make our work even easier, we can go into the virtual environment to use the pip and python command without typing bin/python and bin/pip.
We have to activate the new virtual environment to be able to do work there:

source rose/bin/activate

Now we can install a python app/package:

pip install requests

The command above is a command to install “requests”. This is the same as the one we previously installed. However, if you install it in a virtual environment, you only need to type pip install requests instead of bin/pip install requests.
When you are finished working in the virtual environment or want to switch to another one, you can deactivate the environment you are working in by invoking this command:

deactivate

One more thing…
Here are a few hand-picked guides for you to read next:

  1. How to install PyCharm on Ubuntu 16.04
  2. RPM Rebuild Database in CentOS: A Complete Tutorial

 

Updated on December 23, 2018

Was this article helpful?

Related Articles

Leave a Comment

[apsl-login-lite login_text='Please login with a social account']