Skip to main content
Welcome to this comprehensive guide on configuring a PyTorch development environment. In this tutorial, you’ll learn how to create an isolated Python virtual environment on an Ubuntu machine, install all required dependencies (including PyTorch, TorchVision, and TorchAudio), and verify the installation. This step-by-step approach ensures that your environment is reproducible for collaboration or deployment. Before you start, visit the PyTorch “Get Started” page to choose your operating system, package manager, and CUDA version. The page dynamically generates the installation commands. For instance, for a nightly CPU build you may receive:
Alternatively, if you prefer Conda with CUDA 11.8 support, you might run:
For a typical installation using CUDA 11.8, the command is:
In the following sections, we detail how to set up your PyTorch environment on an Ubuntu system.

Step 1: Update Your System and Verify Python Installation

Begin by updating your package list to ensure that you have the latest versions of available packages:
It appears that Python is not installed yet. Install Python 3 (version 3.11 in this guide) and verify the installation with:

Step 2: Install pip and the venv Package

Pip is essential for managing Python packages. Install pip along with build dependencies:
During the installation, you will encounter output similar to this:
Verify pip’s installation:

Step 3: Create and Activate a Virtual Environment

Isolating your project in a virtual environment prevents conflicts between package versions. Create a new virtual environment named “venv”:
Check that the venv directory has been created:
Inside the venv folder, you will find several subdirectories and files:
Activate the virtual environment with:
Your prompt should now indicate that you are working within the virtual environment. You can safely install packages using pip without affecting the global Python installation.

Step 4: Install PyTorch, TorchVision, and TorchAudio

With the virtual environment activated, install PyTorch and its related libraries via pip. This command also pulls in necessary NVIDIA libraries if a GPU is detected:
The installation will display output similar to:
This confirms that PyTorch along with TorchVision and TorchAudio (plus their dependencies) have been installed in your isolated environment.

Step 5: Verify the Installation

To check that all packages installed correctly, list the installed packages using:
For reproducibility, you can generate a requirements file:
Review the generated file:
The file should contain entries like:
Next, validate that PyTorch operates as expected by opening a Python interpreter:
Inside the interactive shell, run:
Optionally, check for CUDA-enabled GPU availability:
This returns True if a CUDA device is available, else it returns False. Exit the interpreter by pressing Ctrl+D.

Step 6: Deactivate the Virtual Environment

Once you’ve completed testing, deactivate the virtual environment to return to the global Python state:
Running pip3 list in the global environment will now display only basic packages (e.g., pip, setuptools, wheel) without the additional PyTorch and NVIDIA libraries. To double-check, reactivate your virtual environment and list its installed packages:

Step 7: Reproducing Your Virtual Environment

Reproducibility is key when collaborating or migrating between machines. First, create a new virtual environment (named “venv2”):
Verify both virtual environments exist:
Activate the new environment:
Your new environment is minimal. Install all dependencies using the previously generated requirements.txt:
After installation, validate the PyTorch version by starting Python:
Then execute:
Exit the interpreter and deactivate the environment:
Generating a requirements.txt file helps ensure your project’s environment can be perfectly replicated on another machine, thereby improving collaboration efficiency.

Conclusion

In this guide, you learned how to:
  • Update an Ubuntu system and verify Python installation.
  • Install pip and create a Python virtual environment.
  • Install PyTorch along with TorchVision and TorchAudio in an isolated environment.
  • Verify the installation and generate a reproducible requirements.txt file.
  • Reproduce the virtual environment on another instance.
By following these steps, you ensure that your development environment is consistent and easily shareable. Happy coding with PyTorch!

Additional Resources

Watch Video

Practice Lab