skip to Main Content

I recently set up Ubuntu 18.04.1 desktop (with Oracle VM) which is coming with with Python 3.6.8. After standard system upgrades it becames 3.6.9. Later I installed Python 3.8, pip and venv as follows:

$ sudo apt install python3.8
$ sudo apt install python3-pip
$ sudo apt install python3-venv

If I understand correctly, pip and venv are commn for both versions and in fact I have realized that pip3 installed 3.6 version files. I did not force apt to somehow install 3.8 version pip3.

Now I can create virtual environments with Python 3.6, but still not with 3.8. There is no option to tell from which Python copy (version) the virtual env should be created. In the old virtualenv and virtualenvwrapper solution there was a command line option to define the version:

mkvirtualenv -p python3.8 myvirtualenv38

or

mkvirtualenv -p python3.6 myvirtualenv36

I could not find a similar option with venv. Some says that we should run venv with the appropriate Python version as

python3.8 -m venv myvirtualenv38

but this will fail with an error message:

user@Server-Ubuntu:~/envs$ python3.8 -m venv env38a
The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

apt-get install python3-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

Failing command: ['/home/user/envs/env38a/bin/python3.8', '-Im', 'ensurepip', '--upgrade', '--default-pip']

The directory structure is created for the new virtual environment, but for example the activate file does not exist.

Additional information: I also played a bit with update-alternatives –config python3, but I stopped as Ubuntu 18 seems to be relying on Python3.6 and changing the default to 3.8 caused immediate problems for example when running the terminal. I had not yet tried to temporarily changing the versions during the above process.

2

Answers


  1. Chosen as BEST ANSWER

    I figured out the answer for my question finally...

    One has to install venv for each Python versions for which he would like to use virtual environments. So the correct installation path is as follows:

    $ sudo apt install python3.8
    $ sudo apt install python3-pip
    $ sudo apt install python3.6-venv  # needed only if you would like to use 3.6 with venv
    $ sudo apt install python3.8-venv
    

    After this creation of virtualenvs for the different version goes as follows:

    mkvirtualenv -p python3.8 myvirtualenv38
    

    and

    mkvirtualenv -p python3.6 myvirtualenv36
    # mkvirtualenv -p python3 myvirtualenv36  # this also installs v3.6 virtual environment
    

  2. You need to install the proper version of venv:

    sudo apt-get install python3.8-venv
    

    Once you have installed it, simply run:

    python3.8 -m venv your_virtual_env
    

    Activate the environment and make sure it’s running on Python 3.8:

    source your_virtual_env/bin/activate
    python -V
    >> Python 3.8.x
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search