<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Pranjali Sachan | Blog]]></title><description><![CDATA[Pranjali Sachan | Blog]]></description><link>https://pranjalisachan.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 07 Nov 2026 10:18:22 GMT</lastBuildDate><atom:link href="https://pranjalisachan.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Deploying Gemma 1B GGUF model with llama.cpp on Ubuntu]]></title><description><![CDATA[This article is a step-by-step guide to run gemma-1b-it-gguf model using llama.cpp on CPU or GPU using CUDA and deploy on an Ubuntu server.]]></description><link>https://pranjalisachan.hashnode.dev/deploying-gemma-1b-gguf-model-with-llamacpp-on-ubuntu</link><guid isPermaLink="true">https://pranjalisachan.hashnode.dev/deploying-gemma-1b-gguf-model-with-llamacpp-on-ubuntu</guid><category><![CDATA[llama-server]]></category><category><![CDATA[gemma-1b-it]]></category><category><![CDATA[llamacpp]]></category><category><![CDATA[gemma]]></category><category><![CDATA[Google]]></category><category><![CDATA[tmux]]></category><category><![CDATA[Ubuntu]]></category><category><![CDATA[NVIDIA]]></category><category><![CDATA[unsloth]]></category><category><![CDATA[cuda]]></category><category><![CDATA[CMake]]></category><category><![CDATA[GitHub]]></category><category><![CDATA[Python]]></category><category><![CDATA[huggingface]]></category><category><![CDATA[nginx]]></category><dc:creator><![CDATA[Pranjali Sachan]]></dc:creator><pubDate>Sun, 23 Mar 2025 20:05:12 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1742754987679/447e5340-5d32-4050-8e71-e5c70d725d7d.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>For this guide we should be using an Ubuntu server with atleast 2 GiB of GPU VRAM, 2 VCPU Cores, 8 GiB RAM, and 50 GiB of storage.</p>
<p>Let’s start by updating and installing necessary libraries.</p>
<pre><code class="lang-bash">sudo apt update &amp;&amp; sudo apt upgrade -y
sudo apt install -y build-essential cmake git curl
</code></pre>
<p>For GPU acceleration, we would need <strong>CUDA</strong> (for NVIDIA GPUs).</p>
<pre><code class="lang-bash">sudo apt install -y nvidia-cuda-toolkit
</code></pre>
<hr />
<p>What is <strong>CUDA</strong>?</p>
<p><strong>CUDA (Compute Unified Device Architecture)</strong> is a parallel computing platform developed by NVIDIA that allows developers to use NVIDIA GPUs for general-purpose computing (GPGPU).</p>
<p>Why is CUDA important?</p>
<ul>
<li><p><strong>Massive Parallelism:</strong> Uses thousands of GPU cores for simultaneous calculations.</p>
</li>
<li><p><strong>High Performance:</strong> Faster than CPU-based processing for AI/ML, gaming, and scientific computing.</p>
</li>
<li><p><strong>Deep Learning Optimization:</strong> Enables efficient execution of frameworks like <strong>PyTorch, TensorFlow, and LLaMA.cpp</strong>.</p>
</li>
</ul>
<p>How CUDA works?</p>
<p>CUDA enables <strong>GPU acceleration</strong> by offloading computations from the CPU to the GPU.</p>
<ul>
<li><p><strong>Host (CPU):</strong> Sends tasks to the GPU.</p>
</li>
<li><p><strong>Device (GPU):</strong> Executes tasks in parallel.</p>
</li>
<li><p><strong>Memory Transfer:</strong> Data is moved between CPU (RAM) and GPU (VRAM).</p>
</li>
</ul>
<hr />
<p>Next, we will clone and build <a target="_blank" href="https://github.com/ggml-org/llama.cpp"><strong>llama.cpp</strong></a></p>
<pre><code class="lang-bash">git <span class="hljs-built_in">clone</span> https://github.com/ggml-org/llama.cpp.git
<span class="hljs-built_in">cd</span> llama.cpp
mkdir build &amp;&amp; <span class="hljs-built_in">cd</span> build

<span class="hljs-comment"># Important!</span>
cmake .. <span class="hljs-comment"># CPU only (No GPU)</span>
cmake .. -DLLAMA_CUDA=ON <span class="hljs-comment"># NVIDIA GPU (CUDA)</span>

cmake --build . --config Release
<span class="hljs-built_in">cd</span>
</code></pre>
<p>Great! You can view the build output inside <code>llama.cpp/build/bin</code> directory.</p>
<hr />
<p>Now, we need to download our quantized model from <a target="_blank" href="https://huggingface.co/unsloth/gemma-3-1b-it-GGUF">huggingface.co</a>. To download from huggingface.co, we will install <code>huggingface-cli</code> using <code>pip</code>. First, check if <code>python</code> and <code>pip</code> are installed?</p>
<pre><code class="lang-bash">python3 --version
pip3 --version

<span class="hljs-comment"># If Not Installed, run</span>
sudo apt install python3 python3-pip -y
</code></pre>
<p>Next, create a virtual env, activate it, and install <code>huggingface-cli</code>:</p>
<pre><code class="lang-bash">python3 -m venv environment_name <span class="hljs-comment"># replace environment_name with the name of your choice</span>
<span class="hljs-built_in">source</span> environment_name/bin/activate
pip3 install --upgrade huggingface_hub
<span class="hljs-comment"># deactivate # use this to deactivate the virtual env after when finished downloading the model</span>
</code></pre>
<p>At this point, you would need to create an account on <a target="_blank" href="https://huggingface.co/join">huggingface.co</a> and create an <a target="_blank" href="https://huggingface.co/settings/tokens">access token</a> that we can use to login in the <code>huggingface-cli</code> on our server:</p>
<pre><code class="lang-bash">huggingface-cli login
<span class="hljs-comment"># It should prompt you to enter your access token!</span>
</code></pre>
<p>Now, we’re ready to download our model - <a target="_blank" href="https://huggingface.co/unsloth/gemma-3-1b-it-GGUF">unsloth/gemma-3-1b-it-GGUF</a>. You can use any similar model that satisfies the minimum hardware requirements. We will be downloading <a target="_blank" href="https://huggingface.co/unsloth/gemma-3-1b-it-GGUF/blob/main/gemma-3-1b-it-Q4_K_M.gguf">gemma-3-1b-it-Q4_K_M.gguf</a> for balanced performance.</p>
<pre><code class="lang-bash">huggingface-cli download unsloth/gemma-3-1b-it-GGUF gemma-3-1b-it-Q4_K_M.gguf --local-dir ./models
</code></pre>
<p>What is <code>Q4_K_M</code> in models?</p>
<p><code>Q4_K_M</code> is a quantization format used in <code>.GGUF</code> models to reduce model size while maintaining accuracy and performance. It uses less VRAM, had good accuracy, and works on both GPUs and CPUs.</p>
<hr />
<p>We will utilize <code>tmux</code> to run the model in background. Ensure <code>tmux</code> is installed:</p>
<pre><code class="lang-bash">tmux -V <span class="hljs-comment"># check tmux version</span>
sudo apt install tmux -y <span class="hljs-comment"># run only if tmux command not found</span>

<span class="hljs-comment"># start a new tmux session</span>
tmux new -s llama-server
</code></pre>
<p>Awesome, we’re now ready to run the model using <code>llama.cpp/build/bin/lama-server</code> which will start a lightweight chat ui on <code>127.0.0.1:8080</code> to interact with the model.</p>
<pre><code class="lang-bash">llama.cpp/build/bin/llama-server -m models/gemma-3-1b-it-Q4_K_M.gguf --n-gpu-layers 20 --threads 2
</code></pre>
<p>What is <code>—n-gpu-layers 20</code> flag?</p>
<p>It specifies how many layers of the model should be offloaded on the GPU instead of running on CPU.</p>
<p>How to Choose the Right Value?</p>
<ul>
<li><p><strong>Higher value (</strong><code>100+</code><strong>)</strong> → More layers on GPU → <strong>Faster inference</strong> but requires <strong>more VRAM</strong>.</p>
</li>
<li><p><strong>Lower value (</strong><code>10-50</code>) → Fewer layers on GPU → <strong>Slower inference</strong> but consumes <strong>less VRAM</strong>.</p>
</li>
</ul>
<p>Typical VRAM Requirements:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Model</td><td>Layers on GPU (<code>--n-gpu-layers</code>)</td><td>VRAM Needed</td></tr>
</thead>
<tbody>
<tr>
<td>3B</td><td>20-50</td><td>~4GB</td></tr>
<tr>
<td>7B</td><td>40-70</td><td>~6GB</td></tr>
<tr>
<td>13B</td><td>80-100</td><td>~10GB</td></tr>
<tr>
<td>33B</td><td>100+</td><td>~24GB+</td></tr>
</tbody>
</table>
</div><p>We can move the <code>tmux</code> session <code>llama-server</code> in the background by pressing <code>Ctrl + B</code> followed by <code>D</code>. Below are additional commands to list, re-attach, or kill a <code>tmux</code> session:</p>
<pre><code class="lang-bash">tmux ls <span class="hljs-comment"># list tmux sessions</span>
tmux attach -t llama-server <span class="hljs-comment"># re-attach session running llama-server</span>
tmux kill-session -t llama-server <span class="hljs-comment"># kill sessing running llama-server</span>
</code></pre>
<p>Now, to access the chat ui from outside the server, we can setup a reverse-proxy to chat ui running locally on <code>127.0.0.1:8080</code>. Let’s setup a reverse-proxy using NGINX.</p>
<pre><code class="lang-bash">sudo apt install nginx -y   <span class="hljs-comment"># Install NGINX</span>
nginx -v                    <span class="hljs-comment"># Check if installed correctly</span>
sudo systemctl start nginx  <span class="hljs-comment"># Start NGINX service</span>
sudo systemctl status nginx <span class="hljs-comment"># Check status</span>
</code></pre>
<p>Next, we will create a NGINX configuration file and call it <code>llama-server</code>:</p>
<pre><code class="lang-bash">sudo nano /etc/nginx/sites-available/llama-server
</code></pre>
<p>Add the following configuration to the file and replace the <code>server_name</code> with the actual IP address of your server:</p>
<pre><code class="lang-nginx"><span class="hljs-section">server</span> {
    <span class="hljs-attribute">listen</span> <span class="hljs-number">80</span>;
    <span class="hljs-attribute">server_name</span> yourdomain.com; <span class="hljs-comment"># replace with actual IP addr of server</span>

    <span class="hljs-attribute">location</span> / {
        <span class="hljs-attribute">proxy_pass</span> http://127.0.0.1:8080; <span class="hljs-comment"># since our chat ui is running locally on 8080</span>
        <span class="hljs-attribute">proxy_set_header</span> Host <span class="hljs-variable">$host</span>;
        <span class="hljs-attribute">proxy_set_header</span> X-Real-IP <span class="hljs-variable">$remote_addr</span>;
        <span class="hljs-attribute">proxy_set_header</span> X-Forwarded-For <span class="hljs-variable">$proxy_add_x_forwarded_for</span>;
        <span class="hljs-attribute">proxy_set_header</span> X-Forwarded-Proto <span class="hljs-variable">$scheme</span>;
    }
}
</code></pre>
<p>Next, enable the configuration file and restart NGINX service:</p>
<pre><code class="lang-bash">sudo ln -s /etc/nginx/sites-available/llama-server /etc/nginx/sites-enabled/ <span class="hljs-comment"># enable config</span>
sudo nginx -t <span class="hljs-comment"># check for syntax errors</span>
sudo systemctl restart nginx <span class="hljs-comment"># restart NGINX service</span>
</code></pre>
<p>It is recommended to add a Firewall rule to limit access to server from your IP address. I will publish a different article on how to limit access to your server but for now, we will enable the firewall and allow <code>http</code> traffic on our server:</p>
<pre><code class="lang-bash">ufw allow http <span class="hljs-comment"># enable http traffic</span>
ufw <span class="hljs-built_in">enable</span>     <span class="hljs-comment"># enable firewall</span>
ufw status     <span class="hljs-comment"># check if http rule is added</span>
</code></pre>
<p>Great! Now, you should be able to access the chat ui to interact with the model by navigating to <code>http://ip-of-server/</code> in your browser!</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1742759305012/6b365ef9-bfdd-43d6-ab54-1f3e8376297f.png" alt="Screenshot" class="image--center mx-auto" /></p>
<p>Thank you for reading this guide! I appreciate any comments or suggestions from readers!</p>
]]></content:encoded></item></channel></rss>