[Ubuntu] 編譯或安裝軟體的流程,以 ImageMagick 為例

本篇文章更新時間:2022/12/21
如有資訊過時或語誤之處,歡迎使用 Contact 功能通知。
一介資男的 LINE 社群開站囉!歡迎入群聊聊~
如果本站內容對你有幫助,歡迎使用 BFX Pay 加密貨幣新台幣 贊助支持。


Ubuntu 的使用者應該對 aptapt-get 指令不陌生。通常如果主機上需要什麼軟體,找一找資料就可以發現原來使用 apt install 軟體名稱 就可以安裝到,直接就可以使用。

類似的套件管理工具其實從各個作業系統(brew, yum..)甚至到開發語言(composer, pip..)都有,目的為解決這些常用又或是相依性高的套件分散無集中管理的問題。如果遇到這類型需求,往這方向尋找工具解決問題可能 80% 以上機會有解!

本篇筆記就兩種大方向:

  1. 套件管理程式的辦法(apt
  2. 自行編譯(下載)軟體

差別主要就是:套件管理中找得到就用,找不到或不合適就自己編譯!

有時候作業系統中的套件管理程式打包的軟體版本並不符合當下需求,這時候就有必要自己手動來了。

以 ImageMagick 為例是因為處理 WordPress 主機時,這個工具也是 WordPress 執行處理影像時優先選擇使用的方法,很常會碰到!

本篇筆記設定作業系統為 Linux 的 Ubuntu,任何版本理論上幾乎都通用。如果當下使用的作業系統不同,套用本篇思路也是差不多的。

套件管理程式

如果這台主機剛開,預設條件下,都要先做一次套件更新:

apt update && apt upgrade -y && apt remove -y && apt autoremove -y && apt clean -y && apt autoclean -y

更新完後,要安裝本篇舉例的 ImageMagick 軟體前,先看一下有沒有這個軟體,指令: apt-cache madison imagemagick

# apt-cache madison imagemagick
imagemagick | 8:6.9.11.60+dfsg-1.3build2 | http://us.archive.ubuntu.com/ubuntu jammy/universe amd64 Packages
imagemagick | 8:6.9.11.60+dfsg-1.3build2 | http://us.archive.ubuntu.com/ubuntu jammy/universe Sources

注意,這邊查詢的大小寫有差,通常都是全小寫。

軟體名稱 imagemagick 後面的 8:6.9.11.60+dfsg-1.3build2 就是打包的套件資訊,可以知道就是指 ImageMagick_6.9.11.60 這個版本。

如果正是你需要的版本,直接輸入指令 apt install imagemagick -y 套件管理程式就會直接幫你安裝到好直接可以使用了!

自行編譯(下載)軟體

如果發現版本號不對,最直接的就是上軟體官網找「Download」下載對應版本號的打包,又或是找找看開發者這軟體的 GitHub Releases 頁面。

運氣好找到要的版本以及對應編譯好打包好你當前要使用的作業系統版本,就也是快樂的抓下來安裝使用,搞定~

不過,總有個不過,也就是運氣不怎麼好的時候,就要試著發揮一下實力的部分了XD

ImageMagick 這套軟體本身算是一個大家長,他整合了很多影像格式來處理,如果只是按照他官網上的 git clone repo 下載專案原始碼 -> configure 設定組態-> make 編譯 -> install 安裝 等流程,肯定會不如你預期可以正常影像轉檔。

所以如果要安裝好這套軟體的「最通用使用情境」版本的話,首先可以先查詢看看套件管理程式裡紀錄的「套件相依」:

apt-get -s build-dep imagemagick

如果碰到

You must put some 'deb-src' URIs in your sources.list

這個錯誤的話,通常編輯你的 /etc/apt/sources.list 這檔案,把每一個套件來源都補一條 deb-src 開頭後面都照抄的來源,就可以。有時候可以注意一下是否是被註解了,解開 # 井號註解就好。

如果沒問題會得到下面這樣類似的結果:

# apt-get -s build-dep imagemagick
Reading package lists... Done
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  autopoint chrpath cm-super-minimal debhelper debugedit dh-autoreconf dh-exec dh-strip-nondeterminism diffstat doxygen doxygen-latex dwz fontconfig
  fontconfig-config fonts-dejavu-core fonts-lmodern fonts-urw-base35 ghostscript gir1.2-freedesktop gir1.2-gdkpixbuf-2.0 gir1.2-harfbuzz-0.0
  gir1.2-pango-1.0 gir1.2-rsvg-2.0 graphviz gsfonts intltool-debian jdupes libaliased-perl libann0 libaom-dev libaom0 libapache-pom-java
  libapt-pkg-perl libarchive-zip-perl libavahi-client3 libavahi-common-data libavahi-common3 libb-hooks-endofscope-perl libb-hooks-op-check-perl
  libbrotli-dev libcairo-gobject2 libcairo-script-interpreter2 libcairo2 libcairo2-dev libcapture-tiny-perl libcdt5 libcgraph6 libclang-cpp12
  libclang1-12 libclass-data-inheritable-perl libclass-method-modifiers-perl libclass-xsaccessor-perl libclone-perl libcommons-logging-java
  libcommons-parent-java libconfig-tiny-perl libcpanel-json-xs-perl libcups2 libdata-dpath-perl libdata-messagepack-perl libdata-optlist-perl
  libdata-validate-domain-perl libdatrie-dev libdatrie1 libdav1d-dev libdav1d4 libde265-0 libde265-dev libdebhelper-perl libdeflate-dev libdeflate0
  libdevel-callchecker-perl libdevel-size-perl libdevel-stacktrace-perl libdjvulibre-dev libdjvulibre-text libdjvulibre21
  libdynaloader-functions-perl libemail-address-xs-perl libexception-class-perl libexif-dev libexif12 libexporter-tiny-perl libfftw3-bin libfftw3-dev
  libfftw3-double3 libfftw3-long3 libfftw3-quad3 libfftw3-single3 libfile-basedir-perl libfile-find-rule-perl libfile-stripnondeterminism-perl
  libfont-ttf-perl libfontbox-java libfontconfig-dev libfontconfig1 libfontconfig1-dev libfontenc1 libfreetype-dev libfreetype6-dev libfribidi-dev
  libgd3 libgdk-pixbuf-2.0-0 libgdk-pixbuf-2.0-dev libgdk-pixbuf2.0-bin libgdk-pixbuf2.0-common libgraphite2-3 libgraphite2-dev libgs9 libgs9-common
  libgts-0.7-5 libgvc6 libgvpr2 libharfbuzz-dev libharfbuzz-gobject0 libharfbuzz-icu0 libharfbuzz0b libheif-dev libheif1 libhtml-html5-entities-perl
  libice-dev libice6 libidn11 libijs-0.35 libilmbase-dev libilmbase25 libimport-into-perl libio-string-perl libipc-run3-perl
  libipc-system-simple-perl libiterator-perl libiterator-util-perl libjbig-dev libjbig0 libjbig2dec0 libjs-jquery libjson-maybexs-perl libkpathsea6
  liblab-gamut1 liblcms2-2 liblcms2-dev liblist-compare-perl liblist-moreutils-perl liblist-moreutils-xs-perl liblist-utilsby-perl libllvm12
  liblqr-1-0 liblqr-1-0-dev liblzma-dev libmarkdown2 libmime-charset-perl libmodule-implementation-perl libmodule-runtime-perl libmoo-perl
  libmoox-aliases-perl libmouse-perl libnamespace-clean-perl libnet-domain-tld-perl libnumber-compare-perl libopenexr-dev libopenexr25 libopenjp2-7
  libopenjp2-7-dev libpackage-stash-perl libpango-1.0-0 libpango1.0-dev libpangocairo-1.0-0 libpangoft2-1.0-0 libpangoxft-1.0-0 libpaper-utils
  libpaper1 libparams-classify-perl libparams-util-perl libpath-tiny-perl libpathplan4 libpdfbox-java libperl-dev libperlio-gzip-perl libpixman-1-0
  libpixman-1-dev libptexenc1 libpthread-stubs0-dev librole-tiny-perl librsvg2-2 librsvg2-bin librsvg2-common librsvg2-dev libsereal-decoder-perl
  libsereal-encoder-perl libsm-dev libsm6 libsombok3 libstrictures-perl libsub-exporter-perl libsub-exporter-progressive-perl libsub-identify-perl
  libsub-install-perl libsub-name-perl libsub-override-perl libsub-quote-perl libsynctex2 libteckit0 libtexlua53 libtexluajit2 libtext-glob-perl
  libtext-levenshteinxs-perl libtext-markdown-discount-perl libtext-xslate-perl libthai-data libthai-dev libthai0 libtiff-dev libtiff5 libtiffxx5
  libtime-duration-perl libtime-moment-perl libtimedate-perl libtry-tiny-perl libtype-tiny-perl libunicode-linebreak-perl libunicode-utf8-perl
  liburi-perl libvariable-magic-perl libwmf-dev libwmf0.2-7 libx11-dev libx265-192 libx265-dev libxapian30 libxau-dev libxaw7 libxcb-render0
  libxcb-render0-dev libxcb-shm0 libxcb-shm0-dev libxcb1-dev libxdmcp-dev libxext-dev libxft-dev libxft2 libxi6 libxml-libxml-perl
  libxml-namespacesupport-perl libxml-sax-base-perl libxml-sax-perl libxml2-utils libxmu6 libxpm4 libxrender-dev libxrender1 libxt-dev libxt6
  libyaml-libyaml-perl libzzip-0-13 lintian lmodern lzip lzop pango1.0-tools patchutils pkg-kde-tools po-debconf poppler-data preview-latex-style
  t1utils tex-common texlive-base texlive-binaries texlive-extra-utils texlive-font-utils texlive-fonts-recommended texlive-latex-base
  texlive-latex-extra texlive-latex-recommended texlive-luatex texlive-pictures texlive-plain-generic x11-common x11proto-dev x11proto-xext-dev
  xdg-utils xfonts-encodings xfonts-utils xorg-sgml-doctools xsltproc xtrans-dev

上面那一大串就是你接下來編譯 ImageMagick 這套軟體會需要提前安裝的,所以複製下來使用 apt install ....這堆套件名稱.... -y 安裝好。(這步驟可以每次裝好後再查詢一次看看,有時候還會看到有需要安裝的套件要補)

然後開始原本的流程(官方參考頁面):

下載原始碼: git clone https://github.com/ImageMagick/ImageMagick.git ImageMagick-7.1.0

然後:

$ cd ImageMagick-7.1.0
$ ./configure
$ make

make 這指令,如果你當前操作的主機 CPU 有 6 顆,可以使用 make -j 4 或是不知道要用幾顆 CPU ,就使用 make -j$(nproc --ignore=2) 這指令來自動換算 CPU 數,幫忙加速編譯。- 參考 GNU make: should the number of jobs equal the number of CPU cores in a system?

沒意外或是沒看到明顯提示錯誤的話,就可以 make install 完成安裝。

理論上是這樣就好,不過實際上我編譯完沒問題,但安裝的時候碰到了找不到 libMagickCore-7.Q16HDRI.so.6 not found 的問題,所以先下這兩個指令:

ldconfig /usr/local/lib
export LD_LIBRARY_PATH=/usr/local/lib && make install

把放在 /usr/local/lib 目錄的引用檔案給連結上後才安裝正確!

安裝完後可以同目錄下指令 make check 測試結果

============================================================================
Testsuite summary for ImageMagick 7.1.0-56
============================================================================
# TOTAL: 87
# PASS:  87
# SKIP:  0
# XFAIL: 0
# FAIL:  0
# XPASS: 0
# ERROR: 0
============================================================================

以及輸入 identify --version 查看版本資訊

# identify --version
Version: ImageMagick 7.1.0-56 beta Q16-HDRI x86_64 6edfae382:20221217 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP(4.5)
Delegates (built-in): bzlib djvu fontconfig freetype heic jbig jng jp2 jpeg lcms lqr lzma openexr pangocairo png tiff webp x xml zlib
Compiler: gcc (11.3)

如果對於支援的影像格式還覺得不足或是不需要這麼多,都可以在 ./configure 指令後補上下方預設輸出的選項 --with-XXX=yes 或關閉 --with-XXX=no

ImageMagick 7.1.0-56 is configured as follows. Please verify that this
configuration matches your expectations.

Host system type: x86_64-pc-linux-gnu
Build system type: x86_64-pc-linux-gnu

                  Option                        Value
------------------------------------------------------------------------------
Shared libraries  --enable-shared=yes       yes
Static libraries  --enable-static=yes       yes
Build utilities   --with-utilities=yes      yes
Module support    --with-modules=no     no
GNU ld            --with-gnu-ld=yes     yes
Quantum depth     --with-quantum-depth=16   16
High Dynamic Range Imagery
                  --enable-hdri=yes     yes

Install documentation:              yes

Memory allocation library:
  JEMalloc          --with-jemalloc=no      no
  TCMalloc          --with-tcmalloc=no      no
  UMem              --with-umem=no      no

Delegate library configuration:
  BZLIB             --with-bzlib=yes        yes
  Autotrace         --with-autotrace=no     no
  DJVU              --with-djvu=yes     yes
  DPS               --with-dps=no       no
  FFTW              --with-fftw=no      no
  FLIF              --with-flif=no      no
  FlashPIX          --with-fpx=no       no
  FontConfig        --with-fontconfig=yes   yes
  FreeType          --with-freetype=yes     yes
  Ghostscript lib   --with-gslib=no     no
  Graphviz          --with-gvc=yes      no
  HEIC              --with-heic=yes     yes
  JBIG              --with-jbig=yes     yes
  JPEG v1           --with-jpeg=yes     yes
  JPEG XL           --with-jxl=yes      no
  LCMS              --with-lcms=yes     yes
  LQR               --with-lqr=yes      yes
  LTDL              --with-ltdl=no      no
  LZMA              --with-lzma=yes     yes
  Magick++          --with-magick-plus-plus=yes yes
  OpenEXR           --with-openexr=yes      yes
  OpenJP2           --with-openjp2=yes      yes
  PANGO             --with-pango=yes        yes
  PERL              --with-perl=no      no
  PNG               --with-png=yes      yes
  RAQM              --with-raqm=yes     no
  RAW               --with-raw=yes      no
  RSVG              --with-rsvg=no      no
  TIFF              --with-tiff=yes     yes
  WEBP              --with-webp=yes     yes
  WMF               --with-wmf=no       no
  X11               --with-x=           yes
  XML               --with-xml=yes      yes
  ZIP               --with-zip=yes      no
  ZLIB              --with-zlib=yes     yes
  ZSTD              --with-zstd=yes     no

如果最後面 Value 顯示為 no 表示沒抓到系統內有對應函式庫,可以另外自己在作業系統裡安裝其他函式庫,然後再重新組態一次看看有沒有變。(但 --with-XXX=no 的意思是預設不啟用,所以要啟用的話要自己補上 --with-XXX=yes./configure 後面,以此類推其他函式庫)

參考:

  1. error while loading shared libraries: libMagickCore-7.Q16HDRI.so.6 & GitHub libMagickCore-7.Q16HDRI.so.6 not found #229
  2. How to install delegate libraries for Image Magick 7.0.7
  3. Error :: You must put some 'source' URIs in your sources.list

後記

其實這類型描述解決問題思路方法的中文文章我還真的不多見(國外資源常見在 Stack Overflow)。我想一方面每套軟體有自己設計的核心理念與方向,還包含對環境的要求,抓個大方向來描述也可能描述得過於籠統,乾脆大家都是直接紀錄自己當下可行方案,所以看起來就是會像某種制式答案,或是「好像只能這樣解決」的樣子。

解讀這樣的「學習方法」的確還是需要比較多案例和實戰經驗,試著自己從頭來過一次最準確,經過一段時間練習後,很常被自己學習過的經驗給救到。


Share:

作者: Chun

資訊愛好人士。主張「人人都該為了偷懶而進步」。期許自己成為斜槓到變進度條 100% 的年輕人。[///////////____36%_________]

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *


文章
Filter
Apply Filters
Mastodon