Skip to content

Instantly share code, notes, and snippets.

@frfahim
Last active April 23, 2024 11:09
Show Gist options
  • Save frfahim/73c0fad6350332cef7a653bcd762f08d to your computer and use it in GitHub Desktop.
Save frfahim/73c0fad6350332cef7a653bcd762f08d to your computer and use it in GitHub Desktop.
How to install virtual environment on ubuntu 16.04

How to install virtualenv:

Install pip first

sudo apt-get install python3-pip

Then install virtualenv using pip3

sudo pip3 install virtualenv 

Now create a virtual environment

virtualenv venv 

you can use any name insted of venv

You can also use a Python interpreter of your choice

virtualenv -p /usr/bin/python2.7 venv

Active your virtual environment:

source venv/bin/activate

Using fish shell:

source venv/bin/activate.fish

To deactivate:

deactivate

Create virtualenv using Python3

virtualenv -p python3 myenv

Instead of using virtualenv you can use this command in Python3

python3 -m venv myenv

Install pip3

sudo apt-get install -y python3-pip

to install package

pip3 install package_name

setup few more packages and development tools

sudo apt-get install build-essential libssl-dev libffi-dev python-dev

install venv module

sudo apt-get install -y python3-venv

create a enviornments

python3 -m venv project_env

@oshliaer
Copy link

If executing pip with sudo, you may want sudo's -H flag.

sudo -H apt-get install python3-pip

@graywen24
Copy link

simple and good...

thanks

@arviesan24
Copy link

nice. easy to understand

@CarlosAndres12
Copy link

CarlosAndres12 commented Jul 22, 2018

you can use sudo apt-get install python3-venv instead of sudo pip3 install virtualenv .

@Anusha-G-Rao
Copy link

thank you!! helped me a lot with my problem :)

@stsynguev
Copy link

What is the difference between virtualenv and virtualenvwrapper?

@AndrewMo60
Copy link

What is the difference between virtualenv and virtualenvwrapper?

virtualenv is a tool that allows you to create isolated Python environments, which can be quite helpful when you have different projects with differing requirements. mkvirtualenv is command under virtualenvwrapper which is just a wrapper utility around virtualenv that makes it even easier to work with. virtualenvwrapper is a set of extensions to Ian Bicking's virtualenv tool. The extensions include wrappers for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies.

@bajpairohan6306
Copy link

thanks for help......

@tandavala
Copy link

thanks!!!

@parakh08
Copy link

Hey, is it possible to merge the virtual environment to the main?

@Salman778
Copy link

Thank you!!

@objarni
Copy link

objarni commented Apr 23, 2019

For Ubuntu 16.04, Python 2.7, I recommend sudo apt-get install virtualenv instead of installing package python-pip, which seems flawed. pip from python-pip package cannot even do pip freeze without crashing. This happens on a fresh Ubuntu 16.04 Desktop install so quite surprising.

@nexto123
Copy link

How can one install the virtualenv with the current version of python?

@VickiLanger
Copy link

VickiLanger commented May 21, 2019

How can one install the virtualenv with the current version of python?
@nexto123
I put in

virtualenv venv 

and it worked fine.

@AlienKevin
Copy link

@objarni Thank you. While sudo pip3 install virtualenv doesn't work, sudo apt-get install virtualenv works perfectly on Ubuntu 18.10

@vzhukovskyy
Copy link

My experience is opposite. I tried sudo apt-get install virtualenv but sometimes it could not find packages installed in the virtual environment. pip3 install virtualenv worked as expected. Ubuntu 18.04.

@JasonStapley
Copy link

Thank you

@rafidka
Copy link

rafidka commented Sep 1, 2019

According to virtualenv documentation:

In non-Windows systems it is discouraged to run pip as root including with sudo. Generally use your system package manager if it provides a package. This avoids conflicts in versions and file locations between the system package manager and pip. See your distribution’s package manager documentation for instructions on using it to install virtualenv.

@sibamoussa
Copy link

super easy to use, thanks!

@HelloMukama
Copy link

great

@Ramsyz
Copy link

Ramsyz commented Aug 8, 2020

Thanks!!! useful.

@saeidalidadi
Copy link

very useful!!!!!:)

@Ishaan-Adarsh
Copy link

Direct and simple. very useful.

@mfalgorepublic
Copy link

Thanks buddy! It helped.

@kifirifis
Copy link

Simple y fácil. Gracias.

@Samuel-Njoroge
Copy link

The packages have been obsoleted for Ubuntu 20.0.

@sowrovsarkar63
Copy link

It was very useful and straight forward. Thank you

@DianaLozanoP
Copy link

DianaLozanoP commented Jul 31, 2023

Hello there,
When I try to install the python3-venv package using the following command.

sudo apt install python3.10-venv

I get this error:
" Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
python3.10-venv : Depends: python3.10 (= 3.10.4-3) but 3.10.6-1~22.04.2ubuntu1 is to be installed
E: Unable to correct problems, you have held broken packages. "

I'm using Ubuntu 22.04.2 LTS, Python 3.10.6.

If someone can help me or guide, I would appreciate it. I'm a newbie here. Thanks!

@BakrFrag
Copy link

@DianaLozanoP you don't have to use 3.10 version unless you have multiplie versions of python 3 on your system
also make sure pip python package index is installed
also make sure your software dependencies on system are up to date use blow commands
sudo apt update
sudo apt upgrade -y
to install python3 pip on ubuntu sudo apt install python3-pip
venv module now in python default packages so you don't have to install

@DianaLozanoP
Copy link

DianaLozanoP commented Jul 31, 2023

Hey @BakrFrag,
I followed your suggestions, and they worked wonders.
Now I can use a virtual environment.
Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment