bash: mkvirtualenv: command not found

235,558

Solution 1

Solution 1:

For some reason, virtualenvwrapper.sh installed in /usr/bin/virtualenvwrapper.sh, instead of under /usr/local/bin.

The following in my .bash_profile works...

source "/usr/bin/virtualenvwrapper.sh"
export WORKON_HOME="/opt/virtual_env/"

My install seems to work fine without sourcing virtualenvwrapper_bashrc

Solution 2:

Alternatively as mentioned below, you could leverage the chance that virtualenvwrapper.sh is already in your shell's PATH and just issue a source `which virtualenvwrapper.sh`

Solution 2

Try:

source `which virtualenvwrapper.sh`

The backticks are command substitution - they take whatever the program prints out and put it in the expression. In this case "which" checks the $PATH to find virtualenvwrapper.sh and outputs the path to it. The script is then read by the shell via 'source'.

If you want this to happen every time you restart your shell, it's probably better to grab the output from the "which" command first, and then put the "source" line in your shell, something like this:

echo "source /path/to/virtualenvwrapper.sh" >> ~/.profile

^ This may differ slightly based on your shell. Also, be careful not to use the a single > as this will truncate your ~/.profile :-o

Solution 3

I had the same issue on OS X 10.9.1 with python 2.7.5. No issues with WORKON_HOME for me, but I did have to manually add source "/usr/local/bin/virtualenvwrapper.sh" to ~/.bash_profile (or ~/.bashrc in unix) after I ran pip install virtualenvwrapper

Solution 4

Prerequisites to execute this command -

  1. pip (recursive acronym of Pip Installs Packages) is a package management system used to install and manage software packages written in Python. Many packages can be found in the Python Package Index (PyPI).

    sudo apt-get install python-pip

  2. Install Virtual Environment. Used to create virtual environment, to install packages and dependencies of multiple projects isolated from each other.

    sudo pip install virtualenv

  3. Install virtual environment wrapper About virtual env wrapper

    sudo pip install virtualenvwrapper

After Installing prerequisites you need to bring virtual environment wrapper into action to create virtual environment. Following are the steps -

  1. set virtual environment directory in path variable- export WORKON_HOME=(directory you need to save envs)

  2. source /usr/local/bin/virtualenvwrapper.sh -p $WORKON_HOME

As mentioned by @Mike, source `which virtualenvwrapper.sh` or which virtualenvwrapper.sh can used to locate virtualenvwrapper.sh file.

It's best to put above two lines in ~/.bashrc to avoid executing the above commands every time you open new shell. That's all you need to create environment using mkvirtualenv

Points to keep in mind -

  • Under Ubuntu, you may need install virtualenv and virtualenvwrapper as root. Simply prefix the command above with sudo.
  • Depending on the process used to install virtualenv, the path to virtualenvwrapper.sh may vary. Find the appropriate path by running $ find /usr -name virtualenvwrapper.sh. Adjust the line in your .bash_profile or .bashrc script accordingly.

Solution 5

Use this procedure to create virtual env in ubuntu

Step 1

Install pip

   sudo apt-get install python-pip

step 2

Install virtualenv

   sudo pip install virtualenv

step 3

Create a dir to store your virtualenvs (I use ~/.virtualenvs)

   mkdir ~/.virtualenvs

or use this command to install specific version of python in env

virtualenv -p /usr/bin/python3.6 venv

step 4

   sudo pip install virtualenvwrapper

step 5

   sudo nano ~/.bashrc

step 6

Add this two line code at the end of the bashrc file

  export WORKON_HOME=~/.virtualenvs
  source /usr/local/bin/virtualenvwrapper.sh

step 7

Open new terminal (recommended)

step 8

Create a new virtualenv

  mkvirtualenv myawesomeproject

step 9

To load or switch between virtualenvs, use the workon command:

  workon myawesomeproject

step 10

To exit your new virtualenv, use

 deactivate

and make sure using pip vs pip3

OR follow the steps below to install virtual environment using python3

Install env

python3 -m venv my-project-env

and activate your virtual environment using the following command:

source my-project-env/bin/activate

or if you want particular python version

virtualenv --python=python3.7.5 myenv
Share:
235,558

Related videos on Youtube

Mike Pennington
Author by

Mike Pennington

-->

Updated on April 01, 2022

Comments

  • Mike Pennington
    Mike Pennington about 2 years

    After following the instructions on Doug Hellman's virtualenvwrapper post, I still could not fire up a test environment.

    [mpenning@tsunami ~]$ mkvirtualenv test
    -bash: mkvirtualenv: command not found
    [mpenning@tsunami ~]$
    

    It should be noted that I'm using WORKON_HOME that is not in my $HOME. I tried looking for /usr/local/bin/virtualenvwrapper.sh as shown in the virtualenvwrapper installation docs, but it does not exist.

    I'm running CentOS 6 and python 2.6.6, if this matters.


    # File: ~/.bash_profile
    # ...
    
    export WORKON_HOME="/opt/virtual_env/"
    source "/opt/virtual_env/bin/virtualenvwrapper_bashrc"
    
  • Gregology
    Gregology about 10 years
    Where did you manually add source "/usr/local/bin/virtualenvwrapper.sh"?
  • Nick Benes
    Nick Benes about 10 years
    @Gregology I added that line to ~/.bash_profile. Note that when you first add it you'll either have to reload the terminal (which runs .bash_profile) or just run that source command directly from the command line.
  • Pierre de LESPINAY
    Pierre de LESPINAY over 9 years
    Setting WORKON_HOME to "~/.virtualenvs" (default value) allows to set private virtualenvs
  • JGallardo
    JGallardo over 8 years
    It would be very helpful if you had a title with the filename and displayed all the contents so you don't have to get followup questions about "where did you put it?"
  • Toby Speight
    Toby Speight almost 8 years
    While this might be a valuable hint to solve the problem, an answer really needs a bit more detail than this. Please edit to explain how this will solve the problem. Alternatively, consider writing this as a comment instead.
  • tripleee
    tripleee almost 8 years
    Whether you use .bash_profile or .bashrc is not really directly a consequence of which platform you are usng, though one or the other may be missing on some platforms. See the Bash manual page for their differences. Generally, you should only really need this in your .bash_profile, but some distros use setups which complicate matters.
  • tripleee
    tripleee almost 8 years
    If you install using your distro's package manager, files will be in /usr/bin instead of /usr/local/bin. Generally, you should not assume one or the other to be found; that's one of the reasons we have a PATH variable.
  • Mike Pennington
    Mike Pennington almost 8 years
    This answer was based on installing virtualenvwrapper with pip, which is what I prefer doing.
  • h0r53
    h0r53 about 7 years
    This solved my problem. But will someone explain why and how?
  • Erich
    Erich about 7 years
    The backticks are command substitution - they take whatever the program prints out and put it in the expression. In this case "which" checks the $PATH to find virtualenvwrapper.sh and outputs the path to it. The script is then read by the shell via 'source'.
  • Eddie
    Eddie about 6 years
    It needs to be installed first, pip install virtualenvwrapper
  • Dan Grahn
    Dan Grahn over 5 years
    Adding a note. On ubuntu 18.04, I had to reboot after install and then it worked.
  • Keshav
    Keshav over 5 years
    @screenmutt Thanks for valuable input. I might have missed it as I use ubuntu 16.04. However, I would like to know after which step did you have to reboot ? Is is after installing pip packages or after setting up "virtual environment directory" ?
  • Dan Grahn
    Dan Grahn over 5 years
    After the install of the wrapper. It worked after that.
  • TJ Ellis
    TJ Ellis over 5 years
    @DanGrahn you don't need to reboot, just need to rerun .bashrc - either explicitly source ~/.bashrc in your current terminal, or open a new terminal window
  • johnny 5
    johnny 5 over 4 years
    I actually just copied and pased your solution
  • scones
    scones over 3 years
    WORKON_HOME is optional and has nothing to do with the solution.
  • scones
    scones over 3 years
    works on ubuntu 20.04 as well. a small correction tho: WORKON_HOME=~/.virtualenvs is the default. no need to set that. VIRTUALENVWRAPPER_PYTHON=/usr/bin/python is also the default, but when using python3 this is useful. also one should use export PATH="$HOME/.local/bin:$PATH" for the python binary paths
  • Manur
    Manur over 3 years
    Using Windows 10, Git Bash and Python 3.8, for me it was on: /c/Users/[myUserName]/AppData/Roaming/Python/Python38/Script‌​s
  • Schwarz Kugelblitz
    Schwarz Kugelblitz over 3 years
    After installing virtualwrapper it says no such file or directory found after the first line of your code
  • noobninja
    noobninja over 3 years
    I found virtualenvwrapper/virtualenvwrapper.sh in /usr/share Ubuntu 20.04
  • y_159
    y_159 almost 3 years
    @Manur thanks, it worked. On running source virtualenwrapper.sh , i wasn't able to see this path, when i did source /c/Users/[myUserName]/AppData/Roaming/Python/Python38/Script‌​s, mkvirtualenv <name> worked.