TensorFlow projects often need a Python package set that can be upgraded, rebuilt, or removed without changing other machine-learning work on the same computer. A dedicated venv environment keeps the project interpreter entry points and pip packages together while still using Python's standard library tooling.
The venv module builds the new environment from the exact Python executable used to start it. Choose a TensorFlow-compatible executable before creation, because the environment will keep using that interpreter line after activation.
The shell is ready for TensorFlow installation when python and pip resolve inside ~/venvs/tf. Checking pip through python -m pip also avoids accidentally calling a user-level or system-level pip command with the same name.
Steps to create a virtual environment for TensorFlow:
- Check a Python executable that TensorFlow can use.
$ python3.12 --version Python 3.12.13
TensorFlow 2.21 wheels are published for CPython 3.10 through 3.13. Substitute another supported executable, such as python3.13, when that is the version installed on the workstation.
- Create a directory for reusable virtual environments.
$ mkdir -p ~/venvs
- Create the TensorFlow virtual environment from the chosen executable.
$ python3.12 -m venv ~/venvs/tf
The venv module creates ~/venvs/tf/bin/python and a private site-packages directory without modifying the system Python installation.
- Activate the new environment in the current shell.
$ source ~/venvs/tf/bin/activate (tf) $
The prompt usually shows the environment name after activation. Open a new shell or run the activation command again before later TensorFlow work.
- Confirm that pip resolves inside the virtual environment.
(tf) $ python -m pip --version pip 25.0.1 from /home/user/venvs/tf/lib/python3.12/site-packages/pip (python 3.12)
The important path segment is /venvs/tf/lib/python3.12/site-packages. A system path or user-site path means the shell is not using the intended environment.
- Upgrade pip inside the active environment.
(tf) $ python -m pip install --upgrade pip Requirement already satisfied: pip in /home/user/venvs/tf/lib/python3.12/site-packages (25.0.1) Collecting pip ##### snipped ##### Successfully installed pip-26.1.2
TensorFlow's install documentation recommends a recent pip before installing the package. This upgrade changes only ~/venvs/tf.
- Recheck pip after the upgrade.
(tf) $ python -m pip --version pip 26.1.2 from /home/user/venvs/tf/lib/python3.12/site-packages/pip (python 3.12)
Install TensorFlow from this active shell so the package lands in the environment that produced this path.
Related: How to check the TensorFlow version - Deactivate the environment when setup checks are finished.
(tf) $ deactivate $
Skip deactivation when the next command will install TensorFlow into the same environment.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.