Run a Powerful LLM Locally on Your Laptop

Did you know that you can run powerful, multi-billion-parameter large language models like Llama 3.1 and Mistral on your own laptop? Not long ago, this seemed impossible. Accessing these models meant relying on a cloud service, paying for an API, and sending your data to a server you didn’t control. But that’s changing, fast. A new wave of tools has emerged, and for me, one stands out for its sheer simplicity and power: Ollama. In this article, I’ll guide you through how to run a powerful LLM locally on your laptop using Ollama.

What is Ollama?

Think of Ollama as a personal, local workshop for LLMs.

In a typical workshop, you have your tools, workbench, and raw materials. Ollama is the software that bundles everything you need into one tidy package:

  • The Model Weights: The brain of the LLM (e.g., Llama 3.1).
  • The Configuration: All the settings that tell the model how to behave.
  • The Engine: The code needed to actually run the model efficiently on your specific hardware (your Mac, Windows, or Linux machine).

Ollama is an open-source tool that manages all this complexity for you. It hides the difficult setup and gives you a simple command-line interface (and an API) to download, manage, and interact with a huge library of open-source models.

Your 3-Step Guide to Run a Powerful LLM Locally on Your Laptop using Ollama

This is the best part, it’s incredibly easy. You don’t need to be a systems expert.

Step 1: Install Ollama

First, you need to get the Ollama application. The process is slightly different for each operating system, but it’s simple for all:

  1. On macOS: Go to the ollama.com website and download the .zip file. Unzip it, and move the Ollama.app into your Applications folder. That’s it!
  2. On Windows: Go to the same website and download the .exe installer. Run the installer, and it will set everything up for you.

Once installed, open your terminal (or Command Prompt on Windows) and type ollama -v to verify it’s working.

Step 2: Pull Your First Model

Now, let’s download a model. We’ll use Mistral, which is a fantastic, high-performing model that’s a great size for most laptops. In your terminal of your VS Code (or anywhere you are working), just type:

ollama pull mistral

It will show you something like this:

(venv) (base) amankharwal@Amans-MacBook-Pro multiagents % ollama pull mistral
pulling manifest
pulling f5074b1221da: 100% ▕████████████████████████████████████████▏ 4.4 GB
pulling 43070e2d4e53: 100% ▕████████████████████████████████████████▏ 11 KB
pulling 1ff5b64b61b9: 100% ▕████████████████████████████████████████▏ 799 B
pulling ed11eda7790d: 100% ▕████████████████████████████████████████▏ 30 B
pulling 1064e17101bd: 100% ▕████████████████████████████████████████▏ 487 B
verifying sha256 digest
writing manifest
success
(venv) (base) amankharwal@Amans-MacBook-Pro multiagents %

Ollama will find the model in its library, download it (it might take a few minutes), and store it locally on your machine.

Step 3: Bring Your Model Into Your Code

When you run Ollama, it starts a lightweight server in the background on your machine. All we need to do is have our code talk to that server. The Ollama team has made this incredibly simple with an official Python library.

First, you’ll need to install the client library using pip. Open your terminal (not the Ollama chat, just a regular terminal on your VS Code) and run:

pip install ollama

Now, let’s write a simple Python script (you can save this as app.py). This script will connect to your local Ollama server, send a prompt to the Mistral model, and print the response (make sure your Ollama application is running in the background!):

import ollama

# Let's connect to the model and ask a question
try:
    # Make sure 'mistral' is downloaded (ollama pull mistral)
    response = ollama.chat(
        model='mistral',
        messages=[
            {'role': 'user', 'content': 'How can I write a simple Python function to add two numbers?'}
        ]
    )

    # Print out the assistant's response
    print("\n--- AI Assistant Response ---")
    print(response['message']['content'])
    print("-----------------------------\n")

except Exception as e:
    print(f"\n[Error] Could not connect to Ollama.")
    print(f"Details: {e}")
    print("Please make sure Ollama is running and you have pulled the 'mistral' model.\n")

Save the file and run it from your terminal:

python app.py

You should see the output:

--- AI Assistant Response ---
Here is a simple Python function that adds two numbers:

```python
def add_two_numbers(num1, num2):
return num1 + num2

# Test the function
result = add_two_numbers(3, 4)
print(result) # Outputs: 7
```

You can call this function by passing two numbers as arguments. The function adds these numbers and returns their sum. In the example above, I have tested the function with the numbers 3 and 4, and it correctly outputs 7.
-----------------------------

Final Words

As students and builders, this is more than just a cool tool. It’s a fundamental shift in our relationship with AI. Now, you can experiment with LLMs using your own sensitive data, your personal notes, your company’s proprietary code, and your private journal with zero risk.

You can also use this method to build any project based on Generative AI and AI Agents.

I hope you liked this article on how to run a powerful LLM locally on your laptop using Ollama. Follow me on Instagram for many more resources.

Aman Kharwal
Aman Kharwal

AI/ML Engineer | Published Author. My aim is to decode data science for the real world in the most simple words.

Articles: 2017

2 Comments

Leave a Reply

Discover more from AmanXai by Aman Kharwal

Subscribe now to keep reading and get access to the full archive.

Continue reading