Ubuntu Dev Environment Setup

Why Ubuntu?

So as mentioned in my previous blog entry, i'm moving away from my iMac to a Linux Dev environment, it will cost me far to much to upgrade the iMac for little gain. So with this post i'm going to outline my steps to installing a Dev Environment on Ubuntu 20.04 and basic setup and plugins for Visual Studio Code.

All of the bits and bobs in here are from snippets i have had in notes from various websites and forums overs the last six months, so If you spot anything i have got wrong or missed let me know, I've already done this on my main machine so this tutorial is going to be written whilst running a VM.

Let's Get started!

For this post i have installed a fresh installation of Ubuntu 20.04 with nothing else installed. i won't talk you through installing Ubuntu as, if you're reading this post i'll assume its already installed in some way shape or form.

Let's open up our terminal application and update our apt packages.

sudo apt update
sudo apt upgrade

This can take seconds or a couple of minutes.

You will get the usual message about disk space whilst installing applications, Just answer Yes as usual.

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

Install Git (used full grabbing OhMyZsh later)

If your setting up a laravel environment like i am, you probably want git anyway for your projects, but its also required for OhMyZsh a little later on.

sudo apt install git
-- When install is finished you can check version with: --
git --version

And that's all you need to do. Git is installed.

Time to install ZSH and some dependencies

I cant remember where i saw this but i picked it up from a discussion somewhere on the web a while back on installing OhMyZsh.

sudo apt install zsh

Once installed were going to install OhMyZsh to manage our ZSH configurations. There is a shed load of presets online for this as well. One i use on my iMac that makes file paths look good and also will not down branches your currently working on in Git. you can find install instructions for this here: Romkatv / powerlinelevel10k. The config isn't difficult, just follow the instructions on the Git repository and you'll be fine.

Installing OhMyZsh is a piece of cake. Run the following command in your terminal.

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

If its a fresh install of Ubuntu you will probably get an error like;

Command 'curl' not found, but can be installed with:
sudo apt install curl

Run the install which takes a couple of seconds to download and install and rerun our previous curl command.

Newer ZSH installs will offer to change the Default shell for you:

Looking for an existing zsh config...
Using the Oh My Zsh template file and adding it to ~/.zshrc.

Time to change your default shell to zsh:
Do you want to change your default shell to zsh? [Y/n] y
Changing the shell...
Shell successfully changed to '/usr/bin/zsh'.

Time to start on PHP and its dependencies

We need to install a repository for PHP.... most tutorials will tell you to install PHP straight away... but so far it's never worked for me... i've always had to install the repository first.

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 php7.4

After PHP has installed you can check it with the following command which should give you the output below as well.

➜  ~ php -v

PHP 7.4.16 (cli) (built: Mar  5 2021 07:54:38) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.16, 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
  • json
  • mbstring
  • MySQL
  • XML
  • zip
  • sqlite3

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

sudo apt install openssl php7.4-common php7.4-bcmath php7.4-curl php7.4-json php7.4-mbstring php7.4-mysql php7.4-xml php7.4-zip php7.4-sqlite3

Time to install Composer

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 incase, 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.

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 ubuntu 20.04 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;

export PATH

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

Now apply them changes by typing:

source ~/.zshrc

Install Valet

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 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:

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 website_data. so lets make that in the root:

mkdir ~/website_data

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 ~/website_data
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.

Installing 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 update -- Not really needed now as we already done it
sudo apt install mysql-server

Remember to just accept the installation message for MySQL.

Once this is complete you will need setup MySQL for use... this can be done in a few different ways, i prefer the mysql_secure_installation method as it asks you questions on how you want to setup your MySQL environment.

sudo mysql_secure_installation

During this installation the only question you really need to answer is the one on password security you can set it at "0" as its a local environment, however if you're using this method to install MySQL on a server like i did you'll want to choose a higher security.

Every other question you get, Just answer yes to.

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

systemctl status mysql.service

Which gives the following output:

➜ website_data systemctl status mysql.service
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: e>
   Active: active (running) since Sun 2021-03-21 13:25:21 GMT; 8min ago
  Main PID: 47696 (mysqld)
   Status: "Server is operational"
   Tasks: 38 (limit: 2315)
   Memory: 323.4M
   CGroup: /system.slice/mysql.service
       └─47696 /usr/sbin/mysqld

Now if you don't want to manage the DB's with a GUI instead of going through command line you can install MySQL Workbench or phpMyAdmin i prefer the former so I'll be installing that. I wont go through the specifics of that as it will depend on what you want to install.

Time for 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 Ubuntu 20.04.

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


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