Install Laravel 4 on Ubuntu 12.04 LTS (a how-to tutorial)

Laravel 4 is the big thing. Every blog talks about it, nearly every developer-twitter-account mentions it. Hmm, looks like everybody should try it out ! But – to my surprise – the official installation tutorial is quite confusing and also incomplete. If you install Laravel exactly like described on http://laravel.com/docs/quick#installation, well, then you’ll be stuck right after the first seconds. There’s another, more detailed installation tutorial, right in http://laravel.com/docs/installation, but this will also not clearly help you with the installation process as it simply leaves out essential steps in the setup, and you’ll have to do a lot of additional stuff to get this thing running. That’s probably why you are reading this, right ? ;) So here’s “my” full step-by-step guideline on how to install laravel 4.0 and 4.1 on a naked Ubuntu 12.04 LTS.
Preparing the server / environment
Log in into your naked Ubuntu 12.04 LTS and do an update and upgrade:
sudo apt-get update sudo apt-get upgrade
Choosing the right PHP version: Laravel 4 uses Composer to install. And currently there’s a bug in composer which makes installation of dependencies extremely slow, like 60 minutes for something that usually takes 10 seconds. This bug only happens on PHP 5.3 systems, so installing PHP 5.4 might be a good choice when Composer runs very very slow. Upgrading to PHP 5.5 is (currently, November 2013) not a good choice as this will also install Apache 2.4 which has different config files, and I also don’t know how to rewrite the Apache 2.2 stuff to 2.4.
OPTIONAL: Preparation of installing PHP 5.4: This can be skipped if you are fine with 5.3.
sudo apt-get install python-software-properties sudo add-apt-repository ppa:ondrej/php5-oldstable sudo apt-get update sudo apt-cache policy php5
The last command shows the to-be-installed version of PHP (on the top) . Should be 5.4.x! This process replaces the default PHP version by the chosen one, in this case the “old-stable”, 5.4.
Installing Apache, PHP and MySQL
sudo apt-get install apache2 sudo apt-get install php5 sudo apt-get install mysql-server sudo apt-get install php5-mysql
The installation process is self-explaining. You can prove the installed versions of PHP and Apache with:
php -v
and
apache2 -v
Installing necessary PHP extensions
sudo apt-get install unzip sudo apt-get install curl sudo apt-get install openssl sudo apt-get install php5-mcrypt
Usually these 4 are not installed by default, but they are necessary to 1.) unzip the .zip’ped Laravel 4 download, 2.) use Composer (which needs CURL), 3.) use Composer (which downloads via HTTPS, useable only with installed OpenSSL) 4.) use Laravel 4 (which needs mcrypt).
Install Composer (systemwide)
In case you wonder: The download of Composer into each project “by hand” is not recommended anymore. We use the “real” installation of Composer here:
curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer
Activate mod_rewrite
Install the mod_rewrite module (or extension or whatever it is) and restart the Apache:
sudo a2enmod rewrite sudo service apache2 restart
Open the default vhost config file:
sudo nano /etc/apache2/sites-available/default
Now search for “AllowOverride None” (which should be there TWO times) and change both to “AllowOverride All“. Search for these two lines
DocumentRoot /var/www <Directory /var/www>
and change them to
DocumentRoot /var/www/public <Directory /var/www/public>
This will route a user who enters your server directly to /var/www/public (the base folder of Laravel 4) instead of /var/www. Exit and save with CTRL+X, Y, ENTER.
Install Laravel 4
These commands will move you into the web root (/var/www), download Laravel from GitHub, extract the files into a folder called “laravel-master”, move it’s content into /var/www and delete the download and the empty folder:
cd /var/www wget https://github.com/laravel/laravel/archive/master.zip unzip master.zip && cd laravel-master/ && mv * ../ && cd .. rm -r laravel-master && rm master.zip
Run the installation with Composer by
composer install
Note from comments: Eventually you’ll have to do a sudo composer install.
Make the storage folder writeable (777 is too much for production servers, but it’s okay for this tutorial. Would be cool if more experienced linux guys could comment here: What’s the best overall-working way to give PHP write-rights to this folder ?)
sudo chmod -R 777 app/storage
and restart the server:
sudo service apache2 restart
First run of Laravel 4
When you navigate to your server’s IP (or domain), you should see the Laravel startscreen:
Now edit app/routes.php and add a new route:
Route::get('/mytest', function() { return "Oh yeah, this really works !"; });
When you navigate to your server’s IP/adress and add “/mytest” to the URL, you should see “Oh yeah, this really works !” in the browser.
http://your-domain-or-ip/mytest
This is basically how laravel works. To get an idea what’s possible with routes, have a look on http://laravel.com/docs/quick ! Feel free to comment, share and rate this tutorial.
Excellent, what a webpage it is! This weblog provides valuable facts to us, keep it up.
Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You definitely know what youre talking about, why waste your intelligence on just posting videos to your weblog when you could be giving us something enlightening to read
Excellent site you have here but I was curious if you knew of any forumsthat cover the same topics discussed here? I’d really love to be a part of community where I can get suggestions from other experienced individuals that share the same interest.
I think that is one of the so much important info for me.
And i am happy reading your article. But want to observation on few
general issues, The website taste is great, the articles is truly excellent
thanks for sharing
Instead of manually setting up an Ubuntu based environment on a server for Laravel website, I think going with some solution that automates this process would be more efficient. Like Cloudways. This platform is great for hosting Laravel websites because the platform setups the server automatically. This means you can skip the server setup and configuration part and get right down to working on your Laravel website.
Looking forward for the updated version :D
Great tutorial. Can you please update it for the latest version of Laravel 5.5? It would be awesome
Thi is the best guide I’ve seen on the topic thank you so much!!!! For my scenario I was installing it on amazon linux and upgraded to httpd24 and php54 first, and laravel is now v5. *Most* items here worked, only a few I had to figure out what to change but the process as you’ve laid out worked great. Cheers :D
this command does not work;
sudo nano /etc/apache2/sites-available/default
Thanks alot! Great tutorial worked very well
I have a fresh install of Debian, followed everything, I get the Laravel homepage but when I browse to the route /about, I get 404 not found.
Anyone explain me ?
Hi everyone,
I follow the following steps and install successfully but a major problem comes,
when I’m try to access another project folder like : http://localhost/demo
than error comes like :
Not Found
The requested URL /demo was not found on this server.
Apache/2.2.22 (Ubuntu) Server at localhost Port 80
Nice Tuto! Thanks!
This installation tutorial is awesome . will you provide me another laravel Example for beginners like how to create controller , model or will you provide any small application tutorial in laravel
Thanks Chris
Thanks for the tutorial! it works great! (some people already mentioned the caveats found with some distros – Mint in my case).
How would you go about if you want to work on several projects? I understand this creates one Laravel web app. Can I work on more than one at the same time?
Thanks for your detailed instruction to set up the laravel. I follow the same all steps. However i am facing one issue. When i used to run the url in browser as you suggested either http://localhost or http://localhost/mytest
It just display the pop up with filename do you want to download etc.. But in browser i can’t see anything.
Could you please help me to get resolve this.
Thanks in advance
Thanks for your detailed instruction to set up the laravel. I follow the same all steps. However i am facing one issue. When i used to run the url in browser as you suggested either http://localhost or http://localhost/mytest
It just display the pop up with filename do you want to download etc.. But in browser i can’t see anything.
Could you please help me to get resolve this.
Thanks in advance
Thanks for the great tutorial!
Instead of chmod 777’ing the app/storage folder I chown’d it:
sudo chown -R www-data:www-data /var/www/app/storage
I’m not a Linux expert, but basically the idea is to give ownership of the storage directory to Apache’s user & group www-data instead of changing the write permissions.
Also, on Ubuntu 14.04 my default vhost was named 000-default.conf and I only had to change the document root.
I used this guide to fix the mcrypt error:
http://askubuntu.com/questions/460837/mcrypt-extension-is-missing-in-14-04-server-for-mysql
Thanks! This article was written before 14.04 came out, and as 14.04 ships with a newer version of Apache that also has different configs (!) and different config file names (as you already said). I’ll add a better version and a guide for 14.04 when there’s time! :)
This tutorial is probably the best “we” tried so far. I and my teammates tried several tutorials but nothing worked perfectly or ideally as a submodule-like installation. And found the mcrypt needs to be enabled before execute “install composer” in localhost folder. Hereunder is some commands that may require on Ubuntu 14.04/PHP 5.5.9/Apache/2.4.7:
1. To enable mcrypt: $ sudo php5enmod mcrypt
2. Install Composer:
2.1 I have permission problems even prefix sudo, so root solved the problem: $ sudo -i
2.2 Since logged in root mode…
2.2.1 $ cd /root
2.2.2 Then $ mv composer.phar /usr/local/bin/composer
2.2.3 Don’t forget to: $ exit
2.2.4 Back to the dev-metal tutorial!
3. Specific to my freshly installed Apache:
3.1 Only has ONE “AllowOverride None” at /etc/apache2/apache2.conf.
3.2 Change localhost dir at /etc/apache2/sites-available/000-default.conf
4. I create a public folder under /var/www. But the folder must be empty or laravel installation won’t complete.
Thank you Chris!!!!
Thanks you too, I’ll use this for detailed tutorial for 14.04 soon! Merci!
There’s one crucial step you need to do before running the command “sudo composer install” on Ubuntu 14.04.
You should also run: sudo php5enmod mcrypt . If you don’t, it will give an error saying that the mcrypt module is missing.
I’ve not had any problem with leaving my storage folder with a 755 permission. Is it likely there are future errors? Does it really matter to give it full drwx permissions?
It was really helpful for me. Thank you!
Great guide!
But it only seems to work if I try http://127.0.0.1/index.php/mytest
Otherwise it’ll say not found. I wonder where I can change this…
I’m using ubuntu 14.04 and must be try it on virtualbox :D
Thanks. It works on my virtualbox
It works for me. Thanks buddy
Muchas Gracias, Saludo desde Chile lml !
Thanks…
Great guide, But do you have one for Ubuntu 13.10 ?
The apache settings loads differently and cant figure it out.
Thank you so much, Chris! I’ve been fiddling around with frameworks (Jersey, RoR) and have failed to get them working properly. This one has easily been the most friendly for someone who is still learning about setting up environments from scratch (nice to see what I’ve learned is mirrored in your directions here). I know you talk about how the install docs on the Laravel site are incomplete, but would you say they are plenty sufficient for someone who knows more of what they are doing in dev environment setups? It seems fairly common that they assume a lot which usually leaves me lost on where to go next.
Cheers
I’m having a problem with running “curl -sS https://getcomposer.org/installer | php” it says The json extension is missing. Install it or recompile php without –disable-json
sudo apt-get install php5-json
Yaa its very help full. I fixed my issue, when its happens by followed official site.
Hi,
I tried the way you said and it works well at the first attempt, but then I restart my pc and after that when I am trying to access my localhost it is blank.
what goes wrong ? can you throw some light on it.
vaibhav
This guide is great! It’s weird that 9 times out of 10, the developers/owners of products cannot explain properly to all levels of users – they always assume that readers are at the same level as they are – very frustrating.
Hi Chris.
I am newbie on laravel framework and ubuntu. I have been follow your tutorial, but I’m stuck on “sudo chmod -R 777 app/storage” because there not running and show “No such file or directory”.
Could you help me?
Thank you.
Hey, sorry these are the absolute basics of web development. Go to the folder where app/storage is in and do the command. If you are stuck with this, then Laravel might be too much for you.
Thank you Crish, it’s work now.
Basicly i’m on web design and front-end.
Well, I’ll still learn this framework :)
Thank u…
When I am Run the installation with Composer by “composer install”, I got error message.
And then I am try “sudo composer install”, it work..
I love this tutorial..
Thank You…
Thanks! I’ll add this to the article (but it’s possible to run composer install without sudo, simply because composer is just 100% php, not a real unix script afaik)
Thank you for this tutorial, really helped me.