Error while installing libsodium in PHP 7.1

This did not work for me (Ubuntu 16.04) (to get $ pecl install -f libsodium working):

$ apt install libsodium libsodium-dev

Here I got the following error:

Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package libsodium

What worked instead is downloading libsodium manually and compiling it:

Download libsodium source and unpack

$ wget https://download.libsodium.org/libsodium/releases/LATEST.tar.gz
$ tar -xzf LATEST.tar.gz

Compile libsodium

$ cd libsodium-stable/
$ ./configure
$ make && make check
$ make install

See: https://libsodium.gitbook.io/doc/installation

Afterwards the libsodium installation via PECL worked:

$ pecl install -f libsodium

Troubleshooting:

$ pecl install -f libsodium is still not working

Check if PECL is installed:

$ pecl version
PEAR Version: ...
PHP Version: ...
Zend Engine Version: ...
Running on: ...

If you get an error instead you have to install PECL first:

$ apt install php-pear

Check if phpize is available (to compile libsodium for PHP):

$ phpize --version
Configuring for:
PHP Api Version:         20160303
Zend Module Api No:      20160303
Zend Extension Api No:   320160303

If you get an error instead you have to install php7.1-dev:

$ apt install php7.1-dev

How to activate sodium in PHP?

You only have to create a file sodium.ini in your PHP extension directory (probably in /etc/php/7.1/mods-available/)

Content:

extension=sodium.so

You can activate the module via:

$ phpenmod -v 7.1 sodium

(Deactivation can be done via phpdismod -v 7.1 sodium.)

phpinfo should list the module now:

$ php -i | grep sodium
/etc/php/7.1/cli/conf.d/20-sodium.ini,
sodium
sodium support => enabled
sodium compiled version => 2.0.20
libsodium headers version => 1.0.18
libsodium library version => 1.0.18

Changing my comment to an answer since it helped many people:

I needed to run sudo apt install libsodium-dev and then sudo pecl install libsodium, and then it told me 'You should add "extension=sodium.so" to php.ini'.


@VenkateshLB You have to install the libsodium provided by your linux distribution first.

CentOS 7:

yum install libsodium libsodium-devel

Ubuntu 18.04:

apt install libsodium libsodium-dev

When installed, execute the PECL command.