This document describes the steps to install and configure a Python environment using pyenv and venv on Ubuntu. The Python version to be used is 3.12.0.
Before starting, ensure you have the following tools and dependencies installed:
For Ubuntu-based systems:
sudo apt update
sudo apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev liblzma-dev python3-openssl git
We install pyenv
to manage multiple Python versions without interfering with the system versions.
Clone the pyenv
repository into your home directory:
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
Edit your shell configuration file (~/.bashrc
) and add the following lines:
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
Reload your shell to apply the changes:
source ~/.bashrc
Check that pyenv is installed correctly:
pyenv --version
Use pyenv
to install Python 3.12.0:
pyenv install 3.12.0
This command will download and compile Python 3.12.0 on your system.
Verify the installation:
pyenv versions
Navigate to the project directory and set the Python version to be used locally:
pyenv local 3.12.0
This will create a .python-version
file in the project folder, indicating that pyenv should use Python 3.12.0 in this directory.
Verify that you are using the correct version:
python --version
Use the venv
module to create a virtual environment within the project. You can specify the name of the folder where the virtual environment will be created:
python -m venv ZDZW_BE
This will create a folder named ZDZW_BE
in your project directory, containing the virtual environment.
Activate the virtual environment:
source ZDZW_BE/bin/activate
Verify that the virtual environment is activated:
python --version
It should display Python 3.12.0
.
After activating the virtual environment, you can install the required dependencies for development or production as needed:
pip install -r requirements-dev.txt
pip install -r requirements-prod.txt
When you are done working, deactivate the virtual environment with:
deactivate
-
Install pyenv:
git clone https://github.com/pyenv/pyenv.git ~/.pyenv echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc echo 'eval "$(pyenv init --path)"' >> ~/.bashrc echo 'eval "$(pyenv init -)"' >> ~/.bashrc source ~/.bashrc
-
Install Python 3.12.0:
pyenv install 3.12.0 pyenv local 3.12.0
-
Create and activate a virtual environment:
python -m venv <env_name> source <env_name>/bin/activate
-
Install dependencies:
- Development:
pip install -r requirements-dev.txt
- Production:
pip install -r requirements-prod.txt
- Development:
-
Deactivate the virtual environment:
deactivate