How to Install SQLite 3 on Fedora 40 or 39

SQLite 3 is a lightweight, self-contained, and highly reliable database engine used widely in embedded systems, mobile applications, and standalone database implementations. Its simplicity and zero-configuration nature make it an ideal choice for developers who need a robust database without the overhead of a full-fledged database management system. SQLite 3 supports a rich set of features including transactions, subqueries, triggers, and various indexing methods, making it versatile enough for many use cases.

On Fedora 40 or 39, SQLite 3 can be easily installed via the Fedora AppStream, which provides a stable and well-integrated version. For those who require the latest version or prefer more control over the installation, an alternative method involves downloading and compiling the latest SQLite 3 binary from the source. This guide will walk you through both installation methods, ensuring that you have access to the version of SQLite 3 that best suits your needs.

Method 1: Install SQLite 3 via DNF

Update Fedora Linux Before SQLite 3 Installation

Ensure your Fedora Linux system is up-to-date before installing SQLite. Updating the system helps avoid conflicts or issues during the SQLite installation process.

To update your Fedora Linux system, open a terminal and execute the following command:

sudo dnf upgrade --refresh

This command ensures that your system’s packages are updated to their latest versions, while the –refresh flag forces a refresh of the repository metadata.

Install SQLite 3 via DNF Command

The recommended method for installing SQLite 3 on Fedora Linux is to use the default appstream provided by Fedora’s repository. The appstream contains the latest stable version of SQLite 3, which has been tested for compatibility with Fedora.

To install SQLite 3, run the following command in your terminal:

sudo dnf install sqlite3

This command instructs the package manager (DNF) to install SQLite 3 from Fedora’s repository. Once the installation is complete, verifying the installed version of SQLite 3 is a good practice to ensure you have the correct version.

To check the version of SQLite 3, execute the following command:

sqlite3 --version

This command displays the installed SQLite 3 version, which should match the latest stable release in Fedora’s repository.

Method 2: Install SQLite 3 via source

Download the Latest SQLite 3 Archive

To obtain the latest or preferred version of SQLite 3, you may compile it from the source. First, visit the SQLite Download page and identify the latest version. Then, use the wget command to download the appropriate archive.

wget https://www.sqlite.org/2023/sqlite-autoconf-{version}.tar.gz

Replace {version} with the actual version number. Always check the SQLite Download page for the most recent version.

For example:

wget https://www.sqlite.org/2023/sqlite-autoconf-3410200.tar.gz

Extract the SQLite Archive

Once the archive is downloaded, extract the files using the following command:

tar xvfz sqlite-autoconf-*.tar.gz

Navigate to the Extracted Directory and Configure Prefix

Change the directory to the extracted folder to begin the compilation process:

cd sqlite-autoconf-{replace with version}

Replace {version} with the actual version number.

Now, configure the compilation with the desired installation prefix:

./configure --prefix=/usr

The output should resemble the following:

configure: creating ./config.status
config.status: creating Makefile
config.status: creating sqlite3.pc
config.status: executing depfiles commands
config.status: executing libtool commands

Compile SQLite with the make Command

To start the build process, use the make command along with the -j flag to specify the number of cores you want to utilize for faster compilation:

make -j {number_of_cores}

Replace {number_of_cores} with the desired number of cores for your system.

To determine the number of cores on your system, run:

nproc

For example, if your machine has two cores, use make -j 2. If you have 12 cores, you could use make -j 6 to dedicate half of your cores to the process.

Install Compiled SQLite 3 Binary

After the build process is complete, install SQLite using the following command:

sudo make install

The installation process will display output indicating the progress. Once installed, verify the installation and the version number:

sqlite3 --version

Following these steps, you have successfully compiled and installed the latest or preferred version of SQLite 3 from the source.

Testing SQLite 3 Installation

After installing SQLite 3, it’s essential to test its functionality to ensure it works as expected. This section provides step-by-step instructions to test SQLite 3 by creating a sample database, adding a table, inserting data, and querying the data.

Create a Sample SQLite 3 Database

First, let’s create a sample database named testdb using the following command:

sqlite3 testdb.db

This command creates a new SQLite database file called testdb.db in your current working directory.

Create a Sample SQLite 3 Table

Now, let’s create a sample table named employees with the following columns: id, name, and position. Enter the SQLite shell by running:

sqlite3 testdb.db

Once inside the SQLite shell, execute the following SQL command to create the employees table:

CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT, position TEXT);

Press Enter to execute the command; you should see no output if the table is created successfully.

Insert Sample Data with SQLite 3

Next, insert sample data into the employees table using the following SQL commands:

INSERT INTO employees (name, position) VALUES ('John Doe', 'Manager');
INSERT INTO employees (name, position) VALUES ('Jane Smith', 'Developer');
INSERT INTO employees (name, position) VALUES ('Alice Johnson', 'Designer');

These commands insert three rows into the employees table with different names and positions.

Query the Data with SQLite 3

To confirm that the data has been successfully inserted, run a SELECT query to retrieve the information from the employees table:

SELECT * FROM employees;

You should see the following output, which displays the data stored in the employees table:

1|John Doe|Manager
2|Jane Smith|Developer
3|Alice Johnson|Designer

Exit the SQLite Shell with SQLite 3

Once you have finished testing, exit the SQLite shell by typing:

.exit

This command returns you to your regular terminal.

Configure SELinux for SQLite 3

SELinux (Security-Enhanced Linux) is a security module that enforces mandatory access control policies on Linux systems. If SELinux is enabled on your system, you may need to configure it to work with SQLite. This section provides step-by-step instructions on configuring SELinux for SQLite, ensuring proper access control and security.

Check the SELinux Status

Before making any changes, verifying whether SELinux is enabled on your system is essential. To check the status of SELinux, run the following command:

sestatus

If SELinux is disabled, you don’t need to perform any further actions. If it’s enabled, proceed to the next step.

Set the Appropriate SELinux Context for SQLite 3

SQLite stores its data in database files, usually with a .db extension. To allow SQLite to access these files, you must set the appropriate SELinux context for them. The httpd_sys_content_t context is typically used for web server content, so we’ll use that for our SQLite database file.

First, locate your SQLite database file. For this example, we’ll assume it’s located at /var/www/html/testdb.db. Replace the path with the actual location of your SQLite database file.

Next, run the following command to set the SELinux context for the database file:

sudo chcon -t httpd_sys_content_t /var/www/html/testdb.db

This command sets the SELinux context of the testdb.db file to httpd_sys_content_t, allowing SQLite to access the file under the web server context.

Verify the SELinux Context

To confirm that the context has been changed successfully, use the ls command with the -Z option:

ls -lZ /var/www/html/testdb.db

The output should show the updated SELinux context for the database file:

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/testdb.db

The httpd_sys_content_t context in the output indicates that the context has been set correctly.

Test SQLite 3 Access on Fedora

Now that you’ve configured SELinux for SQLite, it’s essential to test whether SQLite can access the database file. If you’re using SQLite with a web application, try accessing the application and performing some database-related tasks, such as creating a new entry, updating existing data, or deleting an entry.

If the application works as expected and can interact with the SQLite database without issues, the SELinux configuration for SQLite is successful.

Conclusion

By successfully installing SQLite 3 on Fedora, whether through the AppStream or by compiling the latest binary, you have equipped your system with a reliable and versatile database engine. The AppStream method provided a quick and stable installation, while compiling from the source offered the flexibility to access the most recent features. Regularly updating SQLite 3 and ensuring its seamless integration with your applications will help maintain optimal performance and compatibility. It’s important to monitor your setup, especially if you compiled from the source, to avoid potential issues and ensure that your system remains secure and efficient.

Leave a Comment