How to Install Virtual Environment on Ubuntu 16.04

Spread the love

In today’s tutorial we will show you, how to install Virtual Environment on Ubuntu 16.04. We may face issues when our Linux distribution only offers certain versions of Python and its packages, when we actually need newer versions. We can install new versions of Python on the server, of course, but this will be more complex because we will have some dependency issues when trying to compile everything we need. Virtual environment make this very easy to manage and set up, we can have different versions of Python in each environment, and it will be isolated from the main system. Installing Virtual Environment on Ubuntu 16.04 is fairly easy task and it shouldn’t take more than 10 minutes to finish.

1. Install dependencies

:~$ sudo apt install python-pip

2. Create virtual environment directory

:~$ mkdir /home/rosehosting/python

Now go to the directory you just created

:~$ cd /home/rosehosting/python

3. Install virtualenv

:~/python$ sudo pip install virtualenv

4. Create a virtual environment

:~/python$ virtualenv rose

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

:~/python/rose$ 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 install it on a virtual environment, not server wide.

5. Run python shell inside the virtual environment

Now, to run python shell inside the virtual environment, we can do this:

:~/python/rose$ 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 the work even easier, we can go into the virtual environment to use pip and python command without typing bin/pyton and bin/pip

We have to activate the new virtual environment to be able to do works there

:~/python$ source rose/bin/activate

Now we can install a python app/package

(rose):~/python$ pip install requests

The command above is a command to install “requests”. This is the same with the one we previously installed. But, if you install it in a virtual environment, you only need to type pip install requests instead of bin/pip install requests

When you finished working on the virtual environment or wanted to switch to another one, you can deactivate the environment you are working on by invoking this command:

(rose):~/python$ deactivate

Of course, you don’t have to install and configure virtual environment on Ubuntu 16.04, if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to install and configure virtual environment on Ubuntu 16.04 for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post on how to install virtual environment on Ubuntu 16.04, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

One thought on “How to Install Virtual Environment on Ubuntu 16.04

  1. hi,
    Really a best guide to install a virtual envinoment,This just helped me in installing my virtual environment.
    thanks a lot dear..
    wish to see more good article like this..

Leave a Reply

Your email address will not be published. Required fields are marked *