How to create a Python 3.8 virtual environment in Ubuntu 16.04

11,430

Solution 1

I noticed that the deadsnakes ppa has instructions that include this:

  • python#.#-venv: provides the standard library venv module

So, I believe you need to make sure to apt install python3.8-venv. Then the following will work:

python3.8 -m venv venv_dir

If you really want to use virtualenv and not just the native venv, then you could install it, but you would first need pip. So the process would look something like this:

python3.8 -m ensurepip
python3.8 -m pip install virtualenv
python3.8 -m virtualenv venv_dir

I hope this helps! In case you want to read (and review/critique, as I would welcome it) I have written a summary of several Python virtual environment tools you may find helpful. Feel free to tell me how I can make it better.

Solution 2

Try using the built-in venv module instead of virtualenv:

/usr/bin/python3.8 -m venv virtualenv_directory/

venv has been included with Python since version 3.3.

Share:
11,430
fhcat
Author by

fhcat

Updated on June 13, 2022

Comments

  • fhcat
    fhcat almost 2 years

    In Ubuntu 16.04 the latest release of Python3 is 3.5. If I just do virutalenv venv it would create a new virtual environment using Python 3.5.

    I followed the instructions in https://linuxize.com/post/how-to-install-python-3-8-on-ubuntu-18-04/ and installed Python 3.8 with apt from the deadsnakes PPA. But I am still not able to create a Python 3.8 virtual environment. If I do virtualenv --python=/usr/bin/python3.8, I got this:

    user@host:~$ virtualenv --python=/usr/bin/python3.8 venv
    RuntimeError: failed to query /usr/bin/python3.8 with code 1 err: 'Traceback (most recent call last):\n  File "/usr/local/lib/python3.5/dist-packages/virtualenv/discovery/py_info.py", line 16, in <module>\n    from distutils import dist\nImportError: cannot import name \'dist\' from \'distutils\' (/usr/lib/python3.8/distutils/__init__.py)\n'
    
  • fhcat
    fhcat almost 4 years
    I got an error running it. ` user@host:~$ /usr/bin/python3.8 -m venv virtualenv_directory Error: Command '['/home/user/virtualenv_directory/bin/python3.8', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1. `
  • Chris
    Chris almost 4 years
    @fhcat, do you have internet access on this machine?