pip and Python virtual environments in Ubuntu

You can install python packages with pip and make virtual environments to install different versions of them in different directories. python2 is default on Ubuntu 16.04, python3 is default on 18.04. We recommend you specify the version of python or pip (python3 program.py instead of python program.py). Open Terminal on or SSH to an Ubuntu host to start.

Make a directory for your program if you haven’t already and change to it:

mkdir $program_dir
cd $program_dir

Make a virtual environment:

virtualenv -p python2 venv
# or
virtualenv -p python3 venv

Enter virtual environment (Always do this before running the program, and do this in its directory):

source venv/bin/activate


Search pip for package:

pip search $package

Install package with pip:

pip install $package[==$ver]

Example installs:

pip install beautifulsoup4
# or
pip install beautifulsoup4==4.6.3

List installed packages and their version:

pip list

Show info about installed package:

pip show $package

Uninstall package with pip:

pip uninstall $package

If the program has requirements.txt, you can install them in one command:

pip install -r requirements.txt


Now you can run your program in the virtual environment:


Exit virtual environment (Always do this after running the program):


For more info:

man pip
man virtualenv