{"id":44423,"date":"2023-09-26T16:12:06","date_gmt":"2023-09-26T16:12:06","guid":{"rendered":"https:\/\/ubuntuhandbook.org\/?p=44423"},"modified":"2025-05-04T09:24:16","modified_gmt":"2025-05-04T09:24:16","slug":"how-to-install-clang-17-or-16-in-ubuntu-22-04-20-04","status":"publish","type":"post","link":"https:\/\/ubuntuhandbook.org\/index.php\/2023\/09\/how-to-install-clang-17-or-16-in-ubuntu-22-04-20-04\/","title":{"rendered":"Install Clang 20, 19, or old versions in Ubuntu 24.04 | 22.04"},"content":{"rendered":"<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/c-logo.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-thumbnail wp-image-44424\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/c-logo-250x250.webp\" alt=\"\" width=\"250\" height=\"250\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/c-logo-250x250.webp 250w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/c-logo-300x300.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/c-logo-600x600.webp 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/c-logo-768x768.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/c-logo.webp 1200w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/a><\/p>\n<p><b>This simple tutorial shows how to install the Clang compiler 20, 19, or the old version 18, 17 in current Ubuntu 24.04, Ubuntu 22.04, and Ubuntu 20.04 LTS and correct the deprecated repository settings.<\/b><\/p>\n<p>Ubuntu by default includes Clang package in its system repositories. But as time goes on, the Clang package in Ubuntu repository is getting old.<\/p>\n<p>For those who need the most recent Clang compiler (so far v20), it&#8217;s easy to install and keep it up-to-date from the developer team&#8217;s official repository.<\/p>\n<h3>Step 1: Download the Automatic installation script<\/h3>\n<p>The official <a href=\"https:\/\/apt.llvm.org\/\" target=\"_blank\" rel=\"noopener\">Clang repository<\/a>, so far supports Ubuntu 20.04, Ubuntu 22.04, Ubuntu 24.04, and Ubuntu 24.10. It has a script to make adding repository and installing Clang as easy as few Linux commands.<\/p>\n<p>1. First, press <b>Ctrl+Alt+T<\/b> on keyboard to open terminal. When terminal opens, run command to <strong>download the official installation script<\/strong>:<\/p>\n<pre>wget https:\/\/apt.llvm.org\/llvm.sh<\/pre>\n<p>You may also use the script in Debian stable, though you may need to install <code>wget<\/code> first.<br \/>\n<a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/wget-clang-script.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-48805\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/wget-clang-script-700x457.webp\" alt=\"\" width=\"610\" height=\"398\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/wget-clang-script-700x457.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/wget-clang-script-300x196.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/wget-clang-script.webp 706w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>2. After downloading the script, <strong>add executable permission<\/strong> by running command:<\/p>\n<pre>chmod u+x llvm.sh<\/pre>\n<h3>Step 2: Use the script to install Clang<\/h3>\n<p>The script automate the process of adding the official apt repository, updating package cache, and installing specific Clang version into your system.<\/p>\n<p>All this can be done by running a single command. For example, <strong>install Clang-20<\/strong>:<\/p>\n<pre>sudo .\/llvm.sh 20<\/pre>\n<p><i>Replace 20 with 19 for installing Clang-19, or even <code>18<\/code>, <code>17<\/code> for old versions.<\/i><\/p>\n<p><b>NOTE: Ubuntu 24.04 has Clang version from 14 &#8211; 19, Ubuntu 22.04 has Clang 11 ~ 15 in system repositories!! Don&#8217;t use the script to install Clang that&#8217;s already in your system repository.<\/b><\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/install-clang20.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-48806\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/install-clang20-700x517.webp\" alt=\"\" width=\"610\" height=\"451\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/install-clang20-700x517.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/install-clang20-300x221.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/install-clang20.webp 722w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>During the process, it will ask to hit Enter to confirm adding the apt repository. Then, you may just wait until the process done.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/clang-apt-repo.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-44427\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/clang-apt-repo-600x407.webp\" alt=\"\" width=\"600\" height=\"407\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/clang-apt-repo-600x407.webp 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/clang-apt-repo-300x203.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/clang-apt-repo-768x521.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/clang-apt-repo.webp 786w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<h3>Step 3: Modify Clang repository<\/h3>\n<p>The command you run in step 2 will automatically download the key file and add Clang apt repository into your system.<\/p>\n<p>However, it still uses the old method that&#8217;s <b>deprecated<\/b> due to security reason! It&#8217;s recommended to do following steps to modify it to follow the new <a href=\"https:\/\/wiki.debian.org\/DebianRepository\/UseThirdParty#Sources.list_entry\" target=\"_blank\" rel=\"noopener\">Debian policy<\/a>.<\/p>\n<p><b>NOTE: If you installed multiple Clang versions from the commands above in Step 2, then you need to do this step multiple times.<\/b><\/p>\n<p><b>1.<\/b> First, open terminal (Ctrl+Alt+T) and run commands below one by one to first make sure <code>\/etc\/apt\/keyrings<\/code> directory exist, then move the key file from the deprecated <code>\/etc\/apt\/trusted.gpg.d\/<\/code> to that location:<\/p>\n<pre>sudo mkdir -p \/etc\/apt\/keyrings<\/pre>\n<pre>sudo mv \/etc\/apt\/trusted.gpg.d\/apt.llvm.org.asc \/etc\/apt\/keyrings\/<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/mv-clang-key.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-48808\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/mv-clang-key-700x457.webp\" alt=\"\" width=\"610\" height=\"398\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/mv-clang-key-700x457.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/mv-clang-key-300x196.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/mv-clang-key.webp 706w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><b>2.<\/b> Next, run command to edit the source file for the clang repository:<\/p>\n<pre>sudo nano \/etc\/apt\/sources.list.d\/archive_uri-http_apt_llvm_org_noble_*.list<\/pre>\n<p>When file opens in terminal window, add following in between &#8220;deb&#8221; and &#8220;http:\/\/apt.llvm.org\/&#8230;&#8221;:<\/p>\n<pre><b>[arch=amd64 signed-by=\/etc\/apt\/keyrings\/apt.llvm.org.asc]<\/b><\/pre>\n<p>Here:<\/p>\n<ul>\n<li><code>arch=amd64<\/code>, tells your OS type is <code>amd64<\/code> (Intel\/AMD). For non-Intel\/AMD platform, run <code>dpkg --print-architecture<\/code> command tell OS type, and replace <code>amd64<\/code> with the output. So far, <code>arm64<\/code> and <code>s390x<\/code> are also supported<\/li>\n<li><code>signed-by=\/path\/to\/key-file<\/code>, specify the key file.<\/li>\n<\/ul>\n<p>If you installed multiple Clang versions via the script, then you&#8217;ll have more lines in this file start with &#8220;deb&#8221;. Just add the &#8220;signed-by&#8221; section in each line and finally press Ctrl+S to save file, and Ctrl+X to exit.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/edit-clang.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-48809\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/edit-clang-700x499.webp\" alt=\"\" width=\"610\" height=\"435\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/edit-clang-700x499.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/edit-clang-300x214.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/edit-clang.webp 722w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><b>3<\/b>. Finally, run <code>sudo apt update<\/code> to apply change by refreshing package cache.<\/p>\n<p>And, you may manually install\/update the Clang packages by running command (replace number 20 according to which version you want to install):<\/p>\n<pre>sudo apt install clang-20 lldb-20 lld-20 clangd-20<\/pre>\n<p>Or, if you want to install everything, use the command below instead:<\/p>\n<pre>sudo apt install clang-tidy-20 clang-format-20 clang-tools-20 llvm-20-dev lld-20 lldb-20 llvm-20-tools libomp-20-dev libc++-20-dev libc++abi-20-dev libclang-common-20-dev libclang-20-dev libclang-cpp20-dev liblldb-20-dev libunwind-20-dev<\/pre>\n<h3>Step 4: Verify<\/h3>\n<p>If everything&#8217;s done successfully, just run <code>clang-xx --version<\/code> and\/or <code>locate clang-xx<\/code> to verify.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/clang-20-about.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-48807\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/clang-20-about-700x517.webp\" alt=\"\" width=\"610\" height=\"451\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/clang-20-about-700x517.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/clang-20-about-300x221.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/clang-20-about.webp 722w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<h3>Uninstall<\/h3>\n<p>To remove the repository added by the script, just open terminal (Ctrl+Alt+T) and run command to remove the corresponding source file:<\/p>\n<pre>sudo rm \/etc\/apt\/sources.list.d\/archive_uri-http_apt_llvm_org_*.list<\/pre>\n<p>And, remove the repository key file via command:<\/p>\n<pre>sudo rm \/etc\/apt\/trusted.gpg.d\/apt.llvm.org.asc<\/pre>\n<p>Or, launch &#8220;Software &amp; Updates&#8221; and remove source line and key from &#8220;Other Software&#8221; and &#8220;Authentication&#8221; tabs.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/remove-llvm-repo.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-44429\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/remove-llvm-repo-600x340.webp\" alt=\"\" width=\"600\" height=\"340\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/remove-llvm-repo-600x340.webp 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/remove-llvm-repo-300x170.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/remove-llvm-repo-768x435.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/remove-llvm-repo-1320x747.webp 1320w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/09\/remove-llvm-repo.webp 1486w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>To remove Clang packages (replace 20 accordingly), just run command:<\/p>\n<pre>sudo apt remove --autoremove clang-20 lldb-20 lld-20 clangd-20<\/pre>\n<p>&nbsp;<\/p>","protected":false},"excerpt":{"rendered":"<p>This simple tutorial shows how to install the Clang compiler 20, 19, or the old version 18, 17 in current Ubuntu 24.04, Ubuntu 22.04, and Ubuntu 20.04 LTS and correct the deprecated repository settings. Ubuntu by default includes Clang package in its system repositories. But as time goes on, the Clang package in Ubuntu repository [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":44424,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[2086],"class_list":["post-44423","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","tag-programming"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/44423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/comments?post=44423"}],"version-history":[{"count":0,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/44423\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media\/44424"}],"wp:attachment":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media?parent=44423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/categories?post=44423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/tags?post=44423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}