Question

Python virtual environments

I successfully installed a virtual environment in Ubuntu 22.10 (VirtualBox) by following this excellent tutorial: www.digitalocean.com/community/tutorials/how-to-install-python-3-and-set-up-a-programming-environment-on-ubuntu-22-04. Thank you, Alex. I have two follow up questions:

  1. A clarification, please. The tutorial directs us to cd into the directory created for our virtual environments (here “environments”) and to both create and activate the environment created from this “environments” directory. In other words, we should do our work from within the main “environments” directory, and not from the my_env directory that is created by the python3 -m venv my_env command. Is this correct? I am asking, because I am planning to create a second virtual environment, and to use a different/older version of Ptyhon. Somehow I feel I should be working from within the respective directory created by the venv command, especially since I will have more than one environment.
  2. I am running the Python version 3.10.7, which is the latest update available from the Ubuntu repo. But would need to install a version of Python 3.9 to use in my second virtual environment. Could someone please direct me to a tutorial on how to do that?

Thanks. I have always found the DigitalOcean tutorials comprehensive and well-written.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
March 2, 2023
Accepted Answer

Hi there,

You will be working form the directory that you’ve created with the mkdir command.

For example let’s say that you have 2 projects:

  • Project 1: you will create a new directory called project1 then cd into it and create the venv, python3 -m venv p1_env
  • Project 2: you will create a new directory called project2 then cd into it and create the venv, python3 -m venv p2_env

For your second question, in order to install Python 3.9 on Ubuntu, you can use the pyenv CLI to install specific Python versions:

https://github.com/pyenv/pyenv#getting-pyenv

  1. Install the required dependencies:
sudo apt-get update sudo apt-get install git curl python3-pip gcc make zlib1g-dev libssl-dev libbz2-dev libreadline-dev libsqlite3-dev
  1. Install pyenv using the official installer:
curl https://pyenv.run | bash

This will download and run the pyenv installer script. It will add the necessary configuration to your shell profile file (e.g. ~/.bashrc) to initialize pyenv on each new terminal session.

  1. Restart your shell or reload the profile:
source ~/.bashrc
  1. Verify that pyenv is installed:
pyenv --version

This should output the version of pyenv that you installed.

  1. Install Python versions using pyenv:
pyenv install 3.10.0

This will download and install Python 3.10.0. You can install other Python versions in the same way.

  1. Set the global Python version:
pyenv global 3.10.0

This will set the global Python version to 3.10.0. You can also set the Python version for a specific directory using the pyenv local command.

Let me know if you have any questions!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more