DEV Community

ruchikaatwal
ruchikaatwal

Posted on • Updated on

Beginner's - creating virtual environment on ubuntu for python

Installing virtual environment

  • Update your system
$ sudo apt-get update && sudo apt-get upgrade
Enter fullscreen mode Exit fullscreen mode
  • Install Virtual Environment package with below command :
$ sudo apt install virtualenv -y
Enter fullscreen mode Exit fullscreen mode
  • Create a python virtual environment directory for different.
$ mkdir ~/python-environments && cd ~/python-environments
Enter fullscreen mode Exit fullscreen mode

Explanation :

mkdir -> is used a create folder/directory in ubuntu from
terminal
cd -> is used to change path for folder/directory path

&& -> is used to join and run 2 commands in terminal
together at once

You can run make directory and change directory command in separate line too :

$ mkdir ~/python-environments
$ cd ~/python-environments
Enter fullscreen mode Exit fullscreen mode
  • Now create python virtual environment :
$ virtualenv --python=python3.8 env_python38
Enter fullscreen mode Exit fullscreen mode

After running create virtual enviornment commant output in terminal would be like this :

created virtual environment CPython3.8.10.final.0-64 in 80ms
  creator CPython3Posix(dest=/home/ruchika/python-environments/env_python38, clear=False, global=False)
  seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, pkg_resources=latest, via=copy, app_data_dir=/home/ruchika/.local/share/virtualenv/seed-app-data/v1.0.1.debian.1)
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
Enter fullscreen mode Exit fullscreen mode
  • type ls command to check list of file, folder in current directory :
ls
Enter fullscreen mode Exit fullscreen mode

you can your created virtual environment directory :

env_python38
Enter fullscreen mode Exit fullscreen mode
  • Check your environment is installed with the python version is proper :
$ ls env_python38/lib
Enter fullscreen mode Exit fullscreen mode

Activate and deactivate virtual environment

  • Activate your virtual environment
$ source env_python38/bin/activate
Enter fullscreen mode Exit fullscreen mode

After activating you see your virtual env name in rounf bracket :

(env_python38) yoursystemname:~/python-environments$
Enter fullscreen mode Exit fullscreen mode
  • Deactiavte (close) your virtual environment:
$ deactivate
Enter fullscreen mode Exit fullscreen mode

Top comments (0)