6.0 KiB
sidebar_position
sidebar_position |
---|
1 |
Example
A PKGBUILD script is a simple shell script used by Arch-based Linux distributions (such as Snigdha OS) to define how a package is built from source and installed onto the system. Below is an example of a basic PKGBUILD script with explanations for each part.
Example of a Simple PKGBUILD Script:
# Maintainer: Your Name <youremail@example.com>
pkgname=mysoftware
pkgver=1.0
pkgrel=1
pkgdesc="A simple software example"
arch=('x86_64')
url="https://example.com/mysoftware"
license=('GPL')
depends=('glibc')
source=("https://example.com/mysoftware-${pkgver}.tar.gz")
sha256sums=('abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890')
build() {
cd "$srcdir/mysoftware-${pkgver}"
./configure --prefix=/usr
make
}
package() {
cd "$srcdir/mysoftware-${pkgver}"
make DESTDIR="$pkgdir" install
}
Explanation of the PKGBUILD Components
-
pkgname:
This is the name of the package you're creating. In this example, it’s set tomysoftware
. When the package is installed, it will be referred to by this name in the system.pkgname=mysoftware
-
pkgver:
This is the version of the software you're packaging. For example, here we usepkgver=1.0
, which refers to the first version of the software.pkgver=1.0
-
pkgrel:
This is the release number of the package. If you make changes to the PKGBUILD but don't change the software version (e.g., a patch or configuration change), you would increment this number. For this example, it's set topkgrel=1
.pkgrel=1
-
pkgdesc:
A short description of what the software does. In this case, we use"A simple software example"
, but you should provide a meaningful description relevant to the software.pkgdesc="A simple software example"
-
arch:
This field specifies which architectures the package is compatible with. For most modern systems, you’ll typically seearch=('x86_64')
, which supports 64-bit systems. Other options might includei686
for 32-bit systems.arch=('x86_64')
-
url:
This is the website of the software. It provides users with more information about the software, updates, and support. In the example, we use"https://example.com/mysoftware"
, but you should replace this with the actual URL of the project.url="https://example.com/mysoftware"
-
license:
The license under which the software is distributed. In this case, we use theGPL
license. You can check the software's documentation to confirm which license applies.license=('GPL')
-
depends:
This field specifies any dependencies required by the software in order to run. For example, this software depends onglibc
(the GNU C Library). These dependencies must be installed for the software to run correctly.depends=('glibc')
-
source:
Thesource
array defines where to download the source code for the package. In this example, it points to a tarball (tar.gz
) containing the source code ofmysoftware
at version1.0
. The source code will be fetched from this URL when you run themakepkg
command.source=("https://example.com/mysoftware-${pkgver}.tar.gz")
-
sha256sums:
This is a checksum (SHA256) used to verify the integrity of the downloaded source file. This ensures that the source code has not been tampered with. You can generate this checksum using thesha256sum
command.sha256sums=('abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890')
Build and Package Functions
The PKGBUILD
defines two functions: build()
and package()
. These functions contain the steps to compile and install the software.
-
build():
This function is responsible for compiling the software. Here, it does the following:- Changes the directory to the source code folder (
cd "$srcdir/mysoftware-${pkgver}"
). - Runs the
./configure
command, which prepares the build system, setting the installation prefix (--prefix=/usr
to install to/usr/bin
,/usr/lib
, etc.). - Runs
make
to compile the source code.
build() { cd "$srcdir/mysoftware-${pkgver}" ./configure --prefix=/usr make }
- Changes the directory to the source code folder (
-
package():
After the software is compiled, thepackage()
function installs it into a staging directory (referred to as$pkgdir
). This step essentially places the compiled software into the appropriate directories (/usr/bin
,/usr/lib
, etc.). It uses theDESTDIR="$pkgdir"
option to specify the installation destination.package() { cd "$srcdir/mysoftware-${pkgver}" make DESTDIR="$pkgdir" install }
Building and Installing the Package
Once you've created the PKGBUILD file, follow these steps to build and install the software:
-
Run makepkg:
In the same directory where the PKGBUILD is located, run the following command to build the package.makepkg
This will:
- Download the source code from the URL defined in the
source
array. - Verify the integrity of the source code using the checksum.
- Compile the software as per the
build()
function. - Package the compiled software into a
.pkg.tar.zst
file.
- Download the source code from the URL defined in the
-
Install the package:
After the package is built, usepacman
to install the package locally. Replacemysoftware-1.0-1.pkg.tar.zst
with the actual filename generated bymakepkg
.sudo pacman -U mysoftware-1.0-1.pkg.tar.zst
Conclusion
This example demonstrates the basic structure and components of a PKGBUILD file. By understanding and modifying these fields, you can create your own PKGBUILDs for custom software or modify existing ones to suit your needs. PKGBUILDs provide a powerful way to automate the process of building, installing, and distributing software on Arch-based systems like Snigdha OS.