1

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.

ghrasko
  • 73
  • 8

2 Answers2

3

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
ghrasko
  • 73
  • 8
1

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
Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50
  • Thanks, I also figured out and posted. In fact this complete installation process could be hardly found anywhere on the internet. (I voted up your answer, but it might not be considered as a complete vote-up, as I am a newbe here) – ghrasko Dec 03 '19 at 08:25
  • Please upvote my answer if you think it was useful :) – Riccardo Bucco Dec 03 '19 at 08:27
  • I did, but as a novice user here it did not count as a full vote up. – ghrasko Dec 03 '19 at 09:36