{"id":47561,"date":"2026-11-23T22:06:04","date_gmt":"2026-11-23T22:06:04","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=47561"},"modified":"2026-11-23T22:06:04","modified_gmt":"2026-11-23T22:06:04","slug":"pytorch-lab-1-install-and-setup-pytorch","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/pytorch-lab-1-install-and-setup-pytorch\/","title":{"rendered":"PyTorch Lab -1 &#8211; Install And Setup PyTorch"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This lab installs PyTorch and torchvision so they are ready for the rest of the PyTorch lab series. The notebook above runs <code>pip install torch torchvision<\/code> from Jupyter. This page adds a current setup in a virtual environment, the CPU and GPU install options, a script that checks the install worked, and fixes for the most common errors.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/5cf50d8ebebf0d1311928bfb4ac216d0.js\"><\/script>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What the notebook above does and what it leaves out<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The notebook has one code cell, <code>!pip install torch torchvision<\/code>. In the saved output, torch 1.0.1 was already installed under Anaconda with Python 3.7, so pip left it alone and only added torchvision 0.2.2. Those versions are many years old. Current PyTorch releases do not support Python 3.7, so running the same command today in a new environment gives you a much newer version.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The notebook also stops right after the install. It never checks that <code>import torch<\/code> works or that a GPU can be used. The sections below fill in those steps.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A 64-bit Python 3 release supported by current PyTorch. The supported range moves forward over time, so check the install selector on pytorch.org. 32-bit Python is not supported.<\/li>\n\n\n<li><code>pip<\/code> (comes with Python) and the <code>venv<\/code> module. On Debian or Ubuntu you may need <code>sudo apt install python3-venv<\/code>.<\/li>\n\n\n<li>Several GB of free disk space. The Linux CUDA wheels are large because they include the CUDA runtime libraries.<\/li>\n\n\n<li>For NVIDIA GPU acceleration: a recent NVIDIA driver. <code>nvidia-smi<\/code> should run without errors. You do <strong>not<\/strong> need to install the full CUDA Toolkit, because the PyTorch wheels ship their own CUDA runtime.<\/li>\n\n\n<li>For Apple Silicon Macs: the standard macOS wheel already supports the <code>mps<\/code> (Metal) backend.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Install PyTorch in a virtual environment<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A dedicated virtual environment keeps PyTorch and its dependencies away from your system Python and from other projects. On Linux or macOS:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">python3 -m venv ~\/venvs\/pytorch-lab\nsource ~\/venvs\/pytorch-lab\/bin\/activate\npython -m pip install --upgrade pip\n\n<span class=\"hljs-comment\"># Default build from PyPI<\/span>\npip install torch torchvision\n\n<span class=\"hljs-comment\"># Jupyter support, registered as its own kernel<\/span>\npip install jupyter ipykernel\npython -m ipykernel install --user --name pytorch-lab --display-name <span class=\"hljs-string\">\"Python (pytorch-lab)\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">On Windows PowerShell, create the environment with <code>py -m venv $HOME\\venvs\\pytorch-lab<\/code> and activate it with <code>$HOME\\venvs\\pytorch-lab\\Scripts\\Activate.ps1<\/code>. The remaining commands are the same.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Always install <code>torch<\/code> and <code>torchvision<\/code> with the same command. Each torchvision release is built against one specific torch release. If you install or upgrade only one of them, you can get a mismatched pair, and that is what the original output shows: it kept an old torch and added torchvision alongside it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Choose a CPU or CUDA build<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">What <code>pip install torch<\/code> gives you depends on the platform. When you need a specific build, use the official package index with <code>--index-url<\/code>. The selector on pytorch.org generates the exact command for your operating system, package manager and CUDA version.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Target<\/th><th>Command<\/th><th>Notes<\/th><\/tr><\/thead><tbody><tr><td>Linux, default<\/td><td><code>pip install torch torchvision<\/code><\/td><td>The PyPI wheel includes CUDA support and is a large download<\/td><\/tr><tr><td>CPU only (any OS)<\/td><td><code>pip install torch torchvision --index-url https:\/\/download.pytorch.org\/whl\/cpu<\/code><\/td><td>Much smaller. A good fit for laptops, CI runners and containers without GPUs<\/td><\/tr><tr><td>Specific CUDA version<\/td><td><code>pip install torch torchvision --index-url https:\/\/download.pytorch.org\/whl\/cuXXX<\/code><\/td><td>Replace <code>cuXXX<\/code> with a CUDA tag that the selector lists for the current release<\/td><\/tr><tr><td>Windows with NVIDIA GPU<\/td><td>Use the CUDA index URL from the selector<\/td><td>The default Windows wheel on PyPI is CPU only<\/td><\/tr><tr><td>macOS (Apple Silicon)<\/td><td><code>pip install torch torchvision<\/code><\/td><td>Use the <code>mps<\/code> device for GPU acceleration<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> PyTorch no longer publishes new releases to its own conda channel, so <code>conda install pytorch -c pytorch<\/code> gives you old builds. If you use Anaconda or Miniconda, create a conda environment and run the pip commands above inside it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Verify the installation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Save the following as <code>check_torch.py<\/code> and run it with <code>python check_torch.py<\/code>, or paste it into a notebook cell. It prints the versions, reports which accelerator is available and runs a small calculation on that device.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">import torch\nimport torchvision\n\n<span class=\"hljs-keyword\">print<\/span>(<span class=\"hljs-string\">\"torch:\"<\/span>, torch.__version__)\n<span class=\"hljs-keyword\">print<\/span>(<span class=\"hljs-string\">\"torchvision:\"<\/span>, torchvision.__version__)\n<span class=\"hljs-keyword\">print<\/span>(<span class=\"hljs-string\">\"CUDA available:\"<\/span>, torch.cuda.is_available())\n<span class=\"hljs-keyword\">print<\/span>(<span class=\"hljs-string\">\"MPS available:\"<\/span>, torch.backends.mps.is_available())\n\n<span class=\"hljs-keyword\">if<\/span> torch.cuda.is_available():\n    device = torch.device(<span class=\"hljs-string\">\"cuda\"<\/span>)\n    <span class=\"hljs-keyword\">print<\/span>(<span class=\"hljs-string\">\"GPU:\"<\/span>, torch.cuda.get_device_name(<span class=\"hljs-number\">0<\/span>))\n    <span class=\"hljs-keyword\">print<\/span>(<span class=\"hljs-string\">\"CUDA runtime used by torch:\"<\/span>, torch.version.cuda)\nelif torch.backends.mps.is_available():\n    device = torch.device(<span class=\"hljs-string\">\"mps\"<\/span>)\n<span class=\"hljs-keyword\">else<\/span>:\n    device = torch.device(<span class=\"hljs-string\">\"cpu\"<\/span>)\n\nx = torch.rand(<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">3<\/span>, device=device)\ny = x @ x.T\n<span class=\"hljs-keyword\">print<\/span>(<span class=\"hljs-string\">\"Device:\"<\/span>, y.device)\n<span class=\"hljs-keyword\">print<\/span>(y)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">The version strings show which build you have. CPU-only wheels from the PyTorch index end in <code>+cpu<\/code>, and CUDA wheels from that index end in a tag such as <code>+cuXXX<\/code>. On a working GPU machine, <code>CUDA available<\/code> prints <code>True<\/code> and the final tensor shows <code>device=&#039;cuda:0&#039;<\/code>. On a CPU-only machine, the tensor still prints, which confirms that PyTorch itself works.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Running pip from Jupyter correctly<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>!pip<\/code> runs whichever <code>pip<\/code> is first on the shell&#039;s PATH. That may not belong to the Python environment the notebook kernel uses, so the package can install successfully and <code>import torch<\/code> still fails. The <code>%pip<\/code> magic always installs into the running kernel&#039;s environment:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">%pip install torch torchvision<\/code><\/span><\/pre>\n\n\n<p class=\"wp-block-paragraph\">After installing or upgrading in a notebook, restart the kernel before importing. Otherwise the old version may still be loaded in memory. To check which interpreter the kernel uses, run <code>import sys; print(sys.executable)<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common errors and fixes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Symptom<\/th><th>Likely cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td><code>ModuleNotFoundError: No module named &#039;torch&#039;<\/code> in Jupyter<\/td><td>The package went into a different environment from the kernel<\/td><td>Use <code>%pip<\/code>, or select the <code>pytorch-lab<\/code> kernel you registered with <code>ipykernel<\/code><\/td><\/tr><tr><td><code>No matching distribution found for torch<\/code><\/td><td>Unsupported Python version, 32-bit Python, or an unsupported platform<\/td><td>Install a supported 64-bit Python and recreate the virtual environment<\/td><\/tr><tr><td><code>torch.cuda.is_available()<\/code> returns <code>False<\/code> on a GPU machine<\/td><td>A CPU-only wheel is installed, or the NVIDIA driver is missing or too old<\/td><td>Check that <code>nvidia-smi<\/code> works, then reinstall using a CUDA index URL from the selector<\/td><\/tr><tr><td><code>operator torchvision::nms does not exist<\/code> or similar import errors<\/td><td>torch and torchvision versions do not match<\/td><td><code>pip install --upgrade torch torchvision<\/code> together, using the same index URL<\/td><\/tr><tr><td>Install fails partway with <code>No space left on device<\/code><\/td><td>The CUDA wheels plus the pip cache need a lot of space<\/td><td>Free some disk space, or use <code>pip install --no-cache-dir ...<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Good practices for lab and team setups<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use one virtual environment per project. Do not install PyTorch into the system Python, and do not use <code>sudo pip<\/code>.<\/li>\n\n\n<li>Pin exact versions in a <code>requirements.txt<\/code> file, for example by running <code>pip freeze &gt; requirements.txt<\/code>, so the next labs and your teammates get the same build.<\/li>\n\n\n<li>If you pin a CUDA build, record the index URL alongside the pins. <code>pip freeze<\/code> does not save it.<\/li>\n\n\n<li>For CI pipelines and containers that have no GPU, use the CPU index. Image sizes and build times drop considerably.<\/li>\n\n\n<li>Install packages only from PyPI or download.pytorch.org. Avoid similarly named packages from other sources.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Do I need to install the CUDA Toolkit before installing PyTorch?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. The PyTorch CUDA wheels include the CUDA runtime libraries they need. You only need a compatible NVIDIA driver, and you can check it with <code>nvidia-smi<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I check whether PyTorch can use my GPU?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Run <code>import torch; print(torch.cuda.is_available())<\/code>. It prints <code>True<\/code> when an NVIDIA GPU is usable. On Apple Silicon, check <code>torch.backends.mps.is_available()<\/code> instead.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Should I use pip or conda to install PyTorch?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use pip. PyTorch no longer publishes new releases to its own conda channel. Conda users can still create a conda environment and run <code>pip install torch torchvision<\/code> inside it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why does import torch fail in Jupyter after a successful pip install?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>!pip<\/code> probably installed into a different Python environment from the notebook kernel. Use <code>%pip install torch torchvision<\/code>, restart the kernel, and check <code>sys.executable<\/code> to confirm which interpreter the kernel is using.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Is torchvision required?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It is not required to use PyTorch, but this lab series uses it for image datasets, transforms and pretrained models. Install it in the same command as torch so the two versions match.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/artificial-intelligence\/machine-learning\/\">More Machine Learning tutorials<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/staff-mlops-engineer-role-blueprint-responsibilities-skills-kpis-and-career-path\/\">Staff MLOps Engineer: Role Blueprint, Responsibilities, Skills, KPIs, and Career Path<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/lead-machine-learning-engineer-role-blueprint-responsibilities-skills-kpis-and-career-path\/\">Lead Machine Learning Engineer: Role Blueprint, Responsibilities, Skills, KPIs, and Career Path<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This lab installs PyTorch and torchvision so they are ready for the rest of the PyTorch lab series. The notebook above runs pip install torch torchvision from&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[6512],"tags":[7364],"class_list":["post-47561","post","type-post","status-publish","format-standard","hentry","category-machine-learning","tag-pytorch"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47561","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=47561"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47561\/revisions"}],"predecessor-version":[{"id":80165,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47561\/revisions\/80165"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=47561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=47561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=47561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}