DEV Community

Serhat Teker
Serhat Teker

Posted on • Originally published at tech.serhatteker.com on

How to install virtualenv on Ubuntu 18.04

We may face issues when our Linux distribution only offers certain versions of Python and its packages, when we actually need newer or older versions.

We can install new versions of Python on the server, however this will be more complex because we will have some dependency issues when trying to compile everything we need.

Virtual environments make this very easy to manage and set up, we can have different versions of Python and packages in each environment, and it will be isolated from the main system.

Installing Virtual Environment on Ubuntu 18.04 -or later from 16.04, is fairly easy task and it shouldn’t take more then 10 minutes to finish.

install pip

install pip3 if you don't have already

$   sudo apt-get install python3-pip
Enter fullscreen mode Exit fullscreen mode

instal virtualenv

$   sudo pip3 install virtualenv
Enter fullscreen mode Exit fullscreen mode

build new virtualenv

$   cd $YOUR_PROJECT_DIRECTORY
$   virtualenv .venv
Enter fullscreen mode Exit fullscreen mode

.venv or any $NAME for your virtualenv

to activate your virtualev

$   source .venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

and you see your .venv activated

(.venv) ~/project$
Enter fullscreen mode Exit fullscreen mode

to deactivate your virtualev

$   deactivate
Enter fullscreen mode Exit fullscreen mode

install new packages

After activating your virtualenv

$  pip install <some-package>
Enter fullscreen mode Exit fullscreen mode

P.S:

You can also use Python2.7 interpreter for your virtualenv but I would not recommend that since Python 2.7 will not be maintained after January 1, 2020. But if you like to:

$   virtualenv -p /usr/bin/python2.7 .venv
Enter fullscreen mode Exit fullscreen mode

Top comments (0)