LLMs-from-scratch/setup/01_optional-python-setup-preferences
Sebastian Raschka e9c4dac3ad
Update README.md
2025-02-15 13:17:43 -06:00
..
2025-02-15 13:17:43 -06:00

Python Setup Tips

There are several ways to install Python and set up your computing environment. Here, I share my personal preferences.

I have been a long-time user of Conda and pip, but recently, the uv package has gained significant traction as it provides a faster and more efficient way to install packages and resolve dependencies.

I recommend starting with Option 1: Using uv as it is the more modern approach in 2025. If you encounter problems with Option 1, consider Option 2: Using Conda.

 

Option 1: Using uv

 

This section guides you through the Python setup and package installation procedure using uv.

In this tutorial, I am using a computer running macOS, but this workflow is similar for Linux machines and may work for other operating systems as well.

1. Install Python (if not installed)

First, check if you have a modern version of Python installed (I recommend 3.10 or newer) by executing the following code in the terminal:

python --version

If it returns 3.10 or newer, no further action is required.

Note

I recommend installing a Python version that is at least 2 versions older than the most recent release to ensure PyTorch compatibility. For example, if the most recent version is Python 3.13, I recommend installing version 3.10 or 3.11.

Otherwise, if Python is not installed or is an older version, you can install it for your operating system as described below.

No Python Found

  Linux (Ubuntu/Debian)

sudo apt update
sudo apt install python3.10 python3.10-venv python3.10-dev

  macOS

If you use Homebrew, install Python with:

brew install python@3.10

Alternatively, download and run the installer from the official website: https://www.python.org/downloads/.

I recommend installing a Python version that is at least two versions older than the latest release to ensure PyTorch compatibility.

Python version

  Windows

Download and run the installer from the official website: https://www.python.org/downloads/.

Obtain and execute the installer from the official website: https://www.python.org/downloads/.

I recommend installing a Python version that is at least 2 versions older than the most recent release to ensure PyTorch compatibility. For example, if the most recent version is Python 3.13, I recommend installing version 3.10 or 3.11.

 

2. Create a virtual environment

I highly recommend installing Python packages in a separate virtual environment to avoid modifying system-wide packages that your OS may depend on. To create a virtual environment in the current folder, follow the three steps below.

  1. Install uv

pip install uv

  2. Create the virtual environment

uv venv --python=python3.10

  3. Activate the virtual environment

source .venv/bin/activate

 

Note

If you are using Windows, you may have to replace the command above by source .venv/Scripts/activate

Note that you need to activate the virtual environment each time you start a new terminal session. For example, if you restart your terminal or computer and want to continue working on the project the next day, simply run source .venv/bin/activate in the project folder to reactivate your virtual environment.

Venv activated

Optionally, you can deactivate the environment it by executing the command deactivate.

Venv deactivated

 

3. Install packages

After activating your virtual environment, you can install Python packages using uv. For example:

uv pip install packaging

To install all required packages from a requirements.txt file (such as the one located at the top level of this GitHub repository) run the following command, assuming the file is in the same directory as your terminal session:

uv pip install -U -r requirements.txt

Alternatively, install the latest dependencies directly from the repository:

uv pip install -U -r https://raw.githubusercontent.com/rasbt/LLMs-from-scratch/refs/heads/main/requirements.txt
Uv install

  Finalizing the setup

Thats it! Your environment should now be ready for running the code in the repository.

Optionally, you can run an environment check by executing the python_environment_check.py script in this repostiory:

python setup/02_installing-python-libraries/python_environment_check.py
Environment check

If you encounter any issues with specific packages, try reinstalling them using:

uv pip install packagename

(Here, packagename is a placeholder name that needs to be replaced with the package name you are having problems with.)

If problems persist, consider opening a discussion on GitHub or working through the Option 2: Using Conda section below.

  Start working with the code

Once everything is set up, you can start working with the code files. For instance, launch JupyterLab by running:

jupyterlab
Uv install

 

 

Option 2: Using Conda

This section guides you through the Python setup and package installation procedure using uv.

In this tutorial, I am using a computer running macOS, but this workflow is similar for Linux machines and may work for other operating systems as well.

 

1. Download and install Miniforge

Download miniforge from the GitHub repository here.

download

Depending on your operating system, this should download either an .sh (macOS, Linux) or .exe file (Windows).

For the .sh file, open your command line terminal and execute the following command

sh ~/Desktop/Miniforge3-MacOSX-arm64.sh

where Desktop/ is the folder where the Miniforge installer was downloaded to. On your computer, you may have to replace it with Downloads/.

miniforge-install

Next, step through the download instructions, confirming with "Enter".

 

2. Create a new virtual environment

After the installation was successfully completed, I recommend creating a new virtual environment called LLMs, which you can do by executing

conda create -n LLMs python=3.10
new-env

Many scientific computing libraries do not immediately support the newest version of Python. Therefore, when installing PyTorch, it's advisable to use a version of Python that is one or two releases older. For instance, if the latest version of Python is 3.13, using Python 3.10 or 3.11 is recommended.

Next, activate your new virtual environment (you have to do it every time you open a new terminal window or tab):

conda activate LLMs
activate-env

 

Optional: styling your terminal

If you want to style your terminal similar to mine so that you can see which virtual environment is active, check out the Oh My Zsh project.

 

3. Install new Python libraries

To install new Python libraries, you can now use the conda package installer. For example, you can install JupyterLab and watermark as follows:

conda install jupyterlab watermark
conda-install

You can also still use pip to install libraries. By default, pip should be linked to your new LLms conda environment:

check-pip

 

4. Install PyTorch

PyTorch can be installed just like any other Python library or package using pip. For example:

pip install torch

However, since PyTorch is a comprehensive library featuring CPU- and GPU-compatible codes, the installation may require additional settings and explanation (see the A.1.3 Installing PyTorch in the book for more information).

It's also highly recommended to consult the installation guide menu on the official PyTorch website at https://pytorch.org.

 

5. Installing Python packages and libraries used in this book

Please refer to the Installing Python packages and libraries used in this book document for instructions on how to install the required libraries.



Any questions? Please feel free to reach out in the Discussion Forum.