{"id":705,"date":"2025-12-09T19:08:28","date_gmt":"2025-12-09T11:08:28","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=705"},"modified":"2025-12-09T19:08:28","modified_gmt":"2025-12-09T11:08:28","slug":"linux%e4%b8%8a%e8%ae%be%e7%bd%aeollama%e6%9c%8d%e5%8a%a1%e9%85%8d%e7%bd%ae%e5%b8%b8%e7%94%a8%e7%8e%af%e5%a2%83%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/09\/linux%e4%b8%8a%e8%ae%be%e7%bd%aeollama%e6%9c%8d%e5%8a%a1%e9%85%8d%e7%bd%ae%e5%b8%b8%e7%94%a8%e7%8e%af%e5%a2%83%e5%8f%98%e9%87%8f\/","title":{"rendered":"Linux\u4e0a\u8bbe\u7f6eOllama\u670d\u52a1\u914d\u7f6e(\u5e38\u7528\u73af\u5883\u53d8\u91cf)"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u6574\u7406\u4e00\u4efd <strong>\u300aLinux \u4e0a Ollama \u670d\u52a1\u914d\u7f6e\u53ca\u5e38\u7528\u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\u6307\u5357\u300b<\/strong>\uff0c\u65b9\u4fbf\u4f60\u5728\u670d\u52a1\u5668\u6216\u672c\u5730 Linux \u7cfb\u7edf\u4e0a\u8fd0\u884c Ollama \u670d\u52a1\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>\u4e00\u3001Ollama \u670d\u52a1\u542f\u52a8\u6a21\u5f0f<\/strong><\/h1>\n\n\n\n<p>Ollama \u5728 Linux \u4e0a\u901a\u5e38\u6709\u4e24\u79cd\u8fd0\u884c\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u547d\u4ee4\u884c\u4ea4\u4e92\u6a21\u5f0f\uff08REPL\uff09<\/strong><\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nollama run deepseek-r1:7b\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>\u76f4\u63a5\u5728\u7ec8\u7aef\u4ea4\u4e92<\/li>\n\n\n\n<li>\u7528\u4e8e\u8c03\u8bd5\u548c\u6d4b\u8bd5<\/li>\n<\/ul>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u540e\u53f0\u670d\u52a1\u6a21\u5f0f\uff08REST API \/ Daemon\uff09<\/strong><\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nollama serve\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>\u542f\u52a8 HTTP REST \u670d\u52a1<\/li>\n\n\n\n<li>\u9ed8\u8ba4\u7aef\u53e3\u901a\u5e38\u4e3a <strong>11434<\/strong><\/li>\n\n\n\n<li>\u53ef\u4f9b\u5176\u4ed6\u5e94\u7528\u6216\u811a\u672c\u8c03\u7528<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u5efa\u8bae\u5728\u670d\u52a1\u5668\u4e0a\u4f7f\u7528 <code>ollama serve<\/code> \u5e76\u7ed3\u5408 systemd \/ Docker \u540e\u53f0\u7ba1\u7406\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>\u4e8c\u3001\u5e38\u7528\u73af\u5883\u53d8\u91cf\u914d\u7f6e<\/strong><\/h1>\n\n\n\n<p>\u5728 Linux \u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7 <strong>export<\/strong> \u6216\u5728 <code>.bashrc<\/code> \/ <code>.zshrc<\/code> \u4e2d\u8bbe\u7f6e Ollama \u5e38\u7528\u73af\u5883\u53d8\u91cf\uff0c\u4ee5\u63a7\u5236\u8fd0\u884c\u53c2\u6570\u3001\u8def\u5f84\u548c\u8d44\u6e90\u5206\u914d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u6a21\u578b\u8def\u5f84 \/ \u672c\u5730\u7f13\u5b58<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nexport OLLAMA_MODEL_DIR=\/opt\/ollama\/models\nexport OLLAMA_CACHE_DIR=\/opt\/ollama\/cache\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>OLLAMA_MODEL_DIR<\/strong>\uff1a\u5b58\u653e\u5df2\u4e0b\u8f7d\u6a21\u578b\u7684\u8def\u5f84<\/li>\n\n\n\n<li><strong>OLLAMA_CACHE_DIR<\/strong>\uff1a\u8fd0\u884c\u4e2d\u4ea7\u751f\u7684\u4e34\u65f6\u7f13\u5b58<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u5efa\u8bae\u4f7f\u7528 SSD \u8def\u5f84\u4ee5\u63d0\u5347\u52a0\u8f7d\u548c\u63a8\u7406\u901f\u5ea6<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. GPU \/ \u7ebf\u7a0b\u63a7\u5236<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nexport OLLAMA_NUM_THREADS=16       # CPU \u6838\u5fc3\u6570\nexport OLLAMA_NUM_GPU=1            # \u4f7f\u7528 GPU \u6570\u91cf\nexport OLLAMA_GPU_LAYERS=30        # GPU \u52a0\u901f\u7684\u5c42\u6570\uff08\u91cf\u5316\u6a21\u578b\u53ef\u8c03\uff09\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>CPU \u63a8\u7406\u65f6 <code>OLLAMA_NUM_THREADS<\/code> \u51b3\u5b9a\u5e76\u884c\u5ea6<\/li>\n\n\n\n<li>GPU \u63a8\u7406\u53ef\u901a\u8fc7 <code>OLLAMA_NUM_GPU<\/code> \u6307\u5b9a\u4f7f\u7528\u7684 GPU<\/li>\n\n\n\n<li>\u5bf9\u5927\u6a21\u578b\uff0c\u53ef\u4f7f\u7528 <code>OLLAMA_GPU_LAYERS<\/code> \u53ea\u7528 GPU \u63a8\u7406\u90e8\u5206\u5c42\uff0c\u8282\u7701\u663e\u5b58<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u65e5\u5fd7\u4e0e\u8c03\u8bd5<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nexport OLLAMA_LOG_LEVEL=info       # debug \/ info \/ warn \/ error\nexport OLLAMA_LOG_FILE=\/var\/log\/ollama.log\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>OLLAMA_LOG_LEVEL<\/strong>\uff1a\u63a7\u5236\u65e5\u5fd7\u8be6\u7ec6\u5ea6<\/li>\n\n\n\n<li><strong>OLLAMA_LOG_FILE<\/strong>\uff1a\u6307\u5b9a\u65e5\u5fd7\u8f93\u51fa\u8def\u5f84<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. REST \u670d\u52a1\u914d\u7f6e<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nexport OLLAMA_HOST=0.0.0.0         # \u76d1\u542c\u5730\u5740\nexport OLLAMA_PORT=11434           # \u670d\u52a1\u7aef\u53e3\nexport OLLAMA_MAX_REQUESTS=1000    # \u5e76\u53d1\u8bf7\u6c42\u9650\u5236\nexport OLLAMA_TIMEOUT=300          # \u8bf7\u6c42\u8d85\u65f6\uff08\u79d2\uff09\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>\u7528\u4e8e\u540e\u53f0 API \u670d\u52a1\uff0c\u53ef\u7ed3\u5408 nginx \/ reverse proxy<\/li>\n\n\n\n<li>\u5efa\u8bae\u751f\u4ea7\u73af\u5883\u8bbe\u7f6e\u5408\u7406\u7684 <code>MAX_REQUESTS<\/code> \u548c <code>TIMEOUT<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u6a21\u578b\u53c2\u6570\u4f18\u5316\uff08\u53ef\u9009\uff09<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nexport OLLAMA_MAX_TOKENS=2048      # \u6a21\u578b\u4e0a\u4e0b\u6587\u7a97\u53e3\nexport OLLAMA_TEMPERATURE=0.7      # \u751f\u6210\u968f\u673a\u6027\nexport OLLAMA_TOP_P=0.9             # top-p \u91c7\u6837\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>\u9002\u7528\u4e8e\u751f\u6210\u5f0f\u6587\u672c\u63a7\u5236<\/li>\n\n\n\n<li>\u4e0e Python \/ REST \u8c03\u7528\u4e2d <code>messages<\/code> \u914d\u5408\u4f7f\u7528<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>\u4e09\u3001\u6c38\u4e45\u751f\u6548\u65b9\u5f0f<\/strong><\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u4e34\u65f6\u751f\u6548\uff08\u5f53\u524d\u7ec8\u7aef\u6709\u6548\uff09\uff1a<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nexport OLLAMA_MODEL_DIR=\/opt\/ollama\/models\n\n<\/pre><\/div>\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u6c38\u4e45\u751f\u6548\uff08\u6bcf\u6b21\u5f00\u673a\u81ea\u52a8\u52a0\u8f7d\uff09\uff1a<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# \u7f16\u8f91 ~\/.bashrc \u6216 ~\/.zshrc\nnano ~\/.bashrc\n\n# \u6dfb\u52a0\u5982\u4e0b\u884c\nexport OLLAMA_MODEL_DIR=\/opt\/ollama\/models\nexport OLLAMA_NUM_THREADS=16\nexport OLLAMA_HOST=0.0.0.0\nexport OLLAMA_PORT=11434\n\n# \u4fdd\u5b58\u5e76\u6267\u884c\nsource ~\/.bashrc\n\n<\/pre><\/div>\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>systemd \u670d\u52a1\u7ba1\u7406<\/strong>\uff08\u751f\u4ea7\u73af\u5883\u63a8\u8350\uff09\uff1a<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# \/etc\/systemd\/system\/ollama.service\n&#x5B;Unit]\nDescription=Ollama LLM Service\nAfter=network.target\n\n&#x5B;Service]\nType=simple\nUser=ollama\nEnvironment=&quot;OLLAMA_MODEL_DIR=\/opt\/ollama\/models&quot;\nEnvironment=&quot;OLLAMA_NUM_THREADS=16&quot;\nEnvironment=&quot;OLLAMA_HOST=0.0.0.0&quot;\nEnvironment=&quot;OLLAMA_PORT=11434&quot;\nExecStart=\/usr\/local\/bin\/ollama serve\nRestart=always\n\n&#x5B;Install]\nWantedBy=multi-user.target\n\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsudo systemctl daemon-reload\nsudo systemctl enable ollama\nsudo systemctl start ollama\nsudo systemctl status ollama\n\n<\/pre><\/div>\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u8fd9\u6837 Ollama \u5c06\u968f\u7cfb\u7edf\u542f\u52a8\uff0c\u5e76\u8fd0\u884c\u5728\u540e\u53f0\uff0c\u4fbf\u4e8e\u957f\u671f\u670d\u52a1\u90e8\u7f72\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>\u56db\u3001\u8c03\u7528\u793a\u4f8b\uff08Linux \u4e0b\u6d4b\u8bd5 REST API\uff09<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncurl http:\/\/localhost:11434\/api\/chat -d &#039;{\n  &quot;model&quot;:&quot;deepseek-r1:7b&quot;,\n  &quot;messages&quot;:&#x5B;{&quot;role&quot;:&quot;user&quot;,&quot;content&quot;:&quot;Explain AI in simple terms&quot;}],\n  &quot;stream&quot;:false\n}&#039;\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>\u8f93\u51fa JSON\uff0c\u5305\u62ec\u6a21\u578b\u751f\u6210\u5185\u5bb9<\/li>\n\n\n\n<li>\u53ef\u914d\u5408 Python\u3001Node.js \u7b49\u811a\u672c\u8c03\u7528<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>\u4e94\u3001\u5e38\u7528\u7ec4\u5408\u5efa\u8bae<\/strong><\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u573a\u666f<\/th><th>\u73af\u5883\u53d8\u91cf\u7ec4\u5408<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>CPU \u5c0f\u6a21\u578b\u8c03\u8bd5<\/td><td><code>OLLAMA_NUM_THREADS=8<\/code><\/td><td>\u5c0f\u5185\u5b58 \/ \u65e0 GPU<\/td><\/tr><tr><td>GPU \u63a8\u7406 7B \u6a21\u578b<\/td><td><code>OLLAMA_NUM_GPU=1<\/code> + <code>OLLAMA_GPU_LAYERS=24<\/code><\/td><td>\u5355\u5361 RTX 3090 \u8db3\u591f<\/td><\/tr><tr><td>\u591a\u7528\u6237 API \u670d\u52a1<\/td><td><code>OLLAMA_MAX_REQUESTS=500<\/code> + systemd<\/td><td>\u9650\u5236\u5e76\u53d1\uff0c\u907f\u514d OOM<\/td><\/tr><tr><td>\u65e5\u5fd7\u8c03\u8bd5<\/td><td><code>OLLAMA_LOG_LEVEL=debug<\/code> + <code>OLLAMA_LOG_FILE=\/var\/log\/ollama.log<\/code><\/td><td>\u6392\u67e5\u95ee\u9898<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Ollama Linux \u672c\u5730\u90e8\u7f72\u5efa\u8bae\u4f7f\u7528 <strong>\u73af\u5883\u53d8\u91cf\u7edf\u4e00\u914d\u7f6e<\/strong>\uff0c\u4fbf\u4e8e\u7ef4\u62a4\u4e0e\u8c03\u4f18<\/li>\n\n\n\n<li>\u5e38\u7528\u73af\u5883\u53d8\u91cf\u5305\u62ec <strong>\u6a21\u578b\u8def\u5f84\u3001\u7f13\u5b58\u8def\u5f84\u3001CPU\/GPU \u914d\u7f6e\u3001\u65e5\u5fd7\u7ea7\u522b\u3001\u670d\u52a1\u7aef\u53e3<\/strong><\/li>\n\n\n\n<li>\u7ed3\u5408 <strong>systemd \/ \u540e\u53f0\u8fd0\u884c<\/strong> \u53ef\u957f\u671f\u7a33\u5b9a\u63d0\u4f9b REST API \u670d\u52a1<\/li>\n\n\n\n<li>\u6027\u80fd\u4f18\u5316\u91cd\u70b9\uff1a<strong>\u7ebf\u7a0b\u6570\u3001GPU \u5c42\u6570\u3001\u6a21\u578b\u5927\u5c0f\u3001\u4e0a\u4e0b\u6587\u7a97\u53e3<\/strong><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u6574\u7406\u4e00\u4efd \u300aLinux \u4e0a Ollama \u670d\u52a1\u914d\u7f6e\u53ca\u5e38\u7528\u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\u6307\u5357&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/09\/linux%e4%b8%8a%e8%ae%be%e7%bd%aeollama%e6%9c%8d%e5%8a%a1%e9%85%8d%e7%bd%ae%e5%b8%b8%e7%94%a8%e7%8e%af%e5%a2%83%e5%8f%98%e9%87%8f\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-705","post","type-post","status-publish","format-standard","hentry","category-2"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=705"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/705\/revisions"}],"predecessor-version":[{"id":706,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/705\/revisions\/706"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}