- How To Install php-sqlite3 on Ubuntu 22.04
- What is php-sqlite3
- Install php-sqlite3 Using apt-get
- Install php-sqlite3 Using apt
- Install php-sqlite3 Using aptitude
- How To Uninstall php-sqlite3 on Ubuntu 22.04
- Uninstall php-sqlite3 And Its Dependencies
- Remove php-sqlite3 Configurations and Data
- Remove php-sqlite3 configuration, data, and all of its dependencies
- References
- Summary
- Making php 8 use the latest version of sqlite3 on Ubuntu 20.04 LTS
- Get compile options#
- Download and compile latest sqlite#
- Replace shared library#
- Verify that everthing went smoothly#
- how to enable sqlite3 for php?
How To Install php-sqlite3 on Ubuntu 22.04
In this tutorial we learn how to install php-sqlite3 on Ubuntu 22.04.
What is php-sqlite3
This package provides a SQLite3 module for PHP.
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.
This package is a dependency package, which depends on Debian’s default PHP version (currently 8.1).
There are three ways to install php-sqlite3 on Ubuntu 22.04. We can use apt-get , apt and aptitude . In the following sections we will describe each method. You can choose one of them.
Install php-sqlite3 Using apt-get
Update apt database with apt-get using the following command.
After updating apt database, We can install php-sqlite3 using apt-get by running the following command:
sudo apt-get -y install php-sqlite3
Install php-sqlite3 Using apt
Update apt database with apt using the following command.
After updating apt database, We can install php-sqlite3 using apt by running the following command:
sudo apt -y install php-sqlite3
Install php-sqlite3 Using aptitude
If you want to follow this method, you might need to install aptitude first since aptitude is usually not installed by default on Ubuntu. Update apt database with aptitude using the following command.
After updating apt database, We can install php-sqlite3 using aptitude by running the following command:
sudo aptitude -y install php-sqlite3
How To Uninstall php-sqlite3 on Ubuntu 22.04
To uninstall only the php-sqlite3 package we can use the following command:
sudo apt-get remove php-sqlite3
Uninstall php-sqlite3 And Its Dependencies
To uninstall php-sqlite3 and its dependencies that are no longer needed by Ubuntu 22.04, we can use the command below:
sudo apt-get -y autoremove php-sqlite3
Remove php-sqlite3 Configurations and Data
To remove php-sqlite3 configuration and data from Ubuntu 22.04 we can use the following command:
sudo apt-get -y purge php-sqlite3
Remove php-sqlite3 configuration, data, and all of its dependencies
We can use the following command to remove php-sqlite3 configurations, data and all of its dependencies, we can use the following command:
sudo apt-get -y autoremove --purge php-sqlite3
References
Summary
In this tutorial we learn how to install php-sqlite3 package on Ubuntu 22.04 using different package management tools: apt, apt-get and aptitude.
Making php 8 use the latest version of sqlite3 on Ubuntu 20.04 LTS
When installing php 8.1 on your ubuntu machine running 20.04 LTS you’re limited, at the time of writing, to sqlite3 3.31.1 since it’s the version that comes with the distribution. We’re going to make php use the latest sqlite version( 3.37.0 ) by:
Get compile options#
First off, let’s find out what compilation options we have with our current version of sqlite by jumping into the sqlite3 command line interface and getting the options:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
sqlite> PRAGMA compile_options; # my output COMPILER=gcc-9.3.0 ENABLE_COLUMN_METADATA ENABLE_DBSTAT_VTAB ENABLE_FTS3 ENABLE_FTS3_PARENTHESIS ENABLE_FTS3_TOKENIZER ENABLE_FTS4 ENABLE_FTS5 ENABLE_JSON1 ENABLE_LOAD_EXTENSION ENABLE_PREUPDATE_HOOK ENABLE_RTREE ENABLE_SESSION ENABLE_STMTVTAB ENABLE_UNKNOWN_SQL_FUNCTION ENABLE_UNLOCK_NOTIFY ENABLE_UPDATE_DELETE_LIMIT HAVE_ISNAN LIKE_DOESNT_MATCH_BLOBS MAX_SCHEMA_RETRY=25 MAX_VARIABLE_NUMBER=250000 OMIT_LOOKASIDE SECURE_DELETE SOUNDEX THREADSAFE=1 USE_URI
Download and compile latest sqlite#
# download the latest amalgamation wget https://www.sqlite.org/2021/sqlite-amalgamation-3370000.zip # unzip it unzip sqlite-tools-linux-x86-3370000.zip # and cd into the folder cd sqlite-tools-linux-x86-3370000
Next, we’re going compile the shared sqlite3 library file. We’re going to use gcc to compile the file.
For the compilation command we’re gonna get the sqlite compile options we got before add the prefix: -DSQLITE_ to add the compile-time options alongside the rest of the command.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
# inside sqlite-tools-linux-x86-3370000/ gcc \ -Wl,-soname,libsqlite3.so.0 \ -DSQLITE_ENABLE_COLUMN_METADATA \ -DSQLITE_ENABLE_DBSTAT_VTAB \ -DSQLITE_ENABLE_FTS3 \ -DSQLITE_ENABLE_FTS3_PARENTHESIS \ -DSQLITE_ENABLE_FTS3_TOKENIZER \ -DSQLITE_ENABLE_FTS4 \ -DSQLITE_ENABLE_FTS5 \ -DSQLITE_ENABLE_JSON1 \ -DSQLITE_ENABLE_LOAD_EXTENSION \ -DSQLITE_ENABLE_PREUPDATE_HOOK \ -DSQLITE_ENABLE_RTREE \ -DSQLITE_ENABLE_SESSION \ -DSQLITE_ENABLE_STMTVTAB \ -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION \ -DSQLITE_ENABLE_UNLOCK_NOTIFY \ -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT \ -DSQLITE_HAVE_ISNAN \ -DSQLITE_LIKE_DOESNT_MATCH_BLOBS \ -DSQLITE_MAX_SCHEMA_RETRY=25 \ -DSQLITE_MAX_VARIABLE_NUMBER=250000 \ -DSQLITE_OMIT_LOOKASIDE \ -DSQLITE_SECURE_DELETE \ -DSQLITE_SOUNDEX \ -DSQLITE_THREADSAFE=1 \ -DSQLITE_USE_URI \ -shared \ -o libsqlite3.so \ -fPIC \ sqlite3.c
Replace shared library#
Now we should have the shared library file called libsqlite3.so . Let’s find where’s the file the php interpreter is using and then replace it with the one we just compiled.
First, we need the PID of the master process for the php-fpm:
ps -ef | grep php # output root 421802 1 0 14:34 ? 00:00:00 php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf) www-data 421813 421802 0 14:34 ? 00:00:00 php-fpm: pool www www-data 421814 421802 0 14:34 ? 00:00:00 php-fpm: pool www potato 424098 417313 0 18:39 pts/0 00:00:00 grep --color=auto php
The master process has PID of 421802 , let’s use this to see the shared libraries:
sudo lsof -p 421802 | grep sqlite # output php-fpm8. 421802 root mem REG 8,0 67848 262731 /usr/lib/php/20210902/sqlite3.so php-fpm8. 421802 root mem REG 8,0 1757464 1053945 /usr/lib/x86_64-linux-gnu/libsqlite3.so.0.8.6 php-fpm8. 421802 root mem REG 8,0 39176 262730 /usr/lib/php/20210902/pdo_sqlite.so
Our shared library is located in /usr/lib/x86_64-linux-gnu/libsqlite3.so.0.8.6 , let’s replace it with the one we compiled:
# stop php sudo systemctl stop php8.1-fpm.service # make a backup sudo mv /usr/lib/x86_64-linux-gnu/libsqlite3.so.0.8.6 /usr/lib/x86_64-linux-gnu/libsqlite3.so.0.8.6.old # overwrite the library sudo mv libsqlite3.so /usr/lib/x86_64-linux-gnu/libsqlite3.so.0.8.6 # restart php sudo systemctl restart php8.1-fpm.service
Verify that everthing went smoothly#
If everything went alright we should now have php using the latest version of sqlite3. We can quickly verify it with:
php --ri sqlite3 # output sqlite3 SQLite3 support => enabled SQLite Library => 3.37.0 Directive => Local Value => Master Value sqlite3.extension_dir => no value => no value sqlite3.defensive => On => On
You can also check the logs to verify if everything went smoothly:
sudo journalctl -u php8.1-fpm
how to enable sqlite3 for php?
This also added a PHP configuration file in /etc/php5/apache2/conf.d; it even restarted Apache, although for some reason I had to restart it again for the change to take effect.
Good answer but I needed to restart apache to get the module to show up (eg. sudo service apache2 restart )
For PHP7, alter the below for your version of PHP (7.0, 7.2, 7.4, etc) and run
sudo apt-get install php7.0-sqlite3
Edit: This answer is outdated, but can’t be removed because it’s accepted. Please see the solution from Stacey Richards for the correct answer.
sudo apt-get install php5-cli php5-dev make sudo apt-get install libsqlite3-0 libsqlite3-dev sudo apt-get install php5-sqlite3 sudo apt-get remove php5-sqlite3 cd ~ wget http://pecl.php.net/get/sqlite3-0.6.tgz tar -zxf sqlite3-0.6.tgz cd sqlite3-0.6/ sudo phpize sudo ./configure sudo make sudo make install sudo apache2ctl restart
The 2nd to last command should be sudo checkinstall (after running sudo apt-get install checkinstall . Why use an OS with a package manager if you’re not going to use it?
Best advice for this understandably annoying situation: meta.stackoverflow.com/a/276564/2778484 and meta.stackexchange.com/questions/78438/… (fixing the answer with an edit is obviously not possible since it’s beyond just a syntax error).
The accepted answer is not complete without the remainder of instructions (paraphrased below) from the forum thread linked to:
cd /etc/php5/conf.d cat > sqlite3.ini # configuration for php SQLite3 module extension=sqlite3.so ^D sudo /etc/init.d/apache2 restart
At least on Ubuntu 14.04, this step is not needed, as there already is a sqlite3.ini as /etc/php5/mods-available/sqlite3.ini
For Ubuntu 18.04 and PHP 7.2:
sudo apt install php-sqlite3
The SQLite3 PDO driver is named SQLite, not SQLite3, so you can do:
The Debian/Ubuntu way for php-7.2, php-7.3 & php-7.4 (e.g. the [234] part)
sudo apt install php7.[234]-sqlite sudo phpenmod sqlite3
Be sure to note that on Windows Subsystem for Linux version 1 (WSL1) the (file-)locking system for SQlite is broken.
one thing I want to add , before you try to install
apt-get install php5-sqlite
apt-get install php5-sqlite3
search the given package is available or not :-
php5-rrd - rrd module for PHP 5 php5-sasl - Cyrus SASL extension for PHP 5 php5-snmp - SNMP module for php5 **php5-sqlite - SQLite module for php5** php5-svn - PHP Bindings for the Subversion Revision control system php5-sybase - Sybase / MS SQL Server module for php5
Here you get an idea about whether your version support or not .. in my system I get php5-sqlite — SQLite module for php5 so I prefer to install
**apt-get install php5-sqlite**