Introduction to making arch linux software package

Linux China 2021-10-14 04:23:38

PKGBUILD  The document is for Arch Linux And its derivatives ( Such as Manjaro) How to build and create software packages .

If you've ever used it  ( namely Arch Linux User maintained  PKGBUILD  The repository ), You may even have met them .

however , How in the end from  PKGBUILD  To installable packages ? What happened between the two , How to make your own software into a software package ? You will learn about these in this article .

PKGBUILD Basic knowledge of

For those familiar with Bash Or others shell For people who , You may know ,PKGBUILD  Is a with some variables shell Script .

PKGBUILD  The file consists of variables and functions , All of these are used to define the package itself , And how to build it .

In order to learn from  PKGBUILD  Create a package in , Need to use  makepkg  Command line tools . In obtaining  PKGBUILD  After the document , You just need to include  PKGBUILD  Run in the directory of `makepkg', You can get an installable package .

In this tutorial , You will see the package I just made , It prints... At run time “Hello World!”.

Get ready

In order to continue this tutorial , You need to create several files .

First , You need to create one called  PKGBUILD  The file of , It will serve as the basis for building your software package “ formula ”.

Another file you need to do is a file called  The file of . I'll explain its purpose later .

You can also create these two files with one command :


You can use it.  ls  Command to check whether these files have been created .

Then you can start !

Set up your PKGBUILD file

I won't let you copy and paste the whole file , Instead, type each line with you , So you can better understand the purpose of each line . If you don't like this way of learning , I highly recommend    In Chinese, it means Arch Linux Articles on creating software packages .

This article does not introduce  PKGBUILD  Every option you can set in , It just introduces some common options , So that you can get started as soon as possible .

That's all , Open your text editor , Let's get straight to the point .


First of all  pkgname  Variable . This is what defines the package name during installation , It's also    How to track software packages .

This variable ( And some other variables ) The format is  variable=value, The variable name is on the left , The value of the variable is on the right , Separate... With an equal sign .

To set the name of the package , Please be there.  PKGBUILD  Enter the following :

  • To set a different package name , Replace... With the name of your package  hello-world.
  • This does not set the command used to run the program , This will be in the following  package()  Deal with... In section .


As the variable name itself says , It sets the version of your package ( namely  1.0.0). This is useful when users update their systems , Because setting a higher version will prompt the user to upgrade .

To set the version number , Please be there.  PKGBUILD  Enter the following ( After the previous line ):



This is related to  pkgver  Variable related , Usually you don't need to know . But and  pkgver  The variables are the same , If it's changed to a higher number , The user will be notified to upgrade .

It applies to anything that needs to be kept  pkgver  Without change , for example  PKGBUILD  Itself has changed . If you create a for a program you use  PKGBUILD( And want to keep the version of the package the same ), And you need to fix  PKGBUILD  A mistake in itself , It would be very useful .

To set this variable , Please be there.  PKGBUILD  Enter the following :


This variable should   Always   from  1  Start , Then move up again and again . When  pkgver  When it moves upward , This variable can ( It should be ) Reset to  1, because  pkgver  Itself will notify the user to upgrade .


This sets the description of the package , Used to help better identify the package .

To set it up , Just put the description in quotation marks :

pkgdesc="Hello world in your terminal!"


This variable sets what the package is compatible with  . If you don't understand what architecture is , It doesn't matter , Because in most cases , This variable is almost useless .

in any case ,makepkg  You still need to set it , So it knows that the software package is compatible with our system .

This variable supports setting multiple values , therefore  makepkg  Need a different syntax , As shown below .

To set it up , Please be there.  PKGBUILD  Enter the following :


If you want to set multiple values , You need to separate each value with spaces and quotes , like this .arch=(“x86_x64" "arm").


This lists all the packages that provide the functionality required by our packages . And  arch  equally , It can also contain multiple values , Therefore, you must use parenthesis Syntax .

Since our package has no dependencies , So we don't need to be in  PKGBUILD  Enter this field in . However , If our package has dependencies , We will use and  arch  The same grammar .


Here are packages that do not provide the required functionality, but additional functionality .

This is related to  depends  The grammar is the same .


This tells  pacman  Which packages will cause problems with our packages , Or act in a way we don't want .

Any package listed here will be uninstalled before our package is installed .

This is related to  depends  The grammar is the same .


This defines the... Used by your program  . If you need help selecting a license ,  Provides some information . If you don't know how to set it , Set it to  custom  It's fine too .

This is related to  arch  and  depends  The grammar is the same :



This is it.  makepkg  How to know what files to use to build our software package . It can contain a variety of different types of sources , Including local files and URL.

When adding local files , To enter relative to  PKGBUILD  The file path of the file , For example, the following directory layout :


If you want to be in our  PKGBUILD  It includes, You need to type in  src/  As its name .

When the input URL when , You just need to enter the complete URL, namely

Your package only needs  file , Because of it and  PKGBUILD  In the same directory , You just need to enter its name as  source  Value .

This variable is also used with  arch  and  depends  The same grammar :



This is used to verify  source  The file in has not been modified or downloaded incorrectly . Information on how to obtain this value can be found in    Find .

If you'd rather not set this ( Or you just don't need , For example, for local files ), You can have the  source  For each file in the variable, enter  SKIP



This is the last one , It is also the most important part of actually making our package . In dealing with this problem , It's important to know two variables .

  • ${srcdir}: This is a  makepkg  place  source  The location of the file in the variable . In this directory , You can interact with these files , And make any other necessary modifications to the document .
  • ${pkgdir}: This is where we place the files that will be installed on the system . ${pkgdir}  The folder structure of is set according to the actual situation in the system ( for example , Use  pacman  When installing ,${pkgdir}/usr/bin/hello-world  Will create files  /usr/bin/hello-world).

package()  Contains a list of commands for creating software packages .

therefore , If ( hypothesis ) You need someone in  /usr/share/motto.txt  Write “Linux is superior to Windows ” The file of , You can run things like this :

package() {
mkdir -p "${pkgdir}/usr/share"
echo "Linux is superior to Windows" | tee "${pkgdir}/usr/share/motto.txt"

Some instructions about the above command :

  • ${pkgdir}  It was originally   It doesn't contain   The directory . If you skip  ,tee  An error will be output , Say this directory doesn't exist .
  • When specifying a directory , Always   Add... Before them  ${pkgdir}  or  ${srcdir}  Variable . If input  /usr/share/motto.txt, It will literally point to... In the system you are currently running  /usr/share/motto.txt.

For your  PKGBUILD, You're going to  The file is placed on the target system  /usr/bin/hello-world  in . You will also make the file say at run time “Hello to you!”.

Do that , Please be there.  PKGBUILD  Enter the following :

package() {
echo 'Hello to you!' > "${srcdir}/"
mkdir -p "${pkgdir}/usr/bin"
cp "${srcdir}/" "${pkgdir}/usr/bin/hello-world"
chmod +x "${pkgdir}/usr/bin/hello-world"

And then it's done ! use  makepkg -si  Build and install packages , Then run at the terminal  hello-world, View its output .


That's it , You've made your first  PKGBUILD! You walk for yourself and even for AUR On the way to making the actual software package .

What's the problem? , Or is there something wrong ? Please feel free to post in the comments section below .


author :  Topic selection :  translator :  proofreading :

This paper is written by    Original compilation ,  Honor roll out

Please bring the original link to reprint ,thank
Similar articles