Table of Contents
In this article, I will take you through the steps to install virtualenv on Ubuntu 20.04 LTS (Focal Fossa). virtualenv is a free and open source tool for creating isolated Python virtual environments. It is widely used by Python programmers and developers to test their different application releases with ease. Each virtual environments has its own Python binary (which matches the version of the binary that was used to create this environment) and can have its own independent set of installed Python packages in its site directories. Common installation tools such as setuptools and pip work as expected with virtual environments. More on official website.
How to Install virtualenv on Ubuntu 20.04 LTS (Focal Fossa)
Step 1: Prerequisites
a) You should have a running
Ubuntu 20.04 LTS Server.
b) You should have
root access to run privileged commands.
c) You should have
apt-get utility available in your System.
Step 2: Update Your Server
First you need to update all the packages in your System by using
sudo apt update command as shown below. This will sync your System packages with the latest available versions from Ubuntu repo.
cyberithub@ubuntu:~$ sudo apt update Hit:1 https://download.docker.com/linux/ubuntu focal InRelease Hit:2 https://dl.google.com/linux/chrome/deb stable InRelease Hit:3 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu focal InRelease Get:4 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB] Hit:5 http://in.archive.ubuntu.com/ubuntu focal InRelease Get:6 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB] Hit:7 https://apt.boltops.com stable InRelease Get:8 http://security.ubuntu.com/ubuntu focal-security/main amd64 DEP-11 Metadata [40.7 kB] Get:9 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB] Get:10 http://security.ubuntu.com/ubuntu focal-security/universe amd64 DEP-11 Metadata [77.3 kB] Get:11 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [2,113 kB] Get:12 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 DEP-11 Metadata [2,464B] ..........................................................
If any of the system packages needs to be upgraded then use
sudo apt upgrade command as shown below. Since in our case all the packages are already upgraded to the latest versions, it is not showing any packages that needs to be upgraded.
cyberithub@ubuntu:~$ sudo apt upgrade Reading package lists... Done Building dependency tree Reading state information... Done Calculating upgrade... Done The following package was automatically installed and is no longer required: pypy-lib Use 'sudo apt autoremove' to remove it. 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Step 3: Install Python3
In the next step, you need to install python3 package using
sudo apt install python3 command as shown below. This will download and install the package along with all its dependencies.
cyberithub@ubuntu:~$ sudo apt install python3 Reading package lists... Done Building dependency tree Reading state information... Done python3 is already the newest version (3.8.2-0ubuntu2). The following packages were automatically installed and are no longer required: pypy-lib python-pip-whl python3-wheel Use 'sudo apt autoremove' to remove them. 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Step 4: Install pip3
As you need pip3 to install virtualenv so you need to first install the latest version of this python package manager by using
sudo apt install python3-pip command as shown below.
cyberithub@ubuntu:~$ sudo apt install python3-pip Reading package lists... Done Building dependency tree Reading state information... Done The following package was automatically installed and is no longer required: pypy-lib Use 'sudo apt autoremove' to remove it. The following NEW packages will be installed: python3-pip 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 0 B/231 kB of archives. After this operation, 1,050 kB of additional disk space will be used. Selecting previously unselected package python3-pip. (Reading database ... 258939 files and directories currently installed.) Preparing to unpack .../python3-pip_20.0.2-5ubuntu1.6_all.deb ... Unpacking python3-pip (20.0.2-5ubuntu1.6) ... Setting up python3-pip (20.0.2-5ubuntu1.6) ... Processing triggers for man-db (2.9.1-1) ...
Step 5: Install virtualenv
Next step is to install virtualenv by using
pip3 install virtualenv command as shown below. If you are using older pip version then you need to use
pip install virtualenv command.
cyberithub@ubuntu:~$ pip3 install virtualenv Collecting virtualenv Downloading virtualenv-20.16.5-py3-none-any.whl (8.8 MB) |████████████████████████████████| 8.8 MB 1.7 MB/s Collecting platformdirs<3,>=2.4 Downloading platformdirs-2.5.2-py3-none-any.whl (14 kB) Collecting filelock<4,>=3.4.1 Downloading filelock-3.8.0-py3-none-any.whl (10 kB) Collecting distlib<1,>=0.3.5 Downloading distlib-0.3.6-py2.py3-none-any.whl (468 kB) |████████████████████████████████| 468 kB 5.7 MB/s Installing collected packages: platformdirs, filelock, distlib, virtualenv Successfully installed distlib-0.3.6 filelock-3.8.0 platformdirs-2.5.2 virtualenv-20.16.5 ..........................................................
Step 6: Check Version
To verify the virtualenv installation, you need to use
virtualenv --version command as shown below.
cyberithub@ubuntu:~$ virtualenv --version virtualenv 20.16.5 from /home/cyberithub/.local/lib/python3.8/site-packages/virtualenv/__init__.py
Step 7: Create a Virtual environment
After successful installation, you can create a virtual environment by using
virtualenv venv command as shown below.
cyberithub@ubuntu:~$ virtualenv venv created virtual environment CPython3.8.10.final.0-64 in 1411ms creator CPython3Posix(dest=/home/cyberithub/venv, clear=False, no_vcs_ignore=False, global=False) seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/cyberithub/.local/share/virtualenv) added seed packages: pip==22.2.2, setuptools==65.3.0, wheel==0.37.1 activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
Step 8: Activate or Deactivate environment
Once a virtual environment is created, it can be activated and deactivated using a script in the virtual environment’s binary directory. To activate a virtual environment venv, you need to use
source venv/bin/activate command as shown below.
cyberithub@ubuntu:~$ source venv/bin/activate (venv) cyberithub@ubuntu:~$
To deactivate the current virtual environment, you need to use
deactivate command as shown below.
(venv) cyberithub@ubuntu:~$ deactivate cyberithub@ubuntu:~$