Elementary OS - Dev Environment Setup
Tags: linux, php, mysql
Published: 2021-11-20 22:13:26

Overview
--------

So every now and then i see something that catches my eye. Recently that seems to be Linux Distributions, over the last few months i have changed my distribution's a few times to try and find one i'm comfortable with. I moved from my iMac (getting a bit sluggish) to Ubuntu originally and it worked but i didn't "enjoy it", i then tried Manjaro, then Pop!\_OS and then finally thought id have a go with [Elementary OS](https://elementary.io/).

So far i am really enjoying it! it feels a bit "Mac'ish", Looks nice and feels quick!

But compared to Ubuntu its a little bit different with the Setup of the dev environment, so i thought id write up a little tutorial on setting a Laravel Dev Environment on Elementary OS.

### Let's get started!

So as with any tutorial i do with Linux, we start with an update/upgrade.

 ```
sudo apt update
sudo apt upgrade

```

If this is a fresh install you're setting up on, it could take few more mins than usual, Any messages you get for Diskspace just accept them.

 ```
After this operation, 36.4 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

```

### Install Git!

Git is something use a lot anyway so its worth installing it. works from the IDE and normal terminals when working so it just helps.

 ```
sudo apt install git

```

When the install is finished type `git --version` in you're terminal and you should see something like: **git version 2.25.1**

Boom! GIt is installed!

### Time to install ZSH

This is a personal preference for me, but i cant recommend the terminal application enough, it cleans up your terminal window to a nice clean output.

 ```
sudo apt install zsh

```

Now you can stick with ZSH in its basic form, or you can install [PowerLineLevel10k](https://github.com/romkatv/powerlevel10k) later on, which again id recommend as there is a shed load of options to use. but for the purpose of this tutorial... i'm just doing the basic.

### Install PHP and Relevant dependencies.

 ```
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install -y php8.0

```

And once that's done you can check the version by typing `php -v`

 ```
➜  ~ php -v                                        
PHP 8.0.10 (cli) (built: Aug 26 2021 15:50:07) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.10, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.10, Copyright (c), by Zend Technologies

```

### Laravel PHP Requirements

The Laravel framework requires some PHP Dependencies, these can be installed in a single command. The Dependencies are;

- OpenSSL
- Common
- BCMath
- Curl
- mbstring
- MySQL
- XML
- zip
- sqlite3

If you go to the Laravel website there will be a requirement for **php-json** also. But, with PHP 8 it comes with it, so there is no extra install to complete. This will also include some of the Valet Dependencies.

The command for all of these is as follows. (Type 'y' to the question about disk space)

 ```
sudo apt install openssl php8.0-cli php8.0-curl php8.0-mbstring php8.0-mcrypt php8.0-xml php8.0-zip php8.0-common php8.0-bcmath php8.0-mysql php8.0-sqlite3

```

### Install Composer

![](https://www.raspada-blog.co.uk/storage/wink/images/gbWtHrEpsQ8PRMk4dzo0Ilo5sYHPGvgFkp3UdIiR.jpg)

I have no idea why but on my windows machine i used to learn dev work on i always struggled to get composer working properly. On Linux and macOS it just seemed easier...

The following commands needs to be run in terminal, I tend to run them one at a time just in case, but as far as i'm aware you can run them all at the same time.

*Note: hash\_file command is subject to change, so it might be worth checking for the latest command for this* [*here*](https://getcomposer.org/download/)*.*

 ```
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

```

Now lets make it Globally accessible so you can just run `composer <Your Command>` Instead of `php composer.phar <your command>`.

You need to move the composer.phar file to a location that is part of your systems path. In my case running Elementary 6 its **/usr/local/bin/composer** The command for this is simply;

 ```
sudo mv composer.phar /usr/local/bin/composer

```

now if you type `composer --version` You should get your current composer version.

Now we need to add composer to our global path so ZSH can use it correctly as well. Open the ZSH config;

 ```
nano ~/.zshrc

```

Paste the following lines at the bottom of the file;

 ```
PATH=$HOME/.config/composer/vendor/bin:~/.composer/vendor/bin:$PATH
export PATH

```

Just above this command add a shortcut to a folder you will make in a second (if you want to). i add an alias to my **zshrc** file called **wd** and this will Change Directory to **~/www** add the the below command BEFORE the PATH export.

 ```
alias wd="cd ~/www"

```

`Ctrl + X` to exit and `Save modified buffer? y` and hit enter.

Now apply them changes by typing:

 ```
source ~/.zshrc

```

### Lets install Valet Linux

Valet is awesome, early in my Laravel journey i used homestead which is also very powerful but can be cumbersome. Average memory usage could be a gig or two whereas Valet..... 7mb..... Yep 7MB, you can park your domains so you can use ANY folder on your system you want for ease of use. I believe Valet was originally designed just for macOS but has since been ported to both Windows and Linux.

If you checkout the requirements for Valet-Linux [here](https://cpriego.github.io/valet-linux/requirements) you will see there are a number of Dependencies required. You'll also note that we have installed the **PHP Extensions** and **Optional Packages** already when we first installed PHP. we now just need to run the following command to grab the OS packages required for Linux Valet:

Network Manager tools for Valet

 ```
sudo apt-get install network-manager libnss3-tools jq xsel

```

one the prerequisites have installed it's time to time to composer require it from the Git repository:

 ```
composer global require cpriego/valet-linux

```

Once that has run you need to install the package like so:

 ```
valet install

```

Once you press enter to this it will start running through checks for the Valet installations. In my installation **nginx**, **php7.4-fpm**, **dnsmasq** and **inotify-tools** were not installed and Valet automatically pulled these down and installed for me.

Once its finished you should have received: `Valet Installed successfully!`

Time to create our websites folder. My preferred sites folder is **www**. so lets make that in the root:

 ```
mkdir ~/www

```

Now we have created our websites folder we need to tell Valet that's where our sites will be hosted locally, With windows and homestead you need to add the directories manually to the **Homestead.yaml** but with Valet it couldn't be any easier!

 ```
cd ~/www
valet park

```

﻿That's it, now when you add a site to that folder it will generate the necessary dns records and links so you can run something like: `raspada-blog.vm` And it will load the site.

### Install MySQL

We could actually just go ahead and install the Laravel package but nine times out of ten we will need a MySQL DB so lets get that done now.

 ```
sudo apt install mysql-server -y

```

Remember to just accept the installation message for MySQL.

Now the next bit is entirely up to you, however i will say that i select `Y` to everything in the next installation process. And when it gets to the Password Tool, select `0` as your complexity.

 ```
sudo mysql_secure_installation

```

If you're, installing on a server you want to add a higher level password for security reason... but locally, it really doesn't matter.

And that is it, MySQL is installed and you can check the installation with this command:

 ```
systemctl status mysql.service

```

And you should see the following output.

 ```
➜  ~ systemctl status mysql.service
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-09-12 15:08:29 UTC; 2h 24min ago
    Process: 800 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
   Main PID: 926 (mysqld)
     Status: "Server is operational"
      Tasks: 37 (limit: 18755)
     Memory: 418.1M
     CGroup: /system.slice/mysql.service
             └─926 /usr/sbin/mysqld

```

So were not using the root user in MySQL by default were going to create another user with all privileges, including grant privileges.

To open up MySQL Command line Type:

 ```
sudo mysql

```

Now you will be faced with the `mysql>` prompt now lets create a user.

 ```
CREATE USER 'raspadatest'@'localhost' IDENTIFIED BY 'ThisIsMyNewPassword12!';

```

That's the user created! now lets grant all privileges, do this by typing the following:

 ```
GRANT ALL PRIVILEGES ON *.* TO 'raspadatest'@'localhost'; 

```

That's our new user with rights to all tables we currently have but will also create in the future.

We need to flush privileges now:

 ```
FLUSH PRIVILEGES;

```

to test it we can exit out the MySQL prompt by typing:

 ```
mysql> exit;

```

And that's it. which ever client you decide to use, login with what ever you created above on `localhost` or `127.0.0.1` and you're good to go.

Now if you want to manage the DB's with a GUI instead of going through command line you can install MySQL **Workbench** or **phpMyAdmin** i prefer to use DataGrip from JetBrains. I wont go through the specifics of that as it will depend on what you want to install. But if your following along and using Datagrip also you can simply run:

 ```
sudo snap install datagrip --classic

```

### Install Laravel!

This bit is just a single command...

 ```
composer global require laravel/installer

```

And that's it, CD into your website directory and run the:

 ```
laravel new *site* - change site to you site name

```

running the above command will add the relevant links so you can run site.test and you will be directed to your new application.

All done, you now have a fully functioning Laravel development server locally with Elementary OS.

Other things to do
------------------

Make sure you setup your local .env if your pulling from Git.... I stupidly spent around an hour wondering why i couldn't access my site! its because i didn't have an .env properly configured for local environment.

Another thing i tend to do is change the domain from test to **.vm** this is easily done by running the following command:

 ```
valet domain vm

```

Apache2 still showing up when you try access one of your sites? This happened with me and i needed to run the following to stop Apache2 from startup up with system reboot:

 ```
sudo systemctl disable apache2 && sudo systemctl stop apache2

```

That should stop Apache2 from starting when the machine boots up, we now need to get **nginx** starting up with the following command:

 ```
sudo systemctl enable nginx

```

Need to install NodeJS? use the following two commands one for NodeJS itself and also the build essentials.

 ```
curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
sudo apt-get install -y nodejs

```

and

 ```
apt-get install -y build-essential

```

All done!

So far this has been my biggest Blog post so i hope it was all good, if you have any suggestions please drop me a message :D

### Final Word

I hope this little walk through has has been helpful for you all!.

if you found this post useful please consider following me on twitter [@skino2020](https://twitter.com/skino2020) and if you found it really helpful consider buying me a coffee [here](https://www.buymeacoffee.com/skino2020).<br><br> <h2>Raspada-Blog</h2>  I post on my blog primarily and share the posts via API, please check out [Raspada-Blog](www.raspada-blog.co.uk) for more posts and information. If you have any questions please message me on twitter or use my website contact form.