Simple Nextcloud Installation on Raspberry Pi

This article explains how to install Nextcloud on the Raspberry Pi. It has been tested with the latest version of Nextcloud (17.0.1 at the time of writing), but should work for future versions too.  It has also been successfully tested on the latest Pi hardware, the Raspberry Pi 4, and on earlier Pi versions, and on the latest Pi OS, Raspbian 10 “Buster”.  Article last updated 14/1/2020.

Nextcloud is an open source software package providing remote file sharing services, similar to Dropbox. But with Nextcloud, you retain ownership, security and control of the shared data. Nextcloud works well on a Pi 2, Pi 3 and especially a Pi 4 but will run very slowly on a Pi 1.

Note: This is a manual, step-by-step procedure. If you would rather do the installation automatically, please see my recent article Automatic Nextcloud Installation on Raspberry Pi, which explains how to install Nextcloud with 3 commands. It is the quickest and easiest way to get Nextcloud running. Both procedures achieve the same overall result, however.

My Raspberry Pi 4 was purchased from The Pi Hut in July 2019. It was installed with Raspbian 10 “Buster” by applying the image “2019-09-26-raspbian-buster-lite.zip” downloaded from the raspberrypi.org downloads page.  Raspbian Buster was launched in June 2019, to supercede the earlier Raspbian “Stretch” (Debian 9). The following procedure works on both versions. Where there are differences, instructions applying to the both versions are provided.

Note:  If you are running Raspian 10 “Buster”, this procedure installs Nextcloud 17 or later.  If you are using Raspbian 9 “Stretch”, Nextcloud 15 is installed.  This is for reasons of stability and PHP compatibility.

Install Apache Web Server

Log into your Pi, either directly or through ssh from another system. I am using ssh. Update the software sources as follows.

$ sudo apt-get update

Install the Apache web server:

$ sudo apt-get install apache2

When asked if you want to continue, type “y” for “yes”. Apache will be installed along with a number of other packages.

In a browser, surf to the IP address of your Raspberry Pi. If you see a page entitled “Apache 2 Debian Default Page”, then Apache is running correctly on the Raspberry Pi. For example, my Pi is using IP address 192.168.1.99. So I start a browser on my PC and surf to http://192.168.1.99/. Alternatively, if you are using the Pi’s graphical desktop, you could start a browser directly on the Pi, eg. Chromium or Netsurf.

Install PHP and Associated Packages

A few packages will now be installed. On Raspbian Stretch, install PHP 7. On Raspbian Buster, install PHP 7.3. If you are not sure what version of Raspbian you are using, check the contents of the file /etc/os-release. Here, I am using Buster:

$ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
...

The version of Raspbian is clearly shown on the first line of the file.

Install PHP and Associated Packages

Issue the following command to install PHP and a number of other packages. We will use the “-y” switch this time, which answers the “yes/no” question automatically.

$ sudo apt-get -y install php php-gd sqlite php-sqlite3 php-curl php-zip php-xml php-mbstring libapache2-mod-php

The software will be installed.  If you are on Buster, installed packages will be PHP 7.3 versions.  Otherwise, if you are running Stretch, it will be packages at the PHP 7.0 level.

Restart Apache

Restart the web server now with:

$ sudo service apache2 restart

The command restarts Apache, but does not print out any messages.

Download Nextcloud

Okay, the system is ready for Nextcloud to be installed. Download the software with one of the following two  commands.

For Buster, install Nextcloud 17:

$ wget https://download.nextcloud.com/server/releases/nextcloud-17.0.1.zip

Alternatively, for Stretch, install Nextcloud 15:

$ wget -O latest.zip https://download.nextcloud.com/server/releases/latest-15.zip

Unpack Nextcloud

Now move the Nextcloud package into place and unpack it:

$ sudo mv latest.zip /var/www/html
$ cd /var/www/html
$ sudo unzip -q latest.zip

If you are curious, you can check the version of Nextcloud as follows.

$ grep VersionString nextcloud/version.php
$OC_VersionString = '17.0.1';

At the time of writing, (14th January 2020), the latest version is 17.0.1. To check the latest version online, have a look at the Nextcloud download page.

Create the Data Directory

You must create a “data” folder for Nextcloud and set permissions. Proceed as follows.

$ sudo mkdir -p /var/nextcloud/data
$ sudo chown www-data:www-data /var/nextcloud/data
$ sudo chmod 750 /var/nextcloud/data

Check the new directory with ls. You should see output like this, although of course your date and time will be different:

$ ls -ld /var/nextcloud/data
drwxr-x--- 2 www-data www-data 4096 May 27 18:45 /var/nextcloud/data

Set Directory Owners

Next, set the correct ownerships on Nextcloud “config” and “apps” directories:

$ cd /var/www/html/nextcloud
$ sudo chown www-data:www-data config apps

Configure Nextcloud for SQLite or MySQL

Nextcloud can use two kinds of databases: SQLite or MySQL. For a simple installation, with only one or two users, I would recommend SQLite, especially if you are using a P 2 or a Pi 3. It is more light weight and uses fewer resources than MySQL, and is therefore a good choice on Raspberry Pi hardware. To use SQLite, skip straight to the section below entitled “Configure Nextcloud”.

On the other hand, if you want to use MySQL instead, proceed to Install and Configure MySQL Database.  MySQL is an appropriate choice for larger installations, if you plan to have more users, and particularly if you are using a Raspberry Pi 4.

Install and Configure MySQL Database

In order to use the MySQL database (also known as MariaDB), first install a few extra packages.  This is for Raspbian Buster only:

$ sudo apt-get install -y mariadb-server python-mysqldb php-mysql

Alternatively, if you are using the older Raspbian 9 (Stretch), use this command:

$ sudo apt-get install -y mysql-server python-mysqldb php7.0-mysql

Now set the MySQL root password and secure the database installation. Run the script /usr/bin/mysql_secure_installation. It will first ask you to enter the “current password for root”. No password has been set yet, so just press return to log in. Next, it asks if you want to set a root password. Answer “y“, choose a sensible password and make a note of it. Then answer “y” to all of the remaining questions, as below.

$ sudo /usr/bin/mysql_secure_installation
Enter current password for root (enter for none): 
OK, successfully used password, moving on...
Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y
...
Thanks for using MariaDB!

Next, create a database for Nextcloud, and a database user. In the example I have called the Nextcloud user “ncuser” with a password of “raindrop”. For your own installation, choose a different user name and password. Don’t forget to take a note of both. Note that when running the following command, you will be asked to enter the root password that was set just above.

$ sudo mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.1.26-MariaDB-0+deb9u1 Debian 9.1

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database nextcloud;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> create user ncuser;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> set password for ncuser = password("raindrop");
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant all PRIVILEGES on nextcloud.* to ncuser@localhost identified by 'raindrop';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit;
Bye

Restart Apache

Restart the Apache web server once more with:

$ sudo service apache2 restart

The command restarts Apache, but does not print out any messages.


Configure Nextcloud

It is time to configure Nextcloud, either to use SQLite as a database, or, if you performed the commands in the last section, MySQL. Either way, proceed as follows.

In a browser, surf to your new Nextcloud web page. Use the URL:
http://your Pis IP address/nextcloud

For example, the address of my Pi is 192.168.1.99. So I go the the URL: http://192.168.1.99/nextcloud

You should see a mostly dark blue login page. In the middle is a “Performance Warning” about SQLite. Ignore that. Near the top it says “Create an admin account”. Think of a user name and password and type them into the boxes provided. Then click on the “Storage and Database” drop-down. In the “data folder” box which appears, delete what is already there and replace it with “/var/nextcloud/data

If you want to use SQLite as the database, then click on the “Finish Setup” button near the bottom of the page. Wait for a couple of minutes while Nextcloud completes the installation. During this time, Nextcloud creates more files and directories in our data directory, and it puts a SQLite database in there too.

If you want to use MySQL as the database, then where it says “Configure the database”, click on the button marked “MySQL/MariaDB”. Four new fields appear. Complete them as follows.
– for the database user, enter the name of the MySQL user you created above (“ncuser” in the example).
– for the database password, enter the accompanying password (“raindrop” in the example).
– for the database name, enter “nextcloud”
– leave “localhost” as “localhost”.

– then click on the “Finish Setup” button near the bottom of the page. Wait for a couple of minutes while Nextcloud completes the installation. During this time, Nextcloud creates more files and directories in our data directory, and it populates the MySQL database.

Completing the Configuration

Hey Presto! After a delay of 2 minutes or so, you should see the Nextcloud intro page with the slogan “a safe home for all your data”. Click the cross at the top right of the dialogue to dismiss it. You should now be looking at the main Nextcloud “Files” page. There you can see a couple of folders, a Nextcloud MP4 video file, and a PDF Manual.

Congratulations!  You have successfully installed Nextcloud!


Storing Nextcloud Data on a Separate Disk (Optional)

If you have a USB disk or thumb drive, you can use it to store the Nextcloud Data. In doing so, you might obtain more capacity for your data and/or improved performance.

First, mount your disk at a suitable location on the pi. Then proceed as follows. In this example, a thumb drive is mounted at “/disk1”. Move the Nextcloud data folder onto the drive as follows.

$ sudo mv /var/nextcloud/data /disk1/data
$ sudo ln -s /disk1/data /var/nextcloud/data

The first command moves the data. The second creates a link from the original data location to the new one. Finally, if you have Nextcloud open in a browser, refresh the page. It should reappear after a few moments. Your data has now been moved onto the disk/thumb drive. Try dragging a large file into Nextcloud – you should see the disk access light flash as the data is written.

Securing the Site with TLS/HTTPS

If you want to secure your Nextcloud installation by converting it from HTTP to HTTPS, see my later article: How to Convert a Website from HTTP to HTTPS.

Conclusion

That is the end of the procedure for installing Nextcloud. Your Nextcloud installation should now be fully working. I hope this guide was easy to follow and not too long or fiddly.

Note: Prior to January 2018, this article included a section on moving the Nextcloud data directory for security purposes. It is no longer necessary. Alterations to the procedure above mean that the data directory is created at a secure location and does not need to be moved.

Note: In the past, this article also included a section explaining how to increase the Nextcloud file size upload limit. This is no longer needed, as recent Nextcloud versions no longer have the old file size limitation.

Appendix – A Note on Nextcloud vs Owncloud

Nextcloud was forked from ownCloud in June 2016. Since then, Nextcloud has increased in popularity and has become, for many users, the natural successor to ownCloud. Previously I wrote an article describing how to install and upgrade Owncloud. Now, I would recommend Nextcloud over ownCloud, except for large corporate users who might already have commercial agreements with ownCloud Inc. If you came here from the ownCloud article, thank you for your patience.

END.

150 thoughts on “Simple Nextcloud Installation on Raspberry Pi

  1. Thanks for your superb tutorial!

    Everything went great except at the end. The server is running well except that it doesn’t let me upload files.
    Everytime I try to upload one it just displays a ‘Forbidden’ message.
    Do you know where that could come from?

    Thanks for your help!
    Leo

    • Hi Leo

      It sounds like it might be a permissions problem. Did you set the permissions properly on the “data” directory? What was the full error message that you saw ?

      Jim.

  2. Hi, i appreciate your guide, this was the first one that helped with setting up MySQL DB on a Raspberry Pi.

    I am having issues though, with the permissions you’ve given to apps and config folder using “sudo chown www-data:www-data config apps” – The inbuilt browser based updater fails. I used “sudo chown -R www-data:www-data /var/www/html/nextcloud/” to overcome this issue. It worked perfectly.

    However after moving my data folder to external drive, it is not working. There isn’t an HTTP error code but an error page stating internal server error.

    I’ve pasted the error here – https://pastebin.com/RnbTiSQZ

    Could you kindly help me out?

    • I tried with “sudo chown -R www-data:www-data /var/www/html/nextcloud/”

      Any particular reason (security?) that you’re giving permission only to two directories instead of entire /nextcloud* folder and subfolders?

      • Hi Parth. Only the “apps” and “config” folder need those permissions in order for Nextcloud to work properly (with the possible exception of the inbuilt updater, as mentioned in your last comment). My own Nextcloud installation is running just fine with those permissions. Meanwhile, in the interests of security, it is generally important not to loosen permissions more then necessary, especially on an internet-facing system, like a Nextcloud server. Cheers, Jim.

    • Hi Parth and sorry for the lateness of this reply. The procedure is not designed to support the inbuilt browser updater. If you need to upgrade Nextcloud, I would recommend saving all of your files, then doing a fresh install of the latest version of Nextcloud, then putting the files back.

      You made the inbuilt updater work by changing the file permissions. In that case, I would recommend changing the file permissions back immediately after the upgrade.

      The last point you mention is that Nextcloud fails after the data was moved. I am not sure how the data move was done, but judging from the error message, Nextcloud is looking for files (eg. its log file) and not finding them where it expected. It is important that great case it taken when moving Nextcloud data files. At this stage, I would recommend saving off all of your data files, doing a fresh Nextcloud install, and then putting the files back.

  3. I’ve come back to my RPi to install some updates (which took a long time), and realised that it has been running in the background for almost a year, in daily use by some other computers, with no attention required whatsoever. Thanks again Jim.

    • Cheers Greg, glad to hear it is still running. You might be interested to install Nextcloud 16, which has recently come out.

      Jim.

  4. Thanks so much for the instruction. works perfectly. I am new to everything and I have been trying to install nextcloud to the dusty Raspberry Pi3 from my husband, I’ve followed 3 or 4 different instructions closely and I could’t get nextcloud up and running, so packages versions is so confusing and most software update so much and so quick and make so instructions outdated. All told just thank you for putting up this instruction.

    • Hi Sue, sorry about the lateness of this reply which is due to illness. The software packages required for Nextcloud can indeed be confusing, especially with different versions of Raspbian. Glad to hear you got it all working.

      Cheers,
      Jim

  5. Hi,
    Thanks a lot for this tutorial!

    I am stuck on the first installation web page where i have to configure the admin user and db settings. For some reason im not aware of the page is always waitting to the instalation process ends, but it does not ends. Ive checked and seems we have tables created on mysql and the data directory has two files. But the files are empty.

    Ive installed on a fresh raspbian image on pi 4. Two times now and the result is the same. Also tried to configure manually or automatically with your ansible script but with same result.

    Help please!

    • Hi Reis, sorry for the lateness of this reply which was due to illness.

      You have followed the instructions, and at the last stage (the section entitled “Configure Nextcloud” above), you click the “Finish Setup” button and nothing happens? Is that correct?

      It should finish configuring in about 2 minutes or less. I am not sure what has happened in your case. Did you select SQLite or MySQL as the database ?

      Cheers,
      Jim

  6. Hi,

    I’ve followed you method on a raspberry Pi 4 and it works great.
    Thanks for this description step by step.
    It helped me a lot and i understand now better some details with mysql and the mounting of an external disk.
    I’ve got just one notice about the step to give all the rights to the database’s owner. It was wrong for me. The message told me that this command line couldn’t find the user even it was in the table.
    So after a little search on the web it was due by the length of my password.
    After i’ve reduce it, everything works fine.

    Thanks again for this really clear procedure.

    • Hi Sébastien, glad it worked for you, and that you were able to find and fix a bug to do with password length. I haven’t seen that situation myself.

      Cheers again,
      Jim.

  7. Nextcloud works with Raspberry pi 4 on the sd card, but I have a problem with transferring data to a USB drive. After executing the commands from the description, the files and shortcuts appear in the appropriate directories. Refreshing the application in the browser returns an error – Internal Server Error. The server encountered an internal error and was unable to complete your request.

    Sorry for my bad English 🙂

    • Hi dariuss. Your English is good. As I understand it, you are following the section of the article entitled “Storing Nextcloud Data on a Separate Disk”. You execute the two commands shown (mv and ln). But when you refresh the web page, it gives a message about a server error (“Internal Server Error. The server encountered an internal error and was unable to complete your request”).

      This seems to indicate that the data was not moved properly. One of the two commands (mv or ln) probably did not work properly. Please check and try again.

      Cheers,
      Jim.

  8. Hi, thanks for a great article. Excellent, really. Successful in installing nextcloud but when I went to the last step to “move” the data over to my attached usb disk, i get this error and the web page no longer displays – it did work before this last change but now it says “internal server error”.
    I did this:
    1. sudo mount /dev/sda2 /mnt/mydisk
    2. ls /mnt/mydisk – displays directory structure on usb disk
    3. sudo mv /var/nextcloud/data /mnt/mydisk/data
    4. sudo ln -s /mnt/mydisk/data /var/nextcloud/data
    ln: failed to create symbolic link ‘/var/nextcloud/data’:
    File exists
    I think I know how I messed up. Mistakenly I typed in the wrong command – mis-named my file name structure before performing the last step.
    Right before step 4, I mistakenly typed this (note syntax errors) sudo ln -s /disk1/data /var/nextcloud/data
    That command was accepted w/o error, so I believe I may need to “unlink” before I can get the correct command to be accepted. Make sense – I hope?
    Thanks very much –
    Bill

    • Hi Bill, cheers. It sounds like a link was accidentally created. The second “ln” (the correct one) fails because there is already a link there, and it can’t create one link on top of another.

      Remove the erroneous link with “rm /var/nextcloud/data”, then repeat your step 4 – it should work this time.

      Cheers,
      Jim.

      • Yep, that’s what I was thinking – just didn’t know the remove command. Still Learning. Hey, thank you very much for taking the time to support this.
        Cheers,
        Bill

      • Had to get to work this morning, but I followed your instructions just now, and of course, it works – or at least after removing the link and rerunning my #4, – no errors this time. I can connect to the web page again, although it complains about permissions – see below.
        I thought I maybe read someone’s post last night where he had this same error, but running out the door to bible study. Thanks a million. I’ll try to figure it out later…or if you know, by all means, please share. Thank you very much – again.

        “Error
        Your data directory is readable by other users
        Please change the permissions to 0770 so that the directory cannot be listed by other users.”

        Regards,
        Bill

        • Hi Bill, the directory permissions should have been set when you executed the command “sudo chmod 750 /var/nextcloud/data”, earlier in the procedure, under the section entitled “Create the Data Directory”. Did that get missed out?

          • Hi, I went back and looked in my terminal and I see I did/have issued these commands: sudo mkdir -p /var/nextcloud/data

            sudo chown www-data:www-data /var/nextcloud/data

            sudo chmod 750 /var/nextcloud/data

            ls -ld /var/nextcloud/data
            drwxr-x— 2 www-data www-data 4096 Jan 26 19:28 /var/nextcloud/data

            Thank you again – appreciate it.
            Bill

          • Hi Bill, the permissions on your data directory are 0750, but Nextcloud is giving an error message saying that they are 0770, which is incorrect. I am not sure why this would be. Without actually being there, it is difficult to say why this error message is appearing.

            Cheers, Jim

  9. Hi Jim,
    Thanks for writing this great article. It is very clear and well written. I’m a newbie with Linux… actually I used it in when I went to engineering school but that was a lifetime ago. I’m also newbie with raspberry pie and I didn’t even know these existed until last month. The only reason I discovered raspberry pie was because I’m nearly out of storage space on my various cloud accounts (one drive = 5GB, Samsung cloud for phone = 15GB and I used up 10 GB already) so I started researching “personal cloud” and did a lot of reading on NAS devices like the Synology DiskStation, WD Ex2 Ultra, etc…) but the cost was higher than what I wanted to spend. Then I discovered Raspberry Pie, Owncloud and NextCloud and found out how much they cost and ended up here! I attempted other “how to” procedures but it never worked out until I tried yours! Now I have a cost-effective personal cloud.
    Unfortunately, I ran into the same problems other users have written about with moving the data to an attached USB. I get internal errors and the “please change the permissions to 0770 so that the directory cannot be listed by other users” but found a way to fix it! (Hint: umask)
    I still work as a design engineer so my mindset was to test the system and simulate various scenarios before I commit transferring my data to this new system. I would intentionally pull the plug on the RPi to simulate a power outage or I would intentionally pull the usb stick to simulate the kids and pets running around and knocking things over. I didn’t modify any configuration files to automount the storage drives so I know that I would have to re-mount the storage drives and then I ran into the problems. So, I did more research and learning into Linux commands and file structure etc. The solution that worked for me so far is to include umask when mounting the storage drive and making sure all the permissions were the same as before the “power outage”. This is what worked for me:
    sudo mount -o umask=027,uid=www-data,gid=www-data /dev/sda1 /disk1
    I noticed that after mounting the usb storage I wasn’t able to change permissions or ownership of the usb storage. The permissions and ownership are set during mounting and can’t be changed after it’s mounted. I still have more to learn with Linux commands and some more testing before I commit to data transfer but I wanted to share this with you and any others going thru the same.

    Thanks,
    Dan

    • Hi Dan. You have come across some permissions issues and solved them with a umask on the mount command. Thanks for coming back with this information, which will hopefully be useful to other readers.

      Permissions on mounted file systems is quite a large and involved subject in Linux. When one file system is mounted on another, file permissions can be affected by a number of variables. Sometimes these factors combine, which can lead to quite a complicated situation.

      Factors affecting the permissions on a mounted file system include: the file system type (eg. ext4, NTFS, vfat), permissions on the mount point, umask of writing processes, mount options, file ownerships and so on. People reading this article are likely to be using different kinds of file systems and hardware, mounted in different ways. Eg. one person might be using a thumb drive formatted as vfat, while another user is storing his data on a powered USB disk containing an EXT4 file system.

      It is therefore difficult to give a recipe that works for all, and the article does not address these issues in detail. (Incidentally, there are many Internet resources that do).

      One recommendation I would make, and might add to the procedure, is to use EXT4 on your external disk. EXT is native to Linux an the Pi, and using it for the auxiliary storage is likely to avoid many permissions complications.

      Cheers,
      Jim

  10. Hello:
    Thanks for posting such a nicely detailed article. I just got a Pi4 and will do the install on it. One suggestion, would you please make a note somewhere at the beginning of the article when the procedure was last updated? and the current versions of Raspian and Next cloud combinations are recommended?
    Because things move so fast in the Linux and nextcloud world with version releases, It would be nice to know how far one is from the base line configs or the baseline + updates you did, when you read this article ‘in the future’. May be helpful in tracking down reasons for certain types of errors.
    Thank you!
    J

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.